LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-13-2006, 11:43 AM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
Different threads writing to the same file


Hi all!

My program is formed by three threads. What happens if all these threads have write access to a log file? What would be the worst case? I don't care if one thread writes its log before the others but I'm afraid of loosing any log. I didn't want to develop a complicated mechanism just to write logs

Thanks,

Daniel.
 
Old 09-13-2006, 06:16 PM   #2
wwwdev
LQ Newbie
 
Registered: Sep 2006
Posts: 6

Rep: Reputation: 0
Hi estratos,

Quote:
Originally Posted by estratos
Hi all!

My program is formed by three threads. What happens if all these threads have write access to a log file? What would be the worst case? I don't care if one thread writes its log before the others but I'm afraid of loosing any log. I didn't want to develop a complicated mechanism just to write logs
your log file may be wrong. you can use mutexes(pthread_mutex_lock/unlock) or flock.
 
Old 09-13-2006, 06:44 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
You can establish a fairly simple locking system.

Code:
while lock file exists
  wait
end while
touch lock file
write logs
delete lock file
That way you will not lose any log records. However this is really only acceptable if writing to log records is infrequent, otherwise it might lead to one thread bing locked out.

Last edited by graemef; 09-13-2006 at 06:46 PM.
 
Old 09-13-2006, 09:55 PM   #4
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
Anything that threads do with respect to any shared object should be synchronized explicitly. The amount of time needed to do that is completely inconsequential (the I/O operation dwarfs it), and for reliability it's essential.

You might consider whether you want multiple threads to be writing to the same file anyway. That's a "big, fat, slow thing" to be synchronizing against and it could easily wipe out whatever advantages having multiple threads may give you!
 
Old 09-14-2006, 02:20 AM   #5
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Thank you very much for your replies.

I have a last question: what about calling a function from multimple threads? This function would be defined in my code and doesn't alter any shared object.

Sorry for this trivial question.

Daniel.
 
Old 09-14-2006, 09:33 AM   #6
Ztyx
Member
 
Registered: Dec 2001
Location: Stockholm, Sweden
Distribution: Ubuntu, Kubuntu and Debian
Posts: 338

Rep: Reputation: 30
You could have a FIFO-stack which is emptied by a fourth logging-thread. This way only one thread will write to your log file. Just an idea...
 
Old 09-15-2006, 01:16 AM   #7
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Ztyx
You could have a FIFO-stack which is emptied by a fourth logging-thread. This way only one thread will write to your log file. Just an idea...
Hi Ztyx.

I'll use the FIFO stack but the logging-thread will be one of my threads already running. I don't want to create a fourth one.

Thank you very much,

Daniel.
 
Old 09-15-2006, 02:00 AM   #8
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
Originally Posted by graemef
You can establish a fairly simple locking system.

Code:
while lock file exists
  wait
end while
touch lock file
write logs
delete lock file
That way you will not lose any log records. However this is really only acceptable if writing to log records is infrequent, otherwise it might lead to one thread bing locked out.
There is still a risk of simultaneous writes with this code, as the control can be passed from one thread to the other just before the former was about to create the lock file, resulting in two threads believing they own the log file at the same time.
This risk is higher with multicore/multi CPU O/Ses.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Do the threads lock on Meesage queues before writing to it? asarkar Programming 2 09-22-2006 05:06 PM
Any test suite in linux for threads, file descriptors and shared memory segs. Basavaraj Linux - Newbie 2 08-31-2006 08:47 PM
writing an xml file? ati Programming 3 05-05-2006 09:14 AM
Java threads listed using kill -3 does not contain all threads found using ps -auxww coneheed Programming 2 11-14-2005 08:57 AM
writing to file ej25 Programming 17 11-19-2004 11:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration