If I run the following script in parallel I could see few are waiting for acquiring lock but some are running in parallel. LOCK_EX is not working as expected. I don't understand what is missing here.
$|++;
my $lockHandle;
my $file='lock.txt';
#sysopen( $lockHandle, $file, O_RDWR|O_CREAT);
open($lockHandle, '>>', $file);
print "aquiring lock\n";
my $o = flock $lockHandle,LOCK_EX or die "Could not lock '$file' - $!";
print "Locked....$o\n";
## Only executes when the lock is acquired
my $count=5;
my $intex=0;
while ($index <= $count){
print "executing\n";
sleep 1;
$index=$index+1;
}
END {
flock $lockHandle, LOCK_UN;
close $lockHandle;
unlink $file;
}