Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
When I first began I used
flock(file,2);
now I use:
Plucked pretty much from the perl man pages. I really try to read everything I can before asking but this one has got me stumped. Just when I figured I got it down pat, it seems I am running into to problems.use Fcntl ':flock'; flock(file,LOCK_EX); seek file, 0, 0;
creating a simple read and write counter to demonstrate the flocking abilities, I see that every so often, the file is cleared. On every instance I read & write to the file I use the second method above, except for reading I used a shared_lock (LOCK_SH) When I change the shared lock to exclusive, it seems to run better, but then it happens again.
Searching around I came apon this page: From the Monastery and the last reply added by user KM has got me thinking. It seems reasonable, but I have to say I'm confused.
This method that the user speaks of:
... does this lock the file being written to, because it is hanging when the other file is locked arg :( Should I use this method? Is it more secure?#!/usr/bin/perl -wT use Fcntl qw(:flock); my $file = 'test_lock.txt'; my $SEMAPHORE = $file . '.lck'; open(S, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(S, LOCK_EX) or die "flock() failed for $SEMAPHORE: $!"; open (FH, ">>$file") or die "Can't open $file: $!"; print "About to write\n"; print FH "I have written ($$)\n"; print "Written\n"; close FH; print "Going to sleep...\n"; sleep 10; print "Woken up...\n"; close S;
Please help, I'm still learning everday, and this is one question I would like to resolve and get a good nights sleep :-)
Thank you sincerly for any replies.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Flocking .. insanity is close.
by mirod (Canon) on Mar 12, 2001 at 12:16 UTC | |
by Anonymous Monk on Mar 12, 2001 at 12:47 UTC | |
by Anonymous Monk on Mar 13, 2001 at 03:32 UTC |