FLOCK(2) System Calls Manual FLOCK(2)
flock - apply or remove an advisory lock on an open file
#define LOCK_SH 1 /* shared lock */
#define LOCK_EX 2 /* exclusive lock */
#define LOCK_NB 4 /* don't block when locking */
#define LOCK_UN 8 /* unlock */
int flock(fd, operation)
int fd, operation;
flock() applies or removes an advisory lock on the file associated with
the file descriptor fd. A lock is applied by specifying an operation
parameter that is the inclusive OR of LOCK_SH or LOCK_EX and, possibly,
LOCK_NB. To unlock an existing lock, the operation should be LOCK_UN.
Advisory locks allow cooperating processes to perform consistent opera-
tions on files, but do not guarantee exclusive access (that is, pro-
cesses may still access files without using advisory locks, possibly
resulting in inconsistencies).
The locking mechanism allows two types of locks: shared locks and
exclusive locks. More than one process may hold a shared lock for a
file at any given time, but multiple exclusive, or both shared and
exclusive, locks may not exist simultaneously on a file.
A shared lock may be upgraded to an exclusive lock, and vice versa,
simply by specifying the appropriate lock type; the previous lock will
be released and the new lock applied (possibly after other processes
have gained and released the lock).
Requesting a lock on an object that is already locked normally causes
the caller to block until the lock may be acquired. If LOCK_NB is
included in operation, then this will not happen; instead the call will
fail and the error EWOULDBLOCK will be returned.
Locks are on files, not file descriptors. That is, file descriptors
duplicated through dup(2V) or fork(2V)) do not result in multiple
instances of a lock, but rather multiple references to a single lock.
If a process holding a lock on a file forks and the child explicitly
unlocks the file, the parent will lose its lock.
Processes blocked awaiting a lock may be awakened by signals.
0 on success.
-1 on failure and sets errno to indicate the error.
EBADF The argument fd is an invalid descriptor.
EOPNOTSUPP The argument fd refers to an object other than a
EWOULDBLOCK The file is locked and the LOCK_NB option was spec-
close(2V), dup(2V), execve(2V), fcntl(2V), fork(2V), open(2V),
Locks obtained through the flock() mechanism are known only within the
system on which they were placed. Thus, multiple clients may success-
fully acquire exclusive locks on the same remote file. If this behav-
ior is not explicitly desired, the fcntl(2V) or lockf(3) system calls
should be used instead; these make use of the services of the network
lock manager (see lockd(8C)).
21 January 1990 FLOCK(2)