Well, okay. Your requirements have become reasonably clear, although the design choices lead to some doubts here. As usual, there are alternative ways to accomplish the task. I tried to keep this as simple as possible and not reinvent sendmail or somesuch.

#! /usr/bin/perl use strict; use warnings; use File::Temp; use Fcntl qw( :flock ); myturn(); mywork(); exit(); sub diag { warn "$$: @_\n" } sub mywork { diag "WORKING... @ARGV"; sleep 1; } sub myturn { (my $wait, our $lk) = queueup("/tmp/foolock"); diag "waiting on lock"; flock($wait, LOCK_EX) or die; diag "obtained the lock!"; } sub queueup { my $lkfile = shift; my ($prev, $own); # release this once done with our unit work $own = File::Temp->new( UNLINK => 0 ); flock($own, LOCK_EX) or die; diag "create and lock $own"; # protect lockfile ops with another lock... open(my $op, "+>", "$lkfile.lk") or die; flock($op, LOCK_EX) or die; diag "take previous $lkfile"; open($prev, "+>", $lkfile) or die; diag "rename $own to $lkfile"; rename("$own", $lkfile) or die; #flock($op, LOCK_UN) or die; return ($prev, $own); }

As you can see, a chain of per-process locks is used, but just two lockfiles are kept around on /tmp. All in all, I'm somewhat unsure about the robustness of this approach.


In reply to Re: Critical section FCFS by oiskuu
in thread Critical section FCFS by aj7700

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.