flock - apply or remove an advisory lock on an open file
int flock(int fd, int operation)
Apply or remove an advisory lock on an open file. The
file is specified by fd. Valid operations are given
LOCK_SH Shared lock. More than one process may
hold a shared lock for a given file at a
LOCK_EX Exclusive lock. Only one process may
hold an exclusive lock for a given file
at a given time.
LOCK_NB Don't block when locking. May be speci
fied (by or'ing) along with one of the
A single file may not simultaneously have both shared and
A file is locked (i.e., the inode), not the file descrip
tor. So, dup(2) and fork(2) do not create multiple
instances of a lock.
On success, zero is returned. On error, -1 is returned,
and errno is set appropriately.
The file is locked and the LOCK_NB flag was
4.4BSD (the flock(2) call first appeared in 4.2BSD).
flock(2) 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 lock
respect to forked processes and dup(2).
open(2), close(2), dup(2), execve(2), fcntl(2), fork(2),
lockf(3). There are also locks.txt and mandatory.txt in