If memory serves, I coded up a primitive two-stage, file-based, self-help locking mechanism. Something akin to what follows. In this example, for file test.dat:

  1. Create a file (lock request token) based on the original filename
    • Include its creation timestamp
    • Include its modification timestamp
    • Include its server and unique PID
    • Make these sortable by creation, server, pid, modification
  2. Loop until it is the oldest lock request for this file
    • Re-create your lock request token file if it's been removed.
    • Update your lock request token modification timestamp
    • Delete requests older than the lock request timeout
    • sleep a short bit (don't burn the CPU)
  3. Create another file (lock bid token) based on the original filename
    • Include its creation timestamp
    • Include its modification timestamp
    • Include its server and unique PID
    • Make these sortable by creation, server, pid, modification
  4. Loop until it is the oldest lock bid for this file
    • Re-create your lock bid token file if it's been removed.
    • Update your lock bid token modification timestamp
    • Delete bids older than the lock bid timeout
    • sleep a short bit (don't burn the CPU)
  5. Unlocking consists of removing the lock request and lock bid files
  6. Long-running processes need to have a mechanism for updating the modification timestamp on the bid token to avoid it being deleted by others using the same self-help system.

Then requests for lock simply queue up. This is not collision-proof, but in a low-volume environment was sufficiently collision-resistent for my needs (the math was favorable).


In reply to Re^3: How to do atomic file locking? by marinersk
in thread How to do atomic file locking? by Acapulco

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.