Can Linux Flock(Fd, Lock_Ex|Lock_Nb) Fail Spuriously

Can Linux flock(fd, LOCK_EX|LOCK_NB) fail spuriously?

Reading man flock(2):

EWOULDBLOCK
The file is locked and the LOCK_NB flag was selected.

So getting EWOULDBLOCK means the file is already locked. If it is guaranteed that your two processes are the only ones involved, they will never get EWOULDBLOCK on the same file at the same time.

Please note that threads is a different story. Threads normally share file descriptors, so several threads within the same process can call flock() successfully on the same file.

Perl: flock() works on Linux, ignores previous lock on AIX

This isn't anything to do with AIX, the open() call in your script is incorrect.

Should should be something like:

open (my $lockfile, ">>", $fileDesc) # for LOCK_EX, must be write

You were using the "dup() previously opened file handle" syntax with >&=, but the script had not opened any files to duplicate, nor should it.

My quick tests shows the correct behavior (debugging added)

first window:
$ ./flock.pl -n -x lockfile
opened lockfile
locked
second window:
$./flock.pl -n -x lockfile
opened lockfile
Can't change lock - Resource temporarily unavailable
$


Related Topics



Leave a reply



Submit