FLOCK(2) — System Interface Manual — System Calls
NAME
flock − apply or remove an advisory lock on an open file
SYNOPSIS
#include <sys/file.h>
flock(fd, operation)
int fd;
DESCRIPTION
Flock applies or removes an advisory lock on the file associated with the file descriptor fd. The operation parameter is formed by oring the appropriate values together from:
FSHLOCKapply advisory shared lock
FEXLOCKapply advisory exclusive lock
FUNLOCKremove existing lock
FNBLOCKdo not block
Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee consistency (i.e. processes 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. At any time multiple shared locks may be applied to a file, but at no time are multiple exclusive, or both shared and exclusive, locks allowed simultaneously on a file.
A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate lock type; this results in the previous lock being released and the new lock applied (possibly after other processes have gained and released the lock).
NOTES
Processes blocked awaiting a lock are interruptible. An exclusive locked file descriptor may be duplicated through dup(2) or fork(2) resulting in multiple processes actively holding an exclusive lock.
RETURN VALUE
Zero is returned if the operation was sucessful; on an error a −1 is returned and an error code is left in the global location errno.
ERRORS
The flock call fails if:
[EWOULDBLOCK]
The file is locked and the FNBLOCK option was specified.
[EBADF] The argument fd is an invalid descriptor.
[EINVAL] The argument fd refers to an object other than a file.
SEE ALSO
BUGS
This locking mechanism should be resolved with the lockf mechanism of the /usr/group standard.
Sun System Release 0.3 — 12 February 1983