Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid problematic/undefined behavior when locking NFS files on Linux?

I have used flock() and fcntl() in the past, but I've always been concerned that behavior is undefined or problematic for some older versions of Linux.

I need a solution that is compatible with older Linux-es (say, 2.6.18 or better), and NFS 3+.

Will flock() and/or fcntl() work consistently under those circumstances, or do I need to resort to open (.... O_EXCL) to guarantee atomicity?

like image 445
Dave Wade-Stein Avatar asked Jun 27 '26 15:06

Dave Wade-Stein


1 Answers

You definitely cannot expect flock() to work with NFS. fcntl() with F_SETLK has a decent chance of working, with caveats if you have multiple uses in one process: http://0pointer.de/blog/projects/locking.html

like image 72
John Zwinck Avatar answered Jun 30 '26 07:06

John Zwinck



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!