File Locking Follow-up

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 qw:)DEFAULT :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 qw:)DEFAULT :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
 
C

ctcgag

\"Dandy\" Randy said:
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 qw:)DEFAULT :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: $!";

I think this will work, but I can't guarantee that on some system
it won't. I'd worry that some system may decide that, since you
are clobbering the file anyway, that it "clobbers" the old lock
along with it, so happily grants a new one. This isn't the case
on my system (but it would be if someone happened to delete
lockfile.txt while it it was locked).
# 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: $!";

Assuming that all programs that wish to operate on data.txt first
obtain the lock on lockfile.txt, then I can see no need to also
lock data.txt at all. Of course, it doesn't hurt, it just seems
unnecessary. Also, this obtains an exclusive lock on a file open
for reading only, which was the objection that someone raised to my
suggestion that you use the file as it's own semaphore, and that objection
seems equally valid here.
# 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: $!";

Again, unless other programs lock the file itself rather than the
semaphore, there is no reason for this program to lock both files.

# 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;

Xho
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top