Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I have a file which store a counter, whenever the CGI script is accessed, the counter in that file will be incremented. I have used flock() to ensure the file is locked before updating it, but sometimes it get resetted to 0 when the traffic is high. Did I write something wrong in my script? Or is it becuase of the file locking machanism? Is there any work around on this? I am using Redhat Linux 9, Apache 2.0 with perl 5.8.3
Does anyone of you encounter this problem before? Thanks!
The following are the counter code that I am using:
Regards,#!/usr/local/bin/perl use strict; use CGI qw(:all); my $file = 'counter.txt'; my $counter = 0; { local *FH; open(FH, "+<$file") || die $!; flock(FH, 2) || die $!; $counter = <FH>; seek(FH, 0, 0); truncate(FH, 0); print FH $counter+1; close(FH); } print header(); print "counter[$counter]\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Concurrent file access with flock()
by Zaxo (Archbishop) on Jun 17, 2004 at 16:59 UTC | |
by Anonymous Monk on Jun 17, 2004 at 17:54 UTC | |
|
Re: Concurrent file access with flock()
by tilly (Archbishop) on Jun 18, 2004 at 01:30 UTC | |
|
Re: Concurrent file access with flock()
by ambrus (Abbot) on Jun 18, 2004 at 08:14 UTC |