Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

What is the best way to lock a counter file?

by Anonymous Monk
on Jun 24, 2000 at 14:42 UTC ( #19716=categorized question: print w/replies, xml ) Need Help??
Contributed by Anonymous Monk on Jun 24, 2000 at 14:42 UTC
Q&A  > files


I have a banner impression counter.
But when I try to lock the counter file the script doesn't work. What I am doing wrong?
use Fcntl qw(:flock); # import LOCK_* constants if ( -e $file_banner) { open(COUNT,"$file_banner") || die print "Could not open for read"; # flock(COUNT,LOCK_EX) || die print "Could not lock"; $line = <COUNT>; # flock(COUNT,LOCK_UN) || die print "Could not unlock"; close(COUNT); print "a:$line"; $line++; print "b:$line"; open(COUNT,"+< $file_banner") || die print "Could not open for wri +te"; # flock(COUNT,LOCK_EX) || die print "Could not lock"; print COUNT "$line"; # flock(COUNT,LOCK_UN) || die print "Could not unlock"; close(COUNT); } else { open(NEW,"> $file_banner"); print NEW "0"; close(NEW); print "ffff\n\n"; } exit;

Answer: What is the best way to lock a counter file?
contributed by merlyn

Or, instead of doing all that, just use File::CounterFile.

use File::CounterFile; my $number = File::CounterFile->new("/some/place"); my $next = ++$number;
Answer:What is the best way to lock a counter file?
contributed by Ovid

It's difficult to tell what the error is if you don't supply us with the actual error message. If the file is on a network file system, flock is unlikely to work. Further, flock is a polite way of requesting that others leave the file alone. It does not actually stop the file from being used by someone else unless they check to see if it's locked. (That's probably not relevant to your question, but I thought I would mention it).

Here's a somewhat cleaner version of your code (straight from the Perl Cookbook):

use Fcntl qw(:DEFAULT :flock); sysopen(FH, $file_banner, O_RDWR|O_CREAT) or die "Can't open $file_ban +ner: $!\n"; flock(FH, LOCK_EX) or die "Can't write-lock $file_ +banner: $!\n"; $num = <FH> || 0; # do not use 'or' here seek(FH, 0, 0) or die "Can't rewind $file_bann +er: $!\n"; truncate(FH,0) or die "Can't truncate $file_ba +nner: $!\n"; print FH $num+1, "\n" or die "Can't write to $file_ba +nner: $!\n"; close(FH) or die "Can't close $file_banne +r: $!\n";
Note that the above code will create the file for you if it does not exist. Thus, the if/else structure is eliminated.

Hope this helps.


Answer: What is the best way to lock a counter file?
contributed by Skeeve

If I'm not sure whether or not flock will work, I usually use a workaround by renaming the file I want to have access to. Of course this depends on rename being "atomic", meaning: There is no point in time while renaming, where both filenames exists and rename may be halted.

so my code looks something like this:

my $cntr="counterfile"; my $owncntr="counter.mine"; while (! rename $cntr,$owncntr) { # sleep some time to retry # or die if no more retries allowed } # do some work on file $owncntr if (! rename $owncntr,$cntr ) die "Someone created a new $cntr file?";

Please (register and) log in if you wish to add an answer

  • 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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others wandering the Monastery: (10)
    As of 2020-02-26 20:42 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (117 votes). Check out past polls.