in reply to correct usage of flock?

A potential problem here is that the file still exsits after you delete it, at least on UNIX-like systems. This means another process could open the file, look for a lock, not get one, and when it gets a lock you've unlink'd the file. But UNIX still lets the program write to it, though the data goes nowhere. That's not good, now is it.

One idea you could try is to rename the file first, which would allow programs still having the file open to probably have their data go somewhere, and then look at the renamed file.

Replies are listed 'Best First'.
(Ovid) Re(2): correct usage of flock?
by Ovid (Cardinal) on Jun 06, 2001 at 04:32 UTC

    I think this is a good way to go about it. However, if it's likely that separate invocations of this program will occur, a random filename would be a Good Thing.

    There's also the tried and true trick of writing a sentinal file that other programs check for and then seeking to the beginning of the original file and truncating it, but renaming seems much easier.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.