Node:File Locks, Next:Interrupt Input, Previous:File Status Flags, Up:Low-Level I/O
The remaining fcntl
commands are used to support record
locking, which permits multiple cooperating programs to prevent each
other from simultaneously accessing parts of a file in error-prone
ways.
An exclusive or write lock gives a process exclusive access
for writing to the specified part of the file. While a write lock is in
place, no other process can lock that part of the file.
A shared or read lock prohibits any other process from
requesting a write lock on the specified part of the file. However,
other processes can request read locks.
The read
and write
functions do not actually check to see
whether there are any locks in place. If you want to implement a
locking protocol for a file shared by multiple processes, your application
must do explicit fcntl
calls to request and clear locks at the
appropriate points.
Locks are associated with processes. A process can only have one kind
of lock set for each byte of a given file. When any file descriptor for
that file is closed by the process, all of the locks that process holds
on that file are released, even if the locks were made using other
descriptors that remain open. Likewise, locks are released when a
process exits, and are not inherited by child processes created using
fork
(see Creating a Process).
When making a lock, use a struct flock
to specify what kind of
lock and where. This data type and the associated macros for the
fcntl
function are declared in the header file fcntl.h
.
struct flock | Data Type |
This structure is used with the fcntl function to describe a file
lock. It has these members:
|
int F_GETLK | Macro |
This macro is used as the command argument to fcntl , to
specify that it should get information about a lock. This command
requires a third argument of type struct flock * to be passed
to fcntl , so that the form of the call is:
fcntl (filedes, F_GETLK, lockp)If there is a lock already in place that would block the lock described by the lockp argument, information about that lock overwrites *lockp . Existing locks are not reported if they are
compatible with making a new lock as specified. Thus, you should
specify a lock type of F_WRLCK if you want to find out about both
read and write locks, or F_RDLCK if you want to find out about
write locks only.
There might be more than one lock affecting the region specified by the
lockp argument, but fcntl only returns information about
one of them. The l_whence member of the lockp structure is
set to SEEK_SET and the l_start and l_len fields
set to identify the locked region.
If no lock applies, the only change to the lockp structure is to
update the l_type to a value of F_UNLCK .
The normal return value from fcntl with this command is an
unspecified value other than -1, which is reserved to indicate an
error. The following errno error conditions are defined for
this command:
|
int F_SETLK | Macro |
This macro is used as the command argument to fcntl , to
specify that it should set or clear a lock. This command requires a
third argument of type struct flock * to be passed to
fcntl , so that the form of the call is:
fcntl (filedes, F_SETLK, lockp)If the process already has a lock on any part of the region, the old lock on that part is replaced with the new lock. You can remove a lock by specifying a lock type of F_UNLCK .
If the lock cannot be set, fcntl returns immediately with a value
of -1. This function does not block waiting for other processes
to release locks. If fcntl succeeds, it return a value other
than -1.
The following errno error conditions are defined for this
function:
|
int F_SETLKW | Macro |
This macro is used as the command argument to fcntl , to
specify that it should set or clear a lock. It is just like the
F_SETLK command, but causes the process to block (or wait)
until the request can be specified.
This command requires a third argument of type struct flock * , as
for the F_SETLK command.
The fcntl return values and errors are the same as for the
F_SETLK command, but these additional errno error conditions
are defined for this command:
|
l_type
member of the flock
structure. The values are integer constants.
F_RDLCK
F_WRLCK
F_UNLCK