NFS で排他制御

NFS 上のファイルに対する flock() は働かない。man 2 flock にも次のように書かれている。

flock() does not lock files over NFS. Use fcntl(2) instead: that does work over NFS, given a sufficiently recent version of Linux and a server which supports locking.

これは常識…と思っていたのだが、どうやら Linux 2.6.12 以降はちゃんと働くらしい。

The NFS client in 2.6.12 provides support for flock()/BSD locks on NFS files by emulating the BSD-style locks in terms of POSIX byte range locks. Other NFS clients that use the same emulation mechanism, or that use fcntl()/POSIX locks, will then see the same locks that the Linux NFS client sees.

http://nfs.sourceforge.net/#faq_d10

試してみたらちゃんと排他制御できた。

こんな風にも書かれてる。

It's worth noting that until early 2.6 kernels, O_EXCL creates were not atomic on Linux NFS clients. Don't use O_EXCL creates and expect atomic behavior among multiple NFS client unless you are running a kernel newer than 2.6.5.

2.6.5 以降であれば creat(2) の O_EXCL もちゃんと働くらしい。こっちは未検証。