D
\Dandy\ Randy
Hey, after going through several responses to my previous enquires about
file locking, I have opted to go with the following. Please have a look and
advise me if i'm on the right track. Basically the script is sort of a hit
counter, but with a re-direct. The main critisism of my previous scripts was
having the read and write file open's non contained within a single lock
sequence. So ... at least in my mind ... the following script groups the
read and write within a single lock.
CODE WITH COMMENTS
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
# opens a simple a dummy file that contains no data
open (LOCKIT, ">data/lockfile.txt") or die "Can't open: $!";
# starts main lock
flock (LOCKIT, LOCK_EX) or die "Can't lock: $!";
# open the real data file for READ
open (DATA, "<data/data.txt") or die "Can't open file: $!";
# starts data READ lock
flock (DATA, LOCK_EX) or die "Can't lock: $!";
# get data contents
$data=<DATA>;
# closes data READ lock and closes data file
close(DATA);
# advance data
$data = $data + 1;
# open the real data file for WRITE
open (DATA, ">data/data.txt") or die "Can't open file: $!";
# starts data WRITE lock
flock (DATA, LOCK_EX) or die "Can't lock: $!";
# writes the advanced data to file
print DATA $data;
# closes data WRITE lock and closes data file
close(DATA);
# closes the main lock
close(LOCKIT);
# redirects user to the required page
$webview="http://www.mysite.com";
print "Location: $webview\n\n";
exit;
CODE WITHOUT COMMENTS
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
open (LOCKIT, ">data/lockfile.txt") or die "Can't open: $!";
flock (LOCKIT, LOCK_EX) or die "Can't lock: $!";
open (DATA, "<data/data.txt") or die "Can't open file: $!";
flock (DATA, LOCK_EX) or die "Can't lock: $!";
$data=<DATA>;
close(DATA);
$data = $data + 1;
open (DATA, ">data/data.txt") or die "Can't open file: $!";
flock (DATA, LOCK_EX) or die "Can't lock: $!";
print DATA $data;
close(DATA);
close(LOCKIT);
$webview="http://www.mysite.com";
print "Location: $webview\n\n";
exit;
Now both the read and write open's are controlled by a parent lock ... what
do you think? Using this method will my data remain protected in the event
multipul users happen to run the script at the same time? Thanx for all ur
help.
Randy
file locking, I have opted to go with the following. Please have a look and
advise me if i'm on the right track. Basically the script is sort of a hit
counter, but with a re-direct. The main critisism of my previous scripts was
having the read and write file open's non contained within a single lock
sequence. So ... at least in my mind ... the following script groups the
read and write within a single lock.
CODE WITH COMMENTS
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
# opens a simple a dummy file that contains no data
open (LOCKIT, ">data/lockfile.txt") or die "Can't open: $!";
# starts main lock
flock (LOCKIT, LOCK_EX) or die "Can't lock: $!";
# open the real data file for READ
open (DATA, "<data/data.txt") or die "Can't open file: $!";
# starts data READ lock
flock (DATA, LOCK_EX) or die "Can't lock: $!";
# get data contents
$data=<DATA>;
# closes data READ lock and closes data file
close(DATA);
# advance data
$data = $data + 1;
# open the real data file for WRITE
open (DATA, ">data/data.txt") or die "Can't open file: $!";
# starts data WRITE lock
flock (DATA, LOCK_EX) or die "Can't lock: $!";
# writes the advanced data to file
print DATA $data;
# closes data WRITE lock and closes data file
close(DATA);
# closes the main lock
close(LOCKIT);
# redirects user to the required page
$webview="http://www.mysite.com";
print "Location: $webview\n\n";
exit;
CODE WITHOUT COMMENTS
#!/usr/bin/perl
use Fcntl qwDEFAULT :flock);
open (LOCKIT, ">data/lockfile.txt") or die "Can't open: $!";
flock (LOCKIT, LOCK_EX) or die "Can't lock: $!";
open (DATA, "<data/data.txt") or die "Can't open file: $!";
flock (DATA, LOCK_EX) or die "Can't lock: $!";
$data=<DATA>;
close(DATA);
$data = $data + 1;
open (DATA, ">data/data.txt") or die "Can't open file: $!";
flock (DATA, LOCK_EX) or die "Can't lock: $!";
print DATA $data;
close(DATA);
close(LOCKIT);
$webview="http://www.mysite.com";
print "Location: $webview\n\n";
exit;
Now both the read and write open's are controlled by a parent lock ... what
do you think? Using this method will my data remain protected in the event
multipul users happen to run the script at the same time? Thanx for all ur
help.
Randy