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 โ€” fd is 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; see signal(7). rude interruption honestly ๐Ÿ™ˆ
  • EINVAL โ€” op is 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 the LOCK_NB flag 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 ๐Ÿ’–


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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms