R
Randy
Hello,
I seem to be having some difficulties locking a simple counter flatfile
database. To start off, here is the code I am presently using:
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
open (LOCKERFILE, "<$activeuser/locker.txt") or die "Can't open: $!";
flock (LOCKERFILE, LOCK_EX) or die "Can't lock: $!";
open (GETCOUNT, "<$activeuser/counter.txt") or die "Can't open file: $!";
flock (GETCOUNT, LOCK_SH) or die "Can't lock: $!";
$currentcount=<GETCOUNT>;
($date,$count,$users)=split(/\|/,$currentcount);
close(GETCOUNT);
$count = $count + 1;
open (WRITECOUNT, ">$activeuser/counter.txt") or die "Can't open file: $!";
flock (WRITECOUNT, LOCK_SH) or die "Can't lock: $!";
print WRITECOUNT "$date|$count|$users";
close(WRITECOUNT);
close(LOCKERFILE);
exit;
I use the locker.txt file to start a primary lock before the reading and
writing begins, suggested to me by a user from this newsgroup long ago. The
script seems to work most of the time, but every now and then I check the
data and its completely gone ... only leaving the data separators | | | So I
guess it doesnt really work well after all.
I would be interested in knowing if this script is infact flawed in my
approach or if the data loss is being caused by something else. I have
really done lots of reading on locking flatfile databases and there seems to
be alot of opinions, but few hard facts on how to do it correctly.
This script performs a very simple function but is very important to me to
make it work right without losing my data. I would very much value feedback
on this. Many thanks in advance.
Robert
I seem to be having some difficulties locking a simple counter flatfile
database. To start off, here is the code I am presently using:
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
open (LOCKERFILE, "<$activeuser/locker.txt") or die "Can't open: $!";
flock (LOCKERFILE, LOCK_EX) or die "Can't lock: $!";
open (GETCOUNT, "<$activeuser/counter.txt") or die "Can't open file: $!";
flock (GETCOUNT, LOCK_SH) or die "Can't lock: $!";
$currentcount=<GETCOUNT>;
($date,$count,$users)=split(/\|/,$currentcount);
close(GETCOUNT);
$count = $count + 1;
open (WRITECOUNT, ">$activeuser/counter.txt") or die "Can't open file: $!";
flock (WRITECOUNT, LOCK_SH) or die "Can't lock: $!";
print WRITECOUNT "$date|$count|$users";
close(WRITECOUNT);
close(LOCKERFILE);
exit;
I use the locker.txt file to start a primary lock before the reading and
writing begins, suggested to me by a user from this newsgroup long ago. The
script seems to work most of the time, but every now and then I check the
data and its completely gone ... only leaving the data separators | | | So I
guess it doesnt really work well after all.
I would be interested in knowing if this script is infact flawed in my
approach or if the data loss is being caused by something else. I have
really done lots of reading on locking flatfile databases and there seems to
be alot of opinions, but few hard facts on how to do it correctly.
This script performs a very simple function but is very important to me to
make it work right without losing my data. I would very much value feedback
on this. Many thanks in advance.
Robert