#!/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); } } #### 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!