Whenever I have a situation where multiple concurrent users or processes need brief exclusive access to some shared resource (usually to rewrite or append to a common data file), I tend to use a separate "semaphore" file -- in fact, I've needed this often enough that when I found a TPJ article by Sean Burke discussing semaphores in detail, I put his demo code into a module, which I now use regularly (there's a link to the article in the module docs).

The basic idea is that the semaphore file is always empty -- it exists purely to establish a lock on some particular resource. Get the lock on the semaphore file, and the resource is yours until you release the semaphore. This eliminates all the loop-holes, race conditions and weirdness of trying to grab and hold a lock on something that you're trying to change.

Of course, this only works in the cases where everyone agrees to cooperate (or the project is designed to enforce cooperation) and "update access" to the shared resource is always moderated via the specified semaphore -- it won't help in the case where some folks think they can ignore the semaphore stuff and just start hacking away at the actual data file (e.g. with vi or whatever).


In reply to Re: Update in-place with temporary file and flocking by graff
in thread Update in-place with temporary file and flocking by geohar

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.