LOCKING(2) — UNIX Programmer’s Manual
NAME
locking − provide exclusive file regions for reading or writing
SYNOPSIS
locking(fildes, mode, size)
int fildes;
int mode;
int size;
DESCRIPTION
Locking will allow a specified number of bytes to be accessed only by the locking process. Other processes which attempt to lock, read, or write the locked area will sleep until the area becomes unlocked.
Fildes is the word returned from a successful open, creat, dup, or pipe system call.
Mode is zero to unlock the area. Mode is one or two for making the area locked. If the mode is one, and the area has some other lock on it, then the process will sleep until the entire area is available. If the mode is two, and the area is locked, an error will be returned.
Size is the number of contigous bytes to be locked or unlocked. The area to be locked starts at the current offset in the file. If size is zero the area to end of file is locked.
The potential for a deadlock occurs when a process controlling a locked area is put to sleep by accessing another processes locked area. Thus calls to locking, read, or write scan for a deadlock prior to sleeping on a locked area. An error return is made if sleeping on the locked area would cause a deadlock.
Lock requests may, in whole or part, contain or be contained by a previously locked area for the same process. When this or adjacent areas occur, the areas are combined into a single area. If the request requires a new lock element with the lock table full, an error is returned, and the area is not locked.
Unlock requests may, in whole or part, release one or more locked regions controlled by the process. When regions are not fully released, the remaining areas are still locked by the process. Release of the center section of a locked area requires an additional lock element to hold the cut off section. If the lock table is full, an error is returned, and the requested area is not released.
While locks may be applied to special files or pipes, read/write operations will not be blocked. Locks may not be applied to a directory.
SEE ALSO
open(2), creat(2), read(2), write(2), dup(2), close(2)
DIAGNOSTICS
The value −1 is returned if the file does not exist, or if a deadlock using file locks would occur. EACCES will be returned for lock requests in which the area is already locked by another process. EDEADLOCK will be returned by locking, read, or write if a deadlock would occur. EDEADLOCK will also be returned when the locktable overflows.
7th Edition