In general, it's advisable to lock the resources your program is working on, not the program itself. Think about the consequencies in case of abnormal termination and so on.

Anyway, There's More Than One Way To Do It. The most straightforward is probably using a wrapper, that is, a pprogram that does some housekeeping before calling the actual one.

I'd suggest a wrapper and not changing yuor program itself because changing the program for adding this "feature" needs to be seriously motivated, in my opinion. Again, I think that locking resources is a cleaner approach.

Unfornutately you can't use perl's flock because it requires a file to be open in write mode for having an exclusive lock, and production scripts that can be open in write mode are not a Good Thing.

So you may resort to a couple of quite common tricks (watch out, ythey're not immune from race conditions! But most programmers seem not to care too much about it):

Both approaches can be implemented with simple shell scripts, valid in general for every program to be run in "exclusive" mode. As a side note, this kind of locking is only advisory! Malicious programs can circumvent it if they want.

If you want to change your code, you can use Perl's flock in a number of ways, always locking a dummy file, not the program itself!

For example (untested code):

#!/usr/bin/perl -w use strict; open(EXCLUSIVELOCK, ">/tmp/$0.lock"); flock(EXCLUSIVELOCK, LOCK_EX); # If we reach this point, we have exclusive lock # The rest of the code goes here, immediately before # terminating, you can release the lock flock(EXCLUSIVELOCK, LOCK_UN);

This type of locking is always advisory, but it's exempt from race conditions.

This should be a good starting point to modify according to youir needs, for example not timeouts are implemented: if the file is locked, a second program would wait until the first has finished. You may want to check the perlfunc manual page to give an error and exit instead, or implement timeouts and so on. Happy locking!

-- TMTOWTDI


In reply to Re: Locking files by trantor
in thread Locking files by Ras

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.