Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

What is the best way to lock a counter file?

by Anonymous Monk
on Jun 24, 2000 at 14:42 UTC ( [id://19716]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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;

Replies are listed 'Best First'.
Re: What is the best way to lock a counter file?
by merlyn (Sage) on Jun 25, 2000 at 16:47 UTC
    Or, instead of doing all that, just use File::CounterFile.
    use File::CounterFile; my $number = File::CounterFile->new("/some/place"); my $next = ++$number;
Re:What is the best way to lock a counter file?
by Ovid (Cardinal) on Jun 25, 2000 at 10:37 UTC
    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.

    Cheers!

Re: What is the best way to lock a counter file?
by Skeeve (Parson) on Jun 13, 2003 at 06:07 UTC
    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?";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://19716]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (4)
As of 2024-04-19 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found