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
How to Authenticate Username/Password Using Pam W/O Root Privileges
How to Deploy Files for a Remote Debug Launch in Eclipse Cdt
Linux Stand Alone Executable Generation
How to Avoid Having Version Numbers in .So File Name
Source Line Numbers in Perf Call Graph
Does There Exist Kernel Stack for Each Process
How to Enable The Docker Remote API on Windows
Output Data Register Value in Nasm
User-Space Memory Editing Programs
Linux Patch Ignoring Line Numbers
Thread Quantum: How to Compute It
How to Launch Linux Subshell for Ssh_Askpass to Work in Linux
Differences Between Sponge and Tee
How to Get Groupname When I Have The Groupid