lockf(3)
๐ lockf(3) โ like, the POSIX file-locking girlie
okay bestie, welcome to lockf(3) from the Library Functions Manual โจ this one lives in the Standard C library (libc, -lc) and she is ALL about putting locks on bits of an open file. apply them, test them, remove them โ the whole vibe. ๐
โจ SYNOPSIS โจ
#include <unistd.h>
int lockf(int fd, int op, off_t size);
๐ฆ Feature Test Macro Requirements for glibc (peep feature_test_macros(7))
lockf():
_XOPEN_SOURCE >= 500
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
no cap, you need one of those macros defined or glibc will simply not hand her over ๐
๐ธ DESCRIPTION ๐ธ
So here's the tea: lockf() applies, tests, or removes a POSIX lock on a section of an open file. ๐ซถ
- the file is the one behind
fdโ a file descriptor that's open for writing - the action is whatever you put in
op - and the section? it's all about byte positions, and it depends on the sign of
size:- if
sizeis positive, the section ispos..pos+size-1 - if
sizeis negative, the section ispos-size..pos-1 - where
posis the current file position - and if
sizeis zero, the section stretches from the current file position all the way to infinity, absorbing both the present and future end-of-file positions ๐
- if
and in every single one of those cases, the section is allowed to extend past the current end-of-file. slay, boundaries are fake.
On Linux, lockf() is literally just an interface sitting on top of fcntl(2) locking. Many other systems do lockf() this exact same way, but โ heads up โ POSIX.1 leaves the relationship between lockf() and fcntl(2) locks unspecified. So if you want a portable app, you should probably not be mixing calls to these two interfaces. pick a lane, bestie ๐ญ
The valid operations are right here, and there are four of them:
F_LOCKโ set an exclusive lock on the specified section of the file. if (part of) that section is already locked, the call blocks until the previous lock gets released. and if this section overlaps an earlier locked section, the two get merged into one ๐ซ. also: file locks are released the moment the process holding them closes some file descriptor for the file. and a child process does not inherit these locks. main character energy only, no hand-me-downs ๐F_TLOCKโ same asF_LOCK, except this one never blocks, and instead returns an error if the file is already locked. non-committal queen ๐F_ULOCKโ unlock the indicated section of the file. this can cause a locked section to get split into two locked sections, which is honestly kind of iconic ๐F_TESTโ test the lock: return0if the specified section is unlocked or locked by this process; return-1and seterrnotoEAGAIN(EACCESon some other systems) if another process holds a lock. she's just asking questions, not starting drama ๐ฅ
๐ RETURN VALUE ๐
On success, zero is returned. โจ
On error, -1 is returned, and errno is set to tell you exactly what went wrong. ๐ญ
๐ ERRORS ๐
EACCESorEAGAINโ the file is locked andF_TLOCKorF_TESTwas specified, or the operation is prohibited because the file has been memory-mapped by another process. ngl, rude.EBADFโfdis not an open file descriptor; oropisF_LOCKorF_TLOCKandfdis not a writable file descriptor. you gotta be writable for those two, periodt.EDEADLKโopwasF_LOCKand this lock operation would cause a deadlock. we do not do deadlocks in this house ๐EINTRโ while waiting to acquire a lock, the call was interrupted by delivery of a signal caught by a handler; seesignal(7).EINVALโ an invalid operation was specified inop.ENOLCKโ too many segment locks open, lock table is full. the lock table is, like, full full ๐
โญ ATTRIBUTES โญ
For an explanation of the terms used in this section, peep attributes(7). here's the deets:
- Interface:
lockf() - Attribute: Thread safety
- Value:
MT-Safe
so yeah, she's MT-Safe ๐
๐ STANDARDS ๐
POSIX.1-2008. certified, official, canon โจ
๐ง HISTORY ๐ง
POSIX.1-2001, SVr4. she's been around, she's seen things ๐
๐ฅ SEE ALSO ๐ฅ
also, for the extra-credit girlies: locks.txt and mandatory-locking.txt live in the Linux kernel source directory Documentation/filesystems โ and btw, on older kernels those files sit directly under the Documentation directory, and mandatory-locking.txt is called mandatory.txt there. iykyk ๐ธ
and that's the whole page bestie, mwah ๐ this was Linux man-pages 6.20, dated 2026-02-08 โ go forth and lock responsibly, no cap ๐ซถโจ
๐ฆ 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: GPL-2.0-or-later
- Copyright 1997, Nicolรกs Lichtmaier <nick@debian.org>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms