Is fopen() a thread safe function in Linux?

15,477

Solution 1

If two threads both open the same file with fopen(), they will each have independent file streams (FILE *) backed by independent file descriptors referring to the same file. You can write independently to the two file streams, but the net result on the file will depend on where the threads write and when they flush the file stream. The results are unpredictable unless you control where each thread is writing. The simplest thing is to make sure both threads use the same file stream, but you probably still need to coordinate between the threads. Note that POSIX requires the C functions to give coordinated access to the file stream — see flockfile() which imposes the requirement that

All functions that reference (FILE *) objects, except those with names ending in _unlocked, shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these (FILE *) objects.

If you open the file in append mode in both threads, then the writes would be safely at the end of the file each time, but you still have to worry about flushing the data before the buffer fills.

Incidentally, if you open the file in append mode (O_APPEND with open(), using "a" with fopen()), then all writes should be at the end of the file, and you should not get into trouble with interleaved writes — unless, perhaps, your independent threads are using file streams and writing more than a buffer-full at a time, or they are using fflush() after writing parts of each line of output, or they are using write() or one of its myriad relatives to write parts of a line each time. There are ways to run into problems even with append mode, but you typically have to be trying to run into them.

Solution 2

fopen() is reenterable, and you can have as many descriptors pointing to the same file as you like.

What you get as a result of reading/writing from/to the file using multiple descriptors is not a thread safety question, but rather concurrent file access, which in most cases (apart from when the file is read-only) won't work well.

Share:
15,477
Matthewgao
Author by

Matthewgao

Updated on July 29, 2022

Comments

  • Matthewgao
    Matthewgao almost 2 years

    If I use fopen() call to open a same file in multi-thread, and write data to the file. Should I use a mutex to ensure the data won't be disordered?

  • Kamen Stoykov
    Kamen Stoykov almost 11 years
    Actually I'm saying that you should try it. Run two threads and you will see how messed up the files will be. What am I speculating ?
  • stdcall
    stdcall almost 11 years
    "So I think the situation in C will be equivalent to Java." - this can easily be written as a comment, not an answer.
  • ransh
    ransh almost 9 years
    I'm not sure I understood, if I access (read/write) differen files at the same time, should I expect any problem?
  • Jonathan Leffler
    Jonathan Leffler almost 9 years
    @ransh: If two file streams are opened on different files, there will be no problem. If they're opened on the same file, there will be problems.