Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using fseek/fwrite from multiple processes to write to different areas of a file?

I recently came across a bit of not-well-tested legacy code for writing data that's distributed across multiple processes (these are part of an MPI-based parallel computation) into the same file. Is this actually guaranteed to work?

It goes like this:

  • All processes open the same file for writing.

  • Each process calls fseek to seek to a different location within the file. This position may be past the end of the file.

  • Each process then writes a block of data into the file with fwrite. The seek locations and block sizes are such that these writes completely tile a section of the file -- no gaps, no overlaps.

Is this guaranteed to work, or will it sometimes fail horribly? There is no locking to serialize the writes, and in fact they are likely to be starting from a synchronization point. On the other hand, we can guarantee that they are writing to different file positions, unlike other questions which have had issues with trying to write to the "end of the file" from multiple processes.

It occurs to me that the processes may be on different machines that mount the file via NFS, which I suspect probably answers my question -- but, would it work if the file is local?

like image 906
Brooks Moses Avatar asked May 12 '12 18:05

Brooks Moses


1 Answers

I believe this will typically work but there is no guarantee that I can find. The Posix specifications for fwrite(3) defer to ISO C and neither standard mentions concurrency.

So I suspect it will typically work, but fseek(3) and fwrite(3) are buffered I/O functions, so success will depend on internal details of the library implementation. So, absolutely no guarantee but various reasons to expect that it will work.

Now, should the program use lseek(2) and write(2) then I believe you could consider the results guaranteed, but now it's restricted to Posix operating systems.

One thing seems ... odd ... why would an MPI program decide to share its data via NFS and not the message API? It would seem slower, less portable, more prone to trouble, and generally just a waste of the MPI feature set. It certainly is no more distributed given the reliance on a single NFS server.

like image 55
DigitalRoss Avatar answered Oct 21 '22 00:10

DigitalRoss