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

I've used flock () and fcntl () in the past, but I've always worried about undefined behavior or problematic for some older Linux versions.

I need a solution that is compatible with older Linux servers (say 2.6.18 or higher) and NFS 3+.

Will flock () and / or fcntl () work sequentially under these circumstances, or do I need to resort to open (.... O_EXCL) to ensure atomicity?

+3


source to share


2 answers


You definitely cannot expect flock()

to work with NFS. fcntl()

c F_SETLK

has a pretty good job, with caveats if you have multiple uses in the same process: http://0pointer.de/blog/projects/locking.html



+1


source


Historically, it has flock

been available for at least a decade and has been implemented in the 2.0 kernel. From flock man page

:

Since the 2.0 kernel, flock () is implemented as a system call in its own right, and not emulated in the GNU C library as a call to Fcntl (2). This gives true BSD semantics: there is no interoperability between the lock types placed by flock () and fcntl (2), and flock () does not detect a deadlock.



I think it will cover your needs unless you are dealing with pre-2.0 kernels.

0


source







All Articles