flock() applies or removes an advisory lock on the file associated with the file descriptor fd. The compatibility version of flock() has been implemented on top of fcntl(2) locking.
It does not provide complete binary compatibility.
Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee exclusive access (that is, 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. 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 lock is applied by specifying an operation parameter LOCK_SH for a shared lock or LOCK_EX
for an exclusive lock. The operation paramerer may be ORed with LOCK_NB to make the operation non-blocking. To unlock an existing lock,
the operation should be LOCK_UN.
Read permission is required on a file to obtain a shared lock, and write permission is required to obtain an exclusive lock. Locking a segment that is already locked by the calling process causes
the old lock type to be removed and the new lock type to take effect.
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.
|