The code below will do what you want.

Several things could be done to improve the code: 1) the pid could be output with the queue numer ($lock) so that the kill 0 recomened by RazorbladeBidet could be used; 2) better exit handling is needed so that improvement 1 is not needed; 3) the queue (countfile) could have a more robust structure.

use strict; use Fcntl ':flock'; use Time::HiRes qw ( usleep ); sub check_queue { my $lock = shift; my $top; open(FILE, "<countfile") || die; flock(FILE, LOCK_SH); seek(FILE, 0, 0); $top = readline FILE; flock(FILE, LOCK_UN); close FILE; return $lock == $top; } sub add_to_queue { my $lock = 0; if (open FILE, "+<countfile") { flock(FILE, LOCK_SH); seek(FILE, 0, 0); for (readline FILE) { $lock = $_; } $lock++; } else { open(FILE, ">>countfile") || die; } flock(FILE, LOCK_EX); seek(FILE, 0, 2); printf FILE "%d\n", $lock; flock(FILE, LOCK_UN); close FILE; return $lock; } sub remove_from_queue { my $lock = shift; my @procs = (); if (open FILE, "+<countfile") { flock(FILE, LOCK_SH); seek(FILE, 0, 0); for (readline FILE) { if ($_ == $lock) { next; } push @procs, $_; } flock(FILE, LOCK_EX); seek(FILE, 0, 0); truncate(FILE, 0) || die; print(FILE @procs); flock(FILE, LOCK_UN); close FILE; } else { die; } } my $done = 0; my $lock = add_to_queue(); eval { open LOCK, "./lockfile" || die; while (!$done) { flock(LOCK, LOCK_EX); last if check_queue($lock); flock(LOCK, LOCK_UN); usleep 50000; } print STDERR "Doing work on $lock ", scalar localtime, " \n"; # Do your work here sleep 3; print STDERR "Done working on $lock ", scalar localtime, " \n"; remove_from_queue($lock); flock(LOCK, LOCK_UN); }; if ($@) { remove_from_queue($lock); }
-- gam3
A picture is worth a thousand words, but takes 200K.

In reply to Re: Locking/unlocking program execution using a queue by gam3
in thread Locking/unlocking program execution using a queue by RazorbladeBidet

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.