use Fcntl qw [:flock
:DEFAULT];
my $lock_file = "...";
sysopen my $fh => $lock_file, O_RDWR | O_CREAT
or die "sysopen: $!";
exit unless flock $fh => LOCK_EX | LOCK_NB;
... Code ...
END {! -f $lock_file || unlink $lock_file || warn "unlink: $!"}
It turned out to be incorrect. Can you spot the mistake?
Easy (Score:1)
Operator precedence.
Re:Easy (Score:1)
Re:Easy (Score:1)
Then I guess I just found a bonus bug for you.
Admittedly, it’s not a fatal issue.
flock(2) (Score:1)
whoops.
Re:flock(2) (Score:1)
No. Second sentence in
perldoc -f flock:Re:flock(2) (Score:1)
Ah, got it. (Score:1)
A third process will erroneously be allowed to launch after the first of two concurrently launched processes has already exited, despite the second is still hanging around.
But how did you fix it? I can’t see a correct solution other than just not cleaning up the lockfile.
Re:Ah, got it. (Score:1)
It's easily fixed - set a variable right after getting the lock, and don't delete the file unless the variable is set.
END{} (Score:1)
END {! -f $lock_file || unlink $lock_file || warn "unlink: $!"}
or in other words...
"Let's delete the lock file if it exists when we're ENDing, even if we're doing so because we've detected a previous instance of the program"
Lock DATA? (Score:1)
Re:Lock DATA? (Score:2)
But more to the point, who would consider using flock() over NFS? That's scary bad stuff. You're now relying on correct implementation of lockd everywhere, which is not guaranteed. Better to use some form of lockfile, the way that the MTAs & procmail do...
-Dom
Re:Lock DATA? (Score:2)