Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to update a file for each process and let each process read the newest version of the file.

I make a temporary file and copy the new version over the old one. I lock the file during write.

It is possible one process read the file before the write is complete. How to guaranty hundreds of processes read the file at same time and each process get the different newest one? Suppose I do not have database.

Thanks everyone.

  • Comment on Each process needs to read the newest version of the file

Replies are listed 'Best First'.
(tye)Re: Each process needs to read the newest version of the file
by tye (Sage) on Jan 20, 2001 at 01:17 UTC

    Don't copy the old file to the new file, rename the old file over the new file. Then you don't need a lock and the only race condition is that a process might (depending on OS and how you do the rename) see no such file for a very short bit of time.

    Update: The rename() function is atomic on many operating systems, by the way. And make sure each process doesn't open the file in a way that prevents it from being renamed/deleted/unlinked. For Unix, that isn't a problem. For Win32, unfortunately, I don't think I have a great solution at the moment -- but I'm close so let me know if you (or anyone) needs a fix for this.

            - tye (but my friends call me "Tye")

      Thanks, Tye.

      I want to modify line by line of a file to make a temporary one and rename it to the original file. If the file is really big, it may take a while to get it done. During the time, there might be a lot of processes reading the file at same time. How to make sure what each of this processes read is the newest modified version?

      Thanks again.

      JJ

        I'm not sure I understand. If you do what I suggested which is what you just redescribed above, then the readers will always read the most recent completed version of the file each time they reopen the file.

                - tye (but my friends call me "Tye")