I have a file on an NFS mount which is locked and unlocked for every operation done on it.
Initially I used flock(filehandle, LOCK_EX|LOCK_NB) but that attempt failed with I/O Error.
On browsing through multiple forums I found a solution to use File::NFSLock to lock/unlock files on a NFS share. However, NFSLock succeeds on the first attempt to lock and unlock the file, however when 2 processes simultaneously tries to access the locks, it fails with EACESS error.
Here is how the code is:
Process 1acquired lock with the below code and also released the lock once done:
if ($lock = new File::NFSLock {
file => $handle,
lock_type => LOCK_EX|LOCK_NB,
}) {
- Now 2 processes were spawned with a
forkcommand (Process 2andProcess 3). WhenProcess 2tried to lock the same file using same above code in Step 1, file locking failed with errorPermission Denied. After 15 seconds whenProcess 3tried to acquire lock it also failed withPermission Deniederror.
When I executedfuser -von the file, I could see 2 process IDs (Process 2andProcess 3) which had opened the file for writing.
Few documents on internet suggest that The lock cannot be set because it is blocked by an existing lock on the file. Some systems use EAGAIN in this case, and other systems use EACCES however doesn't seem to be the case in my scenario as Process 1 had released the lock.
Also please note that all the 3 processes were spawned from the same script, so there shouldn't be an issue of different user trying to acquire the lock.
Thank You!