The Linux Programming Interface

(nextflipdebug5) #1
File Locking 1125

off_t l_start; /* Offset where the lock begins */
off_t l_len; /* Number of bytes to lock; 0 means "until EOF" */
pid_t l_pid; /* Process preventing our lock (F_GETLK only) */
};

The l_type field indicates the type of lock we want to place. It is specified as one of
the values in Table 55-3.
Semantically, read (F_RDLCK) and write (F_WRLCK) locks correspond to the shared
and exclusive locks applied by flock(), and they follow the same compatibility rules
(Table 55-2): any number of processes can hold read locks on a file region, but only
one process can hold a write lock, and that lock excludes read and write locks by
other processes. Specifying l_type as F_UNLCK is analogous to the flock() LOCK_UN operation.


Figure 55-2: Using record locks to synchronize access to the same region of a file


Table 55-3: Lock types for fcntl() locking


Lock type Description
F_RDLCK Place a read lock
F_WRLCK Place a write lock
F_UNLCK Remove an existing lock

Process A

Request write lock on
bytes 0 to 99

Update bytes 0 to 99

Process B

Convert lock on bytes
0 to 99 to read lock

Request read lock on
bytes 0 to 99
blocks

unblocks

Read bytes 0 to 99 Read bytes 0 to 99

Convert lock on bytes
0 to 99 to write lock
blocks

unblocks Unlock bytes 0 to 99

Update bytes 0 to 99

Unlock bytes 0 to 99

Time
Free download pdf