views:

219

answers:

3

Various users are browsing through a website 100% programmed in C (CGI). Each webpage uses fopen/fgets/fread to read common data (like navigation bars) from files. Would each call to fopen/fgets/fread interefere with each other if various people are browsing the same page ? If so, how can this be solved in C ? (This is a Linux server, compiling is done with gcc and this is for a CGI website programmed in C.)

Example:


FILE *DATAFILE = fopen(PATH, "r");
if ( DATAFILE != NULL )
{
    while ( fgets( LINE, BUFFER, DATAFILE ) )
    {
        /* do something */
    }
}
+5  A: 

On Linux, it's perfectly safe for multiple processes to simultaneously read from a file.

R Samuel Klatchko
+3  A: 

It's perfectly safe to read from multiple processes (in any modern system).

A call to fopen() returns a pointer to a FILE structure, which has its own members, like flags, current position, etc.

You should only care if somebody changes the file (e.g: shrink), while others are reading it. But I imagine this isn't your case.

jweyrich
+2  A: 

Concurrent reads from a file (whether from multiple threads -- assuming from separately opened descriptors -- or from multiple processes) is well-defined and permitted on all modern major operating systems. It is only concurrent writes to a file which are ill-defined and which you should not attempt to do without locking (unless you are appending to the file, like a log, and the OS makes such concurrent writes well-defined).

Michael Aaron Safyan