flock

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

flock - オープンされたファイルに対するアドバイザリロックの適用、解除を行う  

ライブラリ

標準 C ライブラリ (libc,~-lc)  

書式

#include <sys/file.h>

int flock(int fd, int op);
 

説明

Apply or remove an advisory lock on the open file specified by fd. The argument op is one of the following:
LOCK_SH
共有ロックを適用する。 指定したファイルに対して、 一つ以上のプロセスが同時に共有ロックを保持することができる。
LOCK_EX
排他ロックを適用する。 指定したファイルに対して、 ただ一つのプロセスだけが同時に排他ロックを保持することができる。
LOCK_UN
このプロセスが保持している既存のロックを解除する。

flock() を呼び出したときに、指定したロック種別と異なるロックが別プロセスによって 保持されていると、 flock() は停止 (block) されることがある。 非停止 (nonblocking) タイプの要求を行うためには、 上記の操作 (operation) に LOCK_NB を論理和の形で指定する。

一つのファイルに共有ロックと排他ロックを同時に設定することはできない。

flock() によって作られるロックは、 オープンファイル記述 (open file description) (open(2) 参照) と関連付けられる。 したがって、ファイルディスクリプターの複製 (fork(2) や dup(2) などにより作成される) は同じロックを参照し、 これらのファイルディスクリプターのどれを使っても このロックを変更したり解放したりできる。 また、ロックの解放は、 上記の複数のファイルディスクリプターのいずれかに対して 明示的に LOCK_UN 操作を指示した場合か、これらのファイルディスクリプターがすべて 閉じられた場合に行われる。

あるプロセスが open(2) (もしくは同様の方法) を使って同じファイルに対して 複数のファイルディスクリプターを取得した場合、 flock() はこれら複数のファイルディスクリプターを各々独立のものとして扱う。 これらのファイルディスクリプターの一つを使ってファイルをロックしようと した際、そのロック要求は、呼び出し元のプロセスがそのファイルの別のファイルディスクリプター経由ですでに設定しているロックによって拒否される場合がある。

一つのプロセスは、一つのファイルに対して (共有ロックと排他ロックのうち) いずれか一種類のロックしか設定できない。 既にロックされたファイルに対して flock() を呼び出すと、既存のロックを新しいロックモードに変更することになる。

flock() により作成されたロックは execve(2) の前後で保存される。

共有ロックも排他ロックも、ファイルがどのモードでオープンされたかに 関係なく適用することができる。  

返り値

成功すると、0 を返す。 エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

EBADF
fd がオープンされたファイルディスクリプターではない。
EINTR
ロックの獲得を待っている間に、ハンドラーにより捕捉されたシグナルを 受信し、 flock() が中断された。 signal(7) 参照。
EINVAL
op is invalid.
ENOLCK
ロックレコードを割り当てるためのメモリーが不足している。
EWOULDBLOCK
指定したファイルがロックされており、 LOCK_NB フラグが指定されている。
 

バージョン

Since Linux 2.0, flock() is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl(2). With this implementation, there is no interaction between the types of lock placed by flock() and fcntl(2), and flock() does not detect deadlock. (Note, however, that on some systems, such as the modern BSDs, flock() and fcntl(2) locks do interact with one another.)  

CIFS details

Up to Linux 5.4, flock() is not propagated over SMB. A file with such locks will not appear locked for remote clients.

Since Linux 5.5, flock() locks are emulated with SMB byte-range locks on the entire file. Similarly to NFS, this means that fcntl(2) and flock() locks interact with one another. Another important side-effect is that the locks are not advisory anymore: any IO on a locked file will always fail with EACCES when done from a separate file descriptor. This difference originates from the design of locks in the SMB protocol, which provides mandatory locking semantics.

Remote and mandatory locking semantics may vary with SMB protocol, mount options and server type. See mount.cifs(8) for additional information.  

標準規格

BSD.  

歴史

4.4BSD (flock() コールは 4.2BSD で最初に登場した)。 fcntl(2) で実装されているものなどを含めると、 flock() の機能はほとんどの UNIX システムで実装されている。  

NFS での詳細

Up to Linux 2.6.11, flock() does not lock files over NFS (i.e., the scope of locks was limited to the local system). Instead, one could use fcntl(2) byte-range locking, which does work over NFS, given a sufficiently recent version of Linux and a server which supports locking.

Since Linux 2.6.12, NFS clients support flock() locks by emulating them as fcntl(2) byte-range locks on the entire file. This means that fcntl(2) and flock() locks do interact with one another over NFS. It also means that in order to place an exclusive lock, the file must be opened for writing.

Since Linux 2.6.37, the kernel supports a compatibility mode that allows flock() locks (and also fcntl(2) byte region locks) to be treated as local; see the discussion of the local_lock option in nfs(5).  

注意

flock() アドバイザリロックだけを適用する。したがって、ファイルに適切なアクセス権を 付与していれば、プロセスは flock() の使用に無視して、ファイルへの入出力を行うことができる。

flock() と fcntl(2) は fork されたプロセスと dup(2) で違った動作をする。 flock() を fcntl(2) を使って実装しているシステムでは、 flock() の動作はこのマニュアルページに記載されているものとは違うだろう。

ロックの変換 (共有ロックから排他ロックへ、もしくはその反対) がアトミックに 行われることは保証されていない: 既存のロックがまず削除され、それから新しい ロックが設定される。この 2つのステップの間に、他のプロセスからの処理待ちの ロック要求が認められるかもしれず、結果として変換は停止 (block) したり、 (LOCK_NB が指定された場合には) 失敗したりする。 (これは元々の BSD の動作であり、多くの他の実装でも起こる。)

Release of a lock when a file descriptor is closed is not sequenced after all observable effects of close(2). For example, if one process writes a file while holding an exclusive lock, then closes that file, and another process blocks placing a shared lock on that file to wait until it is closed, it may observe that a subsequent execve(2) of that file fails with ETXTBSY, and umount(2) of its underlying filesystem fails with EBUSY, as if the file were still open in the first process.  

関連項目


 flock(1), close(2), dup(2), execve(2), fcntl(2), fork(2), open(2), lockf(3), lslocks(8)

Documentation/filesystems/locks.rst in the Linux kernel source tree


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
バージョン
CIFS details
標準規格
歴史
NFS での詳細
注意
関連項目

This document was created by man2html, using the manual pages.