Results 1 to 7 of 7
Hi,
I am trying to write into a single log file from 3 different applications in C. In one of the applications I am forking out 5 instances. I would ...
- 04-01-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 18
writing to log file in C
Hi,
I am trying to write into a single log file from 3 different applications in C. In one of the applications I am forking out 5 instances. I would like to know what is the best way to open and close the log file in which i want to write from these applications. Should I open and close it at the start and end of each application or is it ok performance wise if i open and close it inside the log function which will be called for every write. Any advise on how to go about this will be great!
Thanks
- 04-01-2010 #2Just Joined!
- Join Date
- May 2009
- Posts
- 2
do this
open in exclusive mode.
if OK, then write then close & exit open file in append mode
if fails, sleep maybe 50 milliseconds & retry.
- 04-02-2010 #3Just Joined!
- Join Date
- Apr 2006
- Posts
- 2
It could be possible for 3 applications and multiple instances of said applications to get in each other's way... 4 or 5 at a time might backlog, sleeping while waiting for the file to be in a closed state, and if you hard-code a standard 50 millisecond sleep you could end up with a race condition, with multiple instances waking and sleeping at the same time. Better would be a (Pseudocode), been some time since I used C:
sleep=Rand() mod 50
Best, would be: write a shared library used by all the applications and instances, that opens the standard log file. Have each app and instance send a message to the library. The lib would then
if not exist log
create log
open log
receive message
note calling app and instance identifier if necessary
write message to log, with caller identifier if necessary
Then, either
close log
Rinse Repeat
Or if you prefer, just flush writes and wait for more messages, until last app instance using the library closes. Have the apps register with the library, IE indicate to library when they are launched and when they exit.
When no more instances running close the log.
While not easier to code, (I for one don't actually know how to write a shared lib), and can't tell you how to... That method is one of the two reasons shared libraries exist. Shared code, and shared resources... in this case, the log file. That will prevent collisions and allow the apps to do their main jobs without worrying about the log writes.
EDIT: Meant to say, I don't know how to write a shared lib in C, been too long since I studied it and didn't use it outside of school.
- 04-02-2010 #4
Or use the syslog call using one of the LOG_LOCALx facilities, and configure the log file in rsyslog.conf. rsyslogd will handle the file contention.
- 04-02-2010 #5Just Joined!
- Join Date
- Apr 2006
- Posts
- 2
There's always someone who knows more, or at least remembers it quicker. Was just learning about using system logger last week, mod +1 the above. Why reinvent the wheel? The more I learn about Linux, the more I know I dont know.
- 04-02-2010 #6Just Joined!
- Join Date
- Oct 2008
- Posts
- 3
syslog-ng is definitely the best and the easiest way to go. Version 3.1 is just out and has lots of improvements and features. OSE edition is the FREE one.
Check out this blog from the author of syslog-ng, Bazsi to learn more about it: bazsi<dot>blogs<dot>balabit<dot>com
- 04-05-2010 #7Just Joined!
- Join Date
- Apr 2010
- Posts
- 18
Thank you so much for all of your responses.
I am currently doing a file lock, write and unlock..it seems to be working fine..but after I added the logging to one of the applications (not sure if this is even related to the problem but I have been facing the issue since then) the number of messages my application processes per second drastically changes each time i run it.
I use shared memory programming to pass messages between applications. Earlier it was consistent at 11-12 messages per second. Now i get numbers like 100 once and 200 second time and 50 the next time. Not sure what is happening. This is the first time I am working with Shared memory. If any of you have had any such experience, do let me know.
Thanks,


Reply With Quote
