Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is glibc's implementation of fprintf() thread-safe?

Is fprintf thread-safe? The glibc manual seems to say it is, but my application, which writes to a file using single call to fprintf() seems to be intermingling partial writes from different processes.

edit: To clarify, the program in question is a lighttpd plugin, and the server is running with multiple worker threads.

Looking at the file, some of the writes are intermingled.

edit 2: It seems the problem I'm seeing might be due to lighttpd's "worker threads" actually being separate processes: http://redmine.lighttpd.net/wiki/lighttpd/Docs:MultiProcessor

Problems

By running 2 or more processes on the same socket you will have a better concurrency, but will have a few drawbacks that you have to be aware of:

  • mod_accesslog might create broken access logs, as the same file is opened twice and is NOT synchronized.
  • mod_status will have n separate counters, one set for each process.
  • mod_rrdtool will fail as it receives the same timestamp twice.
  • mod_uploadprogress will not show correct status.
like image 720
John Carter Avatar asked Feb 27 '09 13:02

John Carter


1 Answers

You're confusing two concepts - writing from multiple threads and writing from multiple processes.

Inside a process its possible to ensure that one invocation of fprintf is completed before the next is allowed access to the output buffer, but once your app pumps that output to a file you're at the mercy of the OS. Without some kind of OS based locking mechanism you cant ensure that an entirely different application doesnt write to your log file.

like image 64
PaulJWilliams Avatar answered Oct 21 '22 19:10

PaulJWilliams