in reply to Each process needs to read the newest version of the file

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")
  • Comment on (tye)Re: Each process needs to read the newest version of the file

Replies are listed 'Best First'.
Re: (tye)Re: Each process needs to read the newest version of the file
by Anonymous Monk on Jan 21, 2001 at 08:02 UTC

    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")