flock
flock is a whole-file locking interface that associates shared or exclusive locks with an open file description on Linux.
LOCK_SH requests a shared lock, LOCK_EX an exclusive lock and LOCK_UN release; LOCK_NB requests a nonblocking attempt. Descriptors inherited through fork or created by dup share ownership. A separate open is a separate contender, even in the same process. [1]
This syscall is distinct from struct flock, the argument structure used by fcntl record-lock operations, and from the byte-range F_OFD_* API. [2]
The local Linux flock and fcntl families are separate; two programs choosing different families can both acquire locks on the same file. Network-filesystem emulation can change that relationship. [5] [1]
Hesela's local ext4 probe ran nine ordered API pairs at two compiler optimization levels with three repeats each. A flock holder admitted both fcntl contenders; the fcntl families excluded one another. All 54 fresh-child post-unlock controls succeeded. This checks one Linux 6.8.0-149-generic configuration, not network locking or durability. [7]
Relations
- related
- Advisory file lock, Open-file-description lock, Process-associated record lock, Open file description
Limitations
- A failed nonblocking conversion need not preserve the earlier lock; flock has no deadlock detection. [1]
- NFS local_lock settings can restrict exclusion to one client; a successful local lock is not cross-client coordination. [3]
- SMB lock enforcement depends on protocol, server and mount settings; nobrl disables server byte-range lock requests. [4]
- This record summarizes APIs, not a new network-filesystem or crash experiment. [6]
Citations
- Linux man-pages: flock(2), ownership, conversion and filesystem differences
- Linux man-pages: fcntl_locking(2), process and open-description record locks
- nfs(5): local_lock and client-local exclusion
- mount.cifs(8): nobrl and server byte-range lock requests
- Linux kernel: File Locking Release Notes, separate flock and fcntl families
- Hesela: lock families, ownership and filesystem scope
- Hesela: local interprocess lock-family matrix, raw observations and source hashes