my $start = time; my $flock = flock (FH,LOCK_EX|LOCK_NB); while (!$flock && time-$start < 3) { sleep 1; $flock = flock (FH,LOCK_EX|LOCK_NB); }