Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple processes accessing the same file

Tags:

c

Is it alright for multiple processes to access (write) to the same file at the same time? Using the following code, it seems to work, but I have my doubts.

Use case in the instance is an executable that gets called every time an email is received and logs it's output to a central file.

if (freopen(console_logfile, "a+", stdout) == NULL || freopen(error_logfile, "a+", stderr) == NULL) {
    perror("freopen");
}
printf("Hello World!");

This is running on CentOS and compiled as C.

like image 568
David Beck Avatar asked Mar 26 '12 22:03

David Beck


People also ask

Can multiple process access same file?

During the actual reading and writing, yes. But multiple processes can open the same file at the same time, then write back. It's up to the actual process to ensure they don't do anything nasty. If your writing the processes, look into flock (file lock).

What happens when two processes open the same file?

In this case, that means that one of the programs will create and open the file, and the other will be unable to open it. The process of opening (and if necessary creating) the file is atomic, so it will never happen that one process will create the file but the other process will open it.

Can two processes execute the same program?

A computer program is a passive collection of instructions, a process is the actual execution of those instructions. Several processes may be associated with the same program; for example, opening up several instances of the same program often means more than one process is being executed.

Can multiple processes read the same file C#?

If you want to read from the same file in many instances of the program, with no instance writing to it, then that is also easily doable. Just open the file in shared read-only mode, and there is no problem at all letting lots of programs read the file simultaneously.


2 Answers

Using the C standard IO facility introduces a new layer of complexity; the file is modified solely via write(2)-family of system calls (or memory mappings, but that's not used in this case) -- the C standard IO wrappers may postpone writing to the file for a while and may not submit complete requests in one system call.

The write(2) call itself should behave well:

   [...] If the file was
   open(2)ed with O_APPEND, the file offset is first set to the
   end of the file before writing.  The adjustment of the file
   offset and the write operation are performed as an atomic
   step.

   POSIX requires that a read(2) which can be proved to occur
   after a write() has returned returns the new data.  Note that
   not all file systems are POSIX conforming.

Thus your underlying write(2) calls will behave properly.

For the higher-level C standard IO streams, you'll also need to take care of the buffering. The setvbuf(3) function can be used to request unbuffered output, line-buffered output, or block-buffered output. The default behavior changes from stream to stream -- if standard output and standard error are writing to the terminal, then they are line-buffered and unbuffered by default. Otherwise, block-buffering is the default.

You might wish to manually select line-buffered if your data is naturally line-oriented, to prevent interleaved data. If your data is not line-oriented, you might wish to use un-buffered or leave it block-buffered but manually flush the data whenever you've accumulated a single "unit" of output.

If you are writing more than BUFSIZ bytes at a time, your writes might become interleaved. The setvbuf(3) function can help prevent the interleaving.

It might be premature to talk about performance, but line-buffering is going to be slower than block buffering. If you're logging near the speed of the disk, you might wish to take another approach entirely to ensure your writes aren't interleaved.

like image 130
sarnold Avatar answered Oct 21 '22 20:10

sarnold


This answer was incorrect. It does work:

So the race condition would be:

  1. process 1 opens it for append, then
  2. later process 2 opens it for append, then
  3. later still 1 writes and closes, then
  4. finally 2 writes and closes.

I'd be impressed if that 'worked' because it isn't clear to me what working should mean. I assume 'working' means all of the bytes written by the two processes are inthe log file? I'd expect that they both write starting at the same byte offset, so one will replace the others bytes. It will all be okay upto and including step 3. and only show up as a problem at step 4, Seems like an easy test to write: open getchar ... write close.

Is it critical that they can have the file open simultaneously? A more obvious solution if the write is quick, is to open exclusive.

For a quick check on your system, try:

/* write the first command line argument to a file called foo
 * stackoverflow topic 9880935
 */

#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

int main (int argc, const char * argv[]) {
    if (argc <2) {
        fprintf(stderr, "Error: need some text to write to the file Foo\n");
        exit(1);
    }

    FILE* fp = freopen("foo", "a+", stdout);

    if (fp == NULL) {
        perror("Error failed to open file\n");
        exit(1);
    }

    fprintf(stderr, "Press a key to continue\n");
    (void) getchar();       /* Yes, I really mean to ignore the character */

    if (printf("%s\n", argv[1]) < 0) {
        perror("Error failed to write to file: ");
        exit(1);        
    }

    fclose(fp);

    return 0;
}
like image 38
gbulmer Avatar answered Oct 21 '22 21:10

gbulmer