Gossamer Forum
Home : General : Perl Programming :

lock file problem

Quote Reply
lock file problem
I use the following method to lock my counter file, but the counter file (count.txt) always reset automatically, anybody see any problem on the script below? or anybody have a better solution to do that?

Code:
#! /usr/bin/perl
#############################################
if ($ENV{'QUERY_STRING'} ne '') {
$options = $ENV{'QUERY_STRING'};
}
@terms = split(/&/, $options);
$adcode = @terms[0];
$randnum = @terms[1];

$logfile = "/home/xxx/www/ad/count.txt";

#lock the file

$i = 0 ;

while (-e "$logfile.lock") {
# The file exists, time to take a nap
sleep(1);
if (++$i>60) { die $!; }

}

open (LOCK,">$logfile.lock");
close (LOCK);

open (ADFILE, "$logfile") | | die $!;
$AdNumber = <ADFILE>;
close(ADFILE);

$AdNumber = $AdNumber + 1;

# Rewrite the counter file
open (ADFILE, ">$logfile") &#0124; &#0124; die $!;
print ADFILE $AdNumber;
close(ADFILE);

# unlock the lock file
unlink("$logfile.lock");

Thanks in advance!!!

Best regards,
Tan Kian Khoon