I think that a dotlock would server you better than flock here.
... my $dotlockfile = $lockfile . '.lock'; my $lfh; # create the .lock with O_EXCL (Fail if the file already exists) while (!sysopen($lfh, $dotlockfile, O_RDWR|O_CREAT|O_EXCL)) { # some timeout checking code to get out this loop ... # wait for the existing .lock to go away sleep(1); } die "Can't create $dotlockfile" unless $lfh; close($lfh); # we got our own .lock, now continue with your lock code # no need to flock here my $number_of_clients = 0; sysopen(LOCKFILE,$lockfile,O_RDWR|O_CREAT) # create if necessary or die "Can not open/create $lockfile ($!)"; eval { $number_of_clients=<LOCKFILE>||0; chomp($number_of_clients); seek(LOCKFILE,0,0) or die "Rewind error on $lockfile ($!)"; truncate(LOCKFILE,0) or die "Truncate error on $lockfile ($!)" +; if($operation='checkin') { ++$number_of_clients; ... } else { ... --$number_of_clients; } print LOCKFILE $number_of_clients,"\n"; } if($@) { warn "Exception: $@"; } close(LOCKFILE) or warn "Error closing $lockfile ($!)"; # delete if necessary (our .lock will keep others at bay) if ($number_of_clients <= 0) { unlink($lockfile) or die "Unlink error on $lockfile ($!)"; } # now release our .lock unlink($dotlockfile) or die "Unlink error on $dotlockfile ($!)";
Note: untested (I hate testing locking code)

In reply to Re: File Locking plus delete Lockfile question by ruzam
in thread File Locking plus delete Lockfile question by rovf

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.