flock(2)
๐ฆ flock(2) โ apply or remove an advisory lock on an open file ๐ฆ
hi bestie ๐ welcome to the System Calls Manual, the part of the docs where we talk directly to the kernel and the kernel talks back. this one's about flock, and it's genuinely one of the cutest little syscalls you'll ever meet โ it's giving file-locking bestie, and yes, you're gonna need it the moment two processes start fighting over the same file like it's the last croissant ๐ฅ
โจ NAME โจ
flock โ apply or remove an advisory lock on an open file ๐
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ธ SYNOPSIS ๐ธ
#include <sys/file.h>
int flock(int fd, int op);
๐ DESCRIPTION ๐
so the whole vibe is: you apply or remove an advisory lock on the open file specified by fd. and op? op is literally one of these three icons ๐
LOCK_SHโ Place a shared lock. More than one process may hold a shared lock for a given file at a given time. sharing is caring ๐ซถLOCK_EXโ Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time. main character energy only, nobody else invited ๐LOCK_UNโ Remove an existing lock held by this process. bye bestie ๐
now, a call to flock() may block if an incompatible lock is held by another process. want it nonblocking? include LOCK_NB (by ORing it) with any of the above operations. it's that easy, no cap โจ
a single file may not simultaneously have both shared and exclusive locks. pick a lane, babe ๐ซ
locks created by flock() are associated with an open file description (see open(2)). which means duplicate file descriptors โ created by, for example, fork(2) or dup(2) โ refer to the same lock, and that lock may be modified or released using any of these file descriptors. furthermore, the lock is released either by an explicit LOCK_UN operation on any of these duplicate file descriptors, or when all such file descriptors have been closed. so it's a whole group chat of file descriptors and they all share one lock ๐ซ
if a process uses open(2) (or similar) to obtain more than one file descriptor for the same file, these file descriptors are treated independently by flock(). an attempt to lock the file using one of these file descriptors may be denied by a lock that the calling process has already placed via another file descriptor. like, your own self can block your own self. embarrassing but iconic ๐ญ
a process may hold only one type of lock (shared or exclusive) on a file. subsequent flock() calls on an already locked file will convert an existing lock to the new lock mode. glow up, lock edition ๐ซ
locks created by flock() are preserved across an execve(2). they survive the transformation, fr fr ๐ฆ
a shared or exclusive lock can be placed on a file regardless of the mode in which the file was opened. read-only, write-only, whatever โ the lock does not care โจ
๐ RETURN VALUE ๐
on success, zero is returned. on error, -1 is returned, and errno is set to indicate the error. simple as ๐
๐ ERRORS ๐
EBADFโfdis not an open file descriptor. bestie that's not a file ๐ญEINTRโ While waiting to acquire a lock, the call was interrupted by delivery of a signal caught by a handler; seesignal(7). rude interruption honestly ๐EINVALโopis invalid. that's not one of the three, babes ๐ENOLCKโ The kernel ran out of memory for allocating lock records. the kernel is full, no room at the inn ๐EWOULDBLOCKโ The file is locked and theLOCK_NBflag was selected. you asked for nonblocking, you got nonblocking ๐ซ
โญ VERSIONS โญ
since Linux 2.0, flock() is implemented as a system call in its own right rather than being emulated in the GNU C library as a call to fcntl(2). with this implementation, there is no interaction between the types of lock placed by flock() and fcntl(2), and flock() does not detect deadlock. (note, however, that on some systems, such as the modern BSDs, flock() and fcntl(2) locks do interact with one another.) so it's giving "we don't talk to each other, we're not on speaking terms" ๐ค
๐ CIFS details ๐
up to Linux 5.4, flock() is not propagated over SMB. a file with such locks will not appear locked for remote clients. the remote besties have no idea, they're living in the dark ๐ซ
since Linux 5.5, flock() locks are emulated with SMB byte-range locks on the entire file. similarly to NFS, this means that fcntl(2) and flock() locks interact with one another. another important side-effect is that the locks are not advisory anymore: any IO on a locked file will always fail with EACCES when done from a separate file descriptor. this difference originates from the design of locks in the SMB protocol, which provides mandatory locking semantics. so yeah, mandatory = the lock actually enforces itself. no ignoring it, periodt ๐
remote and mandatory locking semantics may vary with SMB protocol, mount options and server type. see mount.cifs(8) for additional information. ๐
๐ธ STANDARDS ๐ธ
BSD. slay, we're keeping the old traditions alive ๐
๐ง HISTORY ๐ง
4.4BSD (the flock() call first appeared in 4.2BSD). a version of flock(), possibly implemented in terms of fcntl(2), appears on most UNIX systems. she's been around forever and she's still booked and busy ๐
โ๏ธ NFS details โ๏ธ
up to Linux 2.6.11, flock() does not lock files over NFS (i.e., the scope of locks was limited to the local system). instead, one could use fcntl(2) byte-range locking, which does work over NFS, given a sufficiently recent version of Linux and a server which supports locking. workaround era ๐ญ
since Linux 2.6.12, NFS clients support flock() locks by emulating them as fcntl(2) byte-range locks on the entire file. this means that fcntl(2) and flock() locks do interact with one another over NFS. it also means that in order to place an exclusive lock, the file must be opened for writing. noted, bestie ๐
since Linux 2.6.37, the kernel supports a compatibility mode that allows flock() locks (and also fcntl(2) byte region locks) to be treated as local; see the discussion of the local_lock option in nfs(5).
๐ NOTES ๐
flock() places advisory locks only; given suitable permissions on a file, a process is free to ignore the use of flock() and perform I/O on the file. advisory means "please respect this," not "you physically cannot." the lock is a vibe, not a wall ๐ซถ
flock() and fcntl(2) locks have different semantics with respect to forked processes and dup(2). on systems that implement flock() using fcntl(2), the semantics of flock() will be different from those described in this manual page. so read carefully, it depends on the platform ๐
converting a lock (shared to exclusive, or vice versa) is not guaranteed to be atomic: the existing lock is first removed, and then a new lock is established. between these two steps, a pending lock request by another process may be granted, with the result that the conversion either blocks, or fails if LOCK_NB was specified. (this is the original BSD behavior, and occurs on many other implementations.) there's a little gap in the middle and somebody can sneak in ๐ญ
release of a lock when a file descriptor is closed is not sequenced after all observable effects of close(2). for example, if one process writes a file while holding an exclusive lock, then closes that file, and another process blocks placing a shared lock on that file to wait until it is closed, it may observe that a subsequent execve(2) of that file fails with ETXTBSY, and umount(2) of its underlying filesystem fails with EBUSY, as if the file were still open in the first process. spooky ghost file behavior, genuinely ๐
๐ SEE ALSO ๐
flock(1)close(2)dup(2)execve(2)fcntl(2)fork(2)open(2)lockf(3)lslocks(8)Documentation/filesystems/locks.rstin the Linux kernel source tree
and that's the whole page, bestie ๐ this was Linux man-pages 6.20, dated 2026-02-08 โ go lock something responsibly, wrap your file descriptors in a little hug, and remember: advisory means they should respect it, not that they have to ๐โจ๐ฆ periodt.
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1993, Rickard E. Faith <faith@cs.unc.edu>
- Copyright 2002, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms