correction: this reply, though maybe not totally stupid on its own, is irrelevant to the OP (thanks to dave_the_m for waking me up.)

I adapted some code from an old Perl Journal article by Sean Burke, and I've been using it for years; I posted it here a while back, in response to a similar SoPW thread.

The basic idea is called a "semaphore" file -- a separate file from the one you intend to modify, whose sole purpose is to be the object of an flock operation. Since you aren't going to modify the semaphore file in any way, you side-step the race condition issue. Once one process owns the semaphore, it can do what ever number of steps it needs to in an "atomic" fashion, then free the semaphore for some other process.

Hope that helps.

(update: I just noticed that the url embedded in my posted "Semfile.pm" code is no longer usable -- 404. TPJ is still available online, and you can get the article reference by searching for "semaphore file Sean Burke", but you have to pay for a subscription to TPJ to see it. Bear in mind that the TPJ subscription is definitely a good investment. Apologies for the broken link.)


In reply to Re: opening files: link checking and race conditions by graff
in thread opening files: link checking and race conditions by danderson

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.