I’m performing I/O to a single file from multiple threads. Access to this shared file foo is controlled through an advisory file lock (flock(2) with LOCK_EX). foo was opened with fopen(3) mode a+. a+ was chosen because of the documentation stating:
Subsequent writes to the file will always end up at the then current
end of file, irrespective of any interveningfseek(3)or similar.
Simplified, the operations would start:
FILE *fp = fopen("foo", "a+");
...spawn threads...
Writing would continue:
flock(fileno(fp), LOCK_EX);
fwrite(buffer, buffer_size, 1, fp);
flock(fileno(fp), LOCK_UN);
I currently do not have any fflush(3) or fsync(2) calls before the fwrite(3) and am wondering if I should. Does the fopen(3) a+ mode take into account multiple threads hitting the file when calculating the “current EOF”? I know that flock(2) likely has no problem granting me the lock while there is outstanding I/O.
In my limited tests (write very long lines of ASCII text followed by a newline in multiple threads for many seconds, then ensure the number of characters on each line in the resulting file are equal), I have not seen any “corruption” when not using fflush(3) or fsync(2). Their presence greatly decreases I/O performance.
tl;dr:
When using file locks, do I need to flush the stream before writing to a shared file between multiple threads with opened in a+ mode? Multiple forks/different machines writing to a file a parallel file system?
Possibly related:
why fseek or fflush is always required between reading and writing in the read/write "+" modes
That is the wrong kind of lock.
flockis only for locking between processes, not between threads in the same process. Fromman 2 flock:Emphasis added. And…
You want to use
flockfileinstead (or additionally, if using multiple processes as well). Theflockfilefunction which is used for controlling access to aFILE *from multiple threads. From the man page:Like this:
The good news is that on
glibc, you don’t need to lock the file if you only have one function call from stdio.h in each critical section, sinceglibchas afwritethat locks. But this is not true across other platforms, and it certainly doesn’t hurt to lock the file. So if you are running on Linux, you never would have noticed thatflockdoesn’t do what you want, sincefwritedoes it automatically.About append mode: You do not need extra flushes when writing using append mode, unless you want to ensure ordering between different processes that have the same file open (or one process with multiple handles for the same file). You do not need “a+” mode unless you are reading from the file.
Demonstration of
flockIf you don’t believe me that
flockdoes NOT provide thread safety between threads using the same file descriptor, here is a demonstration program.On my system, it produces the following output:
Note that thread 1 does not release the
flock, and thread 2 is able to acquire it anyway. The use of a condition variable ensures that thread 1 does not exit until thread 2 has acquired the lock. This is exactly what theflockman page says, becauseflocksays that the locks are per-file and per-process but NOT per-thread.Summary for atomically appending to a file
In order to make an atomic write between processes and threads, you can do one of two easy things:
Use
writeand write no more thanPIPE_BUFbytes.PIPE_BUFis defined in<limits.h>, on my system it is 4096. If the file descriptor is open inO_APPENDmode, then the write will go atomically to the end of the file, no matter who else is writing to the file (threads and/or processes).Use
writeandflock. If you ever write more thanPIPE_BUFbytes at a time, this is your only option for all writes. Again, if the file is open inO_APPENDmode, then the bytes will go to the end of the file. This will happen atomically, but only from the perspective of everyone with anflock.Additionally,
If you use
<stdio.h>and share aFILE *between threads, you will also need to callflockfilefrom each thread. This is not needed if you use the lower-level POSIX API (open/write/etc). This is also not needed if you useglibcand every write is a single function call (e.g., you want to atomicallyfputs).If you use only one process,
flockis not needed.