Locking a file for race conditons perl

183 views Asked by At

I am working on a script for no good reason spawns two extra processes when it run. To combat this i wrote i file lock routine. But because these extra processes are running at the same time this does not seem to work at all. what am i missing here.

my $lockfile = '/tmp/lockfile';
my $fhpid;
sub BailOut {
    print "$0 is already running. Exiting.\n";
    print "(File '$lockfile' is locked).\n";
    exit(1);
}
open($fhpid, '>', $lockfile) or die "error: open '$lockfile': $!";
flock($fhpid, LOCK_EX|LOCK_NB) or BailOut();
0

There are 0 answers