FLOCKFILE(3POSIX)
๐ FLOCKFILE(3POSIX) โ the POSIX Programmer's Manual, but make it group chat
okay bestie so this page is literally from the POSIX Programmer's Manual (you know, the FLOCKFILE(3POSIX) one) ๐
๐จ PROLOG ๐จ
so like, real talk before we get into it: this manual page is part of the POSIX Programmer's Manual. the Linux implementation of this interface may differ (so if you're on Linux, bestie, go consult the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not even be implemented on Linux at all. not me warning you first thing ๐
โจ NAME โจ
flockfile, ftrylockfile, funlockfile โ the stdio locking functions ๐ฆ
๐ธ SYNOPSIS ๐ธ
#include <stdio.h>
void flockfile(FILE *file);
int ftrylockfile(FILE *file);
void funlockfile(FILE *file);
like. that's it. that's the whole vibe. ๐
๐ DESCRIPTION ๐
so these functions shall provide for explicit application-level locking of stdio (FILE *) objects. a thread can use them to delineate a sequence of I/O statements that are executed as a unit. it's giving "this block is MINE, nobody else touch it" ๐
flockfile()โ shall acquire for a thread ownership of a(FILE *)object. periodt.ftrylockfile()โ shall acquire for a thread ownership of a(FILE *)object if the object is available;ftrylockfile()is the non-blocking version offlockfile(). it's the "hey are you free? no? okay bye" of functions โจfunlockfile()โ shall relinquish the ownership granted to the thread. and listen: the behavior is undefined if a thread other than the current owner calls thefunlockfile()function. so like, don't do that. that's not your lock. that's not your man. ๐ฉ
now here's the lore: the functions shall behave as if there is a lock count associated with each (FILE *) object. this count is implicitly initialized to zero when the (FILE *) object is created. the (FILE *) object is unlocked when the count is zero. when the count is positive, a single thread owns the (FILE *) object.
when flockfile() is called:
- if the count is zero, or
- if the count is positive and the caller owns the
(FILE *)object,
then the count shall be incremented. otherwise, the calling thread shall be suspended, waiting for the count to return to zero. like, standing outside the club, no cap.
each call to funlockfile() shall decrement the count. this is what allows matching calls to flockfile() (or successful calls to ftrylockfile()) and funlockfile() to be nested. layers, bestie. onions. ๐ง
and the last lil lore drop: all functions that reference (FILE *) objects โ except those with names ending in _unlocked โ shall behave as if they use flockfile() and funlockfile() internally to obtain ownership of these (FILE *) objects. iykyk ๐
๐ RETURN VALUE ๐
none for flockfile() and funlockfile(). they return nothing. they simply are. ๐
the ftrylockfile() function shall return zero for success and non-zero to indicate that the lock cannot be acquired. so zero = slay, non-zero = not today. ๐
๐ ERRORS ๐
no errors are defined. iconic behavior honestly. zero drama. ๐ซถ
the following sections are informative. (yes bestie, that means the good stuff below is informative, not normative โ it's the commentary track, not the movie ๐ฌ)
๐ EXAMPLES ๐
none. nope. absolutely none. the void. the emptiness. crickets ๐ฆ
๐ APPLICATION USAGE ๐
applications using these functions may be subject to priority inversion, as discussed in the Base Definitions volume of POSIX.1โ2017, Section 3.291, Priority Inversion. so like, heads up. ๐
also, a call to exit() can block until locked streams are unlocked, because a thread having ownership of a (FILE*) object blocks all function calls that reference that (FILE*) object from other threads (except those with names ending in _unlocked) โ including calls to exit(). so yeah, your exit could just be standing there waiting. awkward. ๐ญ
๐ง RATIONALE ๐ง
so the flockfile() and funlockfile() functions provide an orthogonal mutual-exclusion lock for each FILE. the ftrylockfile() function provides a non-blocking attempt to acquire a file lock, analogous to pthread_mutex_trylock(). sister functions fr fr. ๐ฏ
these locks behave as if they are the same as those used internally by stdio for thread-safety. this both:
- provides thread-safety of these functions without requiring a second level of internal locking, and
- allows functions in stdio to be implemented in terms of other stdio functions.
efficient AND elegant? we love a two-for-one. โจ
now, the serious part, because bestie i need you locked in for this one: application developers and implementors should be aware that there are potential deadlock problems on FILE objects. for example, the line-buffered flushing semantics of stdio (requested via {_IOLBF}) require that certain input operations sometimes cause the buffered contents of implementation-defined line-buffered output streams to be flushed. if two threads each hold the lock on the other's FILE, deadlock ensues. ๐๐๐
this type of deadlock can be avoided by acquiring FILE locks in a consistent order. in particular, the line-buffered output stream deadlock can typically be avoided by acquiring locks on input streams before locks on output streams if a thread would be acquiring both. order matters, bestie. always has. ๐ฏ
in summary: threads sharing stdio streams with other threads can use flockfile() and funlockfile() to cause sequences of I/O performed by a single thread to be kept bundled. the only case where the use of flockfile() and funlockfile() is required is to provide a scope protecting uses of the *_unlocked functions/macros. this moves the cost/performance tradeoff to the optimal point. chef's kiss. ๐จโ๐ณ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
none. the future is... also nothing. we stay present. ๐ง
๐ SEE ALSO ๐
exit()getc_unlocked()- the Base Definitions volume of POSIX.1โ2017, Section 3.291, Priority Inversion
<stdio.h>
๐ COPYRIGHT ๐
portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.
in the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. the original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. to report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
k that's the whole page bestie, go lock some files responsibly and please, please don't call funlockfile() on someone else's lock, i'm begging ๐
mwah ๐ this has been IEEE/The Open Group, 2017, signing off from FLOCKFILE(3POSIX) โ stay unlocked (metaphorically) ๐ธ๐ฆ๐
๐ฆ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 ยท Other
- debian trixie: manpages-posix-dev 2017a-2 ยท Other
- ubuntu jammy: manpages-posix-dev 2017a-2 ยท Other
- ubuntu noble: manpages-posix-dev 2017a-2 ยท Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms