#!/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 = ; 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 $count times!\n";