You are not protected against the race condition I mentioned earlier. An unlink in Unix does not release the data from the file until the last file descriptor is closed. The second process will be able to read whatever information is in there (and write to it if it desires). Of course other processes wont know this since the file's name space entry is gone. The second process will continue under the assumption that it was accessing the locked file in a valid state (hey it was there when I opened it!).

In some cases, perhaps the current one, this is ok in others it is not -- it depends on the assumptions being made after this section of code. In general if you are going to do this, it is a good idea to put a comment in your code that says you know about the possible race condition and it is ok.


In reply to Re: Re: Re: Re: Re: correct usage of flock? by bluto
in thread correct usage of flock? by cLive ;-)

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.