Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
Running this, waiting for the "DONE FORKING" message and than typing "touch tmp/random.tmp" produces something like this:#!/usr/bin/perl use strict; use warnings; use Time::HiRes qw|time sleep|; use Fcntl qw|:flock|; mkdir "tmp" or warn $!; chdir "tmp" or die $!; for (1 .. 500){ my $pid = fork(); defined($pid) or die "fork failed: $!"; if ($pid == 0) { child_process(); exit; } } print "DONE FORKING\n"; while (wait() > -1) {} sub child_process{ while (1){ while (my $file = glob "*.tmp"){ open my $FH, '>', $file or next; #next if unlinked already if (flock($FH, LOCK_EX | LOCK_NB)){ next unless flock($FH, LOCK_EX | LOCK_NB); print time(), " YES! I ($$) got a lock on $file!\n"; sleep(.1); unlink $file; } close $FH; } sleep(.1); } }
I'm under the impression that there should only be one process that gets an exclusive lock.1199321503.71728 YES! I (25930) got a lock on random.tmp! 1199321504.01631 YES! I (25827) got a lock on random.tmp! 1199321504.01702 YES! I (25976) got a lock on random.tmp!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: flock LOCK_EX not locking exclusively
by pc88mxer (Vicar) on Jan 03, 2008 at 06:07 UTC | |
by Anonymous Monk on Jan 03, 2008 at 09:30 UTC | |
by pc88mxer (Vicar) on Jan 03, 2008 at 15:51 UTC | |
|
Re: flock LOCK_EX not locking exclusively
by Anonymous Monk on Jan 03, 2008 at 00:57 UTC | |
by Anonymous Monk on Jan 03, 2008 at 01:12 UTC | |
|
Re: flock LOCK_EX not locking exclusively
by Anonymous Monk on Jan 03, 2008 at 02:13 UTC | |
by tye (Sage) on Jan 03, 2008 at 06:47 UTC |