F_GETLK, F_SETLK and F_SETLKW are used to acquire, release, and test
for the existence of record locks (also known as file-segment or file-
region locks). The third argument, lock, is a pointer to a structure
that has at least the following fields (in unspecified order).
struct flock {
...
short l_type; /* Type of lock: F_RDLCK,
F_WRLCK, F_UNLCK */
short l_whence; /* How to interpret l_start:
SEEK_SET, SEEK_CUR, SEEK_END */
off_t l_start; /* Starting offset for lock */
off_t l_len; /* Number of bytes to lock */
pid_t l_pid; /* PID of process blocking our lock
(F_GETLK only) */
...
};
Specifying 0 for l_len has the special
meaning: lock all bytes starting at the location specified by l_whence
and l_start through to the end of file, no matter how large the file
grows.
Any number of processes may hold a read lock
(shared lock) on a file region, but only one process may hold a write
lock (exclusive lock).
A single process can hold only one type of
lock on a file region;
Each byte in a file can have only a single type of lock on it at any one time, and may be locked for shared
access, locked for exclusive access, or unlocked.
l_type Either F_RDLCK for a shared (read-only) lock or F_WRLCK for an exclusive
(write) lock