in reply to Read / Write Server

Try this.
  1. There is a file, fully written and readable.
  2. Along come the readers and life is good.
  3. Jill adds something to the end of the file, but Bob only sees half.
    • Bob does not see the trailing EOL, so he is well aware the file is truncated.
  4. Bob keeps reading, IO::Select until he is content or gives up timing out and handling the error. Ignoring partial data, ect.
    • In many cases Bob can just ignore the last record, it's likely he will read it later anyway.
  5. Bob needs to edit the file, cleaning up after Jill or anything else that's not appending to the file.
  6. Bob safely creates a new file with a similar name that's always used when updating the file.
  7. Bob fills the file till it's complete, reading and copying an newly opened original.
  8. Bob closes the new file, flushing it to disk.
  9. Bob renames the new file ontop of the original.
  10. Bob looks at the original(still open) for any additions till he is satisfied that he can quite.
    • fork and Select with time out is good for this.
  11. There is a file, fully written and readable...

Note: Bob will need to set the correct group and group writable permissions as well as have write access to the folder containing the file. The temp file should be in the same folder as the original, because this does not work across mount points.

The above is completely Atomic and works for line buffered files. By Atomic we mean that a reader sees one or the other versions of the file. If it's imperative that the reader always has the freshest data, then you need to use file locks. If using file locks consider using binary data as you'll incur less buffering overhead that is not doing any good.

Keep in mind after step 9, especially step 10, step 6 is accessible to anyone. During step 10 it's imperative that any new updates will be able to pass down a chain of several other updates. For this reason it may be a good idea to skip step 3 and 4, there is a lot of added complexity if there isn't a clear gain from allowing appends to not be done in a new file.