Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi there, I'm very new to the site, and I'm a little overwhelmed :-) I was looking around and see that the discussions are quite active, so I'm hoping someone could help .. please:-) I've been coding perl for sometime now, but there has always been one thing that gets my goat .. and that is flocking.

When I first began I used

flock(file,2);

now I use:

use Fcntl ':flock'; flock(file,LOCK_EX); seek file, 0, 0;
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.

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:

#!/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;
... 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?

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

    Yes, you should use KM's method, the reasons he gives in his comment to the File Locking Tutorial are all valid.

    flock-ing a separate file lets you lock several files at once, including files that cannot be locked as you don't have a file handle on them and is generally a cleaner design than locking the file you are using.

      Thank you SO much mirod for the quick reply :-)

      After literally searching everywhere and reading everything I could get my hands on, I finally get it!! :-) Of course thanks to you, perlmonks.org, and KM for the reply to the tutorial.

      Have a wonderful week ... Thank you again

        Sorry to bother anyone again, but please help, this method is still wiping my file clean!? I have done it exactly as the example and it doesn't seem to work.

        It would mean a great deal to me if you could help.

        Thank you so much.

        Here is a small example -- thank you again :)

        #!/usr/local/bin/perl -wT # Flocking test print "Content-type: text/html\n\n"; use Fcntl qw(:flock); my $file = 'test_lock.txt'; my $SEMAPHORE = $file . '.lck'; unless (-e "$file") { open(FILE,">$file") or die "Reason: $!"; print FILE "1"; close(FILE); } open(SEM, ">$SEMAPHORE") or die "$SEMAPHORE: $!"; flock(SEM, LOCK_SH) or die "flock() failed for $SEMAPHORE: $!"; open (FILE, "$file") or die "Can't open $file: $!"; $count = <FILE>; close FILE; sleep 5; close SEM; $count++; 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 FH $count; close FH; sleep 5; close S; print "You have been here <B>$count</B> times!\n";