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
There might be more than one lock affecting the region specified by the
lockp argument, but If no lock applies, the only change to the lockp structure is to
update the The normal return value from
|
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 If the lock cannot be set, The following
|
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 The
|
The following macros are defined for use as values for the l_type
member of the flock
structure. The values are integer constants.
F_RDLCK
F_WRLCK
F_UNLCK
As an example of a situation where file locking is useful, consider a program that can be run simultaneously by several different users, that logs status information to a common file. One example of such a program might be a game that uses a file to keep track of high scores. Another example might be a program that records usage or accounting information for billing purposes.
Having multiple copies of the program simultaneously writing to the file could cause the contents of the file to become mixed up. But you can prevent this kind of problem by setting a write lock on the file before actually writing to the file.
If the program also needs to read the file and wants to make sure that the contents of the file are in a consistent state, then it can also use a read lock. While the read lock is set, no other process can lock that part of the file for writing.
Remember that file locks are only a voluntary protocol for controlling access to a file. There is still potential for access to the file by programs that don't use the lock protocol.