in reply to Avoiding a race condition

I don't have time to test this, but you should be able to unlink the file before you close it. Unix will mark a file to be deleted, but it won't actually delete the file until the file is closed.

You can also open the file in read/write mode. Read from the file then truncate the file to nothing and close it.

open (FILE,"+</path/file"); flock (FILE,2); # Do something with the data seek (FILE,0,0); truncate (FILE,tell (FILE)); close FILE; #file unlocks when closed

Replies are listed 'Best First'.
RE: Re: Avoiding a race condition
by ZZamboni (Curate) on May 19, 2000 at 05:37 UTC
    This is correct. In fact, it is a very common practice for temporary files to unlink them right after opening them. Only the processes that already had them open will be able to continue accessing them, and the file will magically vanish when the last process closes it.

    From the unlink(2) man page under Linux:

    If the name was the last link to a file but any processes still have the file open the file will remain in existence until the last file descriptor referring to it is closed.

    --ZZamboni

      This is a common practice for unix. Not all platforms allow you to unlink an open file.
        Very true. I'm not very used to think in non-Unix terms... :-)

        --ZZamboni