sleep(3) while -e "file.lock"; open(LCK, "> file.lock");
This is a classic race condition: you take two steps to do something which must be done in one. That's why computer hardware provides an atomic test-and-set instruction. In theory, this ``ought'' to work:
sysopen(FH, "file.lock", O_WRONLY|O_EXCL|O_CREAT, 0644) or die "can't open file.lock: $!":
except that lamentably, file creation (and deletion) is not atomic over
NFS, so this won't work (at least, not every time)
over the net. Various schemes involving involving link
have
been suggested, but these tend to involve busy-wait, which is also
subdesirable.