flock(2)flock(2)NAMEflock() - apply or remove an advisory or enforced lock on an open file
filedes Specifies a file descriptor returned by a successful or
function, identifying the file on which the locking opera‐
tion is to be performed.
operation Specifies one of the following constants for defined in the
Apply a shared lock.
Apply an exclusive lock.
Remove a lock.
In addition to the preceding constants, the following con‐
stant can be bitwise ORed with either or
LOCK_NB Do not block when locking.
The function applies or removes an advisory or enforced lock on the
whole file associated with the filedes file descriptor. Advisory locks
allow cooperating processes to perform consistent operations on files,
but do not guarantee consistency (that is, processes may still access
files without using advisory locks, possibly resulting in inconsisten‐
cies).Enforced locks are in effect if the enforcement bit in the file
mode is set (see chmod(2)). This might affect and other system calls
performing file operations.
You can use the function to coordinate a file's lock status on local
and NFS file systems.
The locking mechanism allows two types of locks: and At any time multi‐
ple shared locks may be applied to a file, but at no time are multiple
exclusive locks or both shared and exclusive locks allowed simultane‐
ously 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).
Requesting a lock on an object that is already locked normally causes
the caller to be blocked until the lock may be acquired. If is
included in operation, this will not happen; instead, the call will
fail and will be set to
Locks are on files, not file descriptors. This means that:
· Locks are not inherited by a child process resulting from a call.
· All locks associated with a file for a given process are removed
when any thread in the process closes any file descriptor for that
Processes that are blocked awaiting a lock may be awakened by signals.
An application needs to check the return value to make sure that the
lock is really acquired.
The interface is not part of any UNIX standard. Therefore, if you are
designing and writing applications to be portable across platforms, you
should use the file locking interface instead of
The advisory file locking capabilities of are implemented throughout
the network by the network lock daemon If the file server crashes and
is rebooted, the lock daemon attempts to recover all locks associated
with the crashed server. If a lock cannot be reclaimed, the process
that held the lock is issued a signal. See lockd(1M).
File locking as implemented for NFS files is only advisory.
RETURN VALUESflock() returns:
for successful completion
for unsuccessful completion;
is set to indicate the error
If the function fails, may be set to one of the following values:
The filedes parameter is not a valid open file descriptor.
A shared lock was requested and filedes was not opened for read‐
ing, or an exclusive lock was requested and filedes was not
opened for writing.
The lock is blocked by some lock from another process.
Putting the calling process to sleep while waiting for that lock
to become free would cause a deadlock.
A signal interrupted the
The filedes parameter refers to a file that does not support lock‐
The operation parameter is not valid.
The lock table is full.
Too many files are already locked.
The filedes parameter refers to an NFS file with the access bits
set for enforcement mode.
The filedes parameter refers to an NFS file, and a system error
occurred on the remote node.
The file is locked and the
option was specified.
The file is mapped into virtual memory via the system call (see
SEE ALSOlockd(1M)chmod(2), close(2), creat(2), dup(2), dup2(2), exec(2),
fcntl(2), fork(2), lockf(2), open(2), read(2), truncate(2), write(2).