FLOCK(2)
π FLOCK(2) π
okay bestie, welcome to the flock(2) manual page β yes, that's a section 2 system call, we're in the System Calls Manual today and it's giving β¨kernel coreβ¨ energy. grab a boba, we're locking files.
β¨ NAME β¨
flock β apply or remove an advisory lock on an open file π
π LIBRARY π
Standard C Library (libc, -lc). yeah. the basics. the girlies. π
π¦ SYNOPSIS π¦
#include <sys/file.h>
#define LOCK_SH 0x01 /* shared file lock */
#define LOCK_EX 0x02 /* exclusive file lock */
#define LOCK_NB 0x04 /* do not block when locking */
#define LOCK_UN 0x08 /* unlock file */
int
flock(int fd, int operation);
the way that's just the whole vibe up front, no cap. πΈ
π DESCRIPTION π
so. the flock() system call applies or removes an advisory lock on the file associated with the file descriptor fd. that's the whole gig.
you apply a lock by passing an operation argument that's either LOCK_SH or LOCK_EX, and you can optionally add LOCK_NB on top. wanna unlock an existing lock? operation should be LOCK_UN. periodt.
here's the flag lineup, one by one, because we don't skip anything in this house:
LOCK_SHβ0x01, the shared file lock πLOCK_EXβ0x02, the exclusive file lock πLOCK_NBβ0x04, do not block when locking πLOCK_UNβ0x08, unlock the file π
now, about "advisory" β it's advisory, bestie. advisory locks let cooperating processes do consistent operations on files, but they do not guarantee consistency. like, i.e., processes may still access files without using advisory locks, possibly resulting in inconsistencies. it's a vibe agreement, not a bodyguard. π
the locking mechanism allows two types of locks: shared locks and exclusive locks. at any time multiple shared locks may be applied to a file, but at no time are multiple exclusive locks β or both shared and exclusive locks β allowed simultaneously on a file. one exclusive, that's the rule, don't fight it. π
a shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate lock type. what happens under the hood? the previous lock gets released and the new lock gets applied β possibly after other processes have gained and released the lock. so like, you might be waiting in line behind the group chat. π
requesting a lock on an object that's already locked normally causes the caller to be blocked until the lock may be acquired. but if LOCK_NB is included in operation, that does not happen β instead the call will fail and the error EWOULDBLOCK will be returned. no waiting, just rejection. iconic honestly.
π NOTES π
locks are on files, not file descriptors. read that again, bestie, it matters.
file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock β they're multiple references to a single lock. and here's the wild part: if a process holding a lock on a file forks, and the child explicitly unlocks the file, the parent will lose its lock. family drama, literally. π
the flock(), fcntl(2), and lockf(3) locks are compatible π«Ά. processes using different locking interfaces can cooperate over the same file safely. however, only one of such interfaces should be used within the same process. and: if a file is locked by a process through flock(), any record within the file will be seen as locked from the viewpoint of another process using fcntl(2) or lockf(3), and vice versa. cross-compatible, no cap. β¨
processes blocked awaiting a lock may be awakened by signals. β‘
π«Ά RETURN VALUES π«Ά
- the
flock()function returns the value0if successful β slay π - otherwise the value
-1is returned, and the global variableerrnois set to indicate the error π
π ERRORS π
the flock() system call fails if:
[EWOULDBLOCK]β the file is locked and theLOCK_NBoption was specified. π[EBADF]β the argumentfdis an invalid descriptor.[EINVAL]β the argumentfdrefers to an object other than a file.[EOPNOTSUPP]β the argumentfdrefers to an object that does not support file locking.[ENOLCK]β a lock was requested, but no locks are available.
π SEE ALSO π
close(2), dup(2), execve(2), fcntl(2), fork(2), open(2), flopen(3), lockf(3)
go read them, bestie, the lore is deep. π
β HISTORY β
the flock() system call appeared in 4.2BSD. ancient. legendary. the blueprint. π
and that's the whole page, fr fr π this edition came from the Debian crew, dated November 9, 2011, filed under FLOCK(2) β so when you're done locking and unlocking like the main character you are, go touch some grass, hydrate, and remember: advisory means advisory. mwah. ππ¦β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1983, 1991, 1993 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms