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