FLOCKFILE(3P)

πŸ¦‹ FLOCKFILE(3P) πŸ¦‹

okay so bestie, welcome to the POSIX Programmer's Manual entry for flockfile πŸ’– β€” it lives right there in section 3P, iykyk.


✨ PROLOG ✨

Real talk before we even start: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, go consult the corresponding Linux manual page for the deets on Linux behavior), or honestly the interface may not be implemented on Linux at all. πŸ™ˆ


πŸ’… NAME πŸ’…

flockfile, ftrylockfile, funlockfile β€” stdio locking functions. the holy trinity of "hey, this FILE is MINE right now" πŸ‘‘


🌸 SYNOPSIS 🌸

#include <stdio.h>

void flockfile(FILE *file);
int ftrylockfile(FILE *file);
void funlockfile(FILE *file);

πŸŽ€ DESCRIPTION πŸŽ€

These functions shall provide for explicit application-level locking of stdio (FILE *) objects. Basically your app gets to do the locking itself instead of vibing and hoping. These functions can be used by a thread to delineate a sequence of I/O statements that are executed as a unit. one thread, one little bundle of I/O, no interruptions ✨

The flockfile() function shall acquire for a thread ownership of a (FILE *) object. it's giving "this is mine now" πŸ’…

The ftrylockfile() function shall acquire for a thread ownership of a (FILE *) object if the object is available; ftrylockfile() is a non-blocking version of flockfile(). it does NOT wait around, it just tries and reports back 🫢

The funlockfile() function shall relinquish the ownership granted to the thread. The behavior is undefined if a thread other than the current owner calls the funlockfile() function. so like… do NOT do that, bestie, that's the forbidden move πŸ’€

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 the flockfile() function is called, if the count is zero or if the count is positive and the caller owns the (FILE *) object, the count shall be incremented. Otherwise, the calling thread shall be suspended, waiting for the count to return to zero. Each call to funlockfile() shall decrement the count. This allows matching calls to flockfile() (or successful calls to ftrylockfile()) and funlockfile() to be nested. nesting rights!! like little lock matryoshka dolls πŸŽ€

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. so the regular ones are lowkey doing all this already behind the scenes πŸ‘€


⭐ RETURN VALUE ⭐

None for flockfile() and funlockfile(). they just do the thing, no receipts

The ftrylockfile() function shall return zero for success and non-zero to indicate that the lock cannot be acquired. zero = slay, non-zero = not this time βœ‹


πŸ’€ ERRORS πŸ’€

No errors are defined. iconic behavior, honestly

The following sections are informative.


πŸ“ EXAMPLES πŸ“

None. bestie there is literally nothing here, go write your own 🫢


🧚 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. yes it's a whole thing, go read 3.291 if you're curious

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 (except those with names ending in _unlocked) from other threads, including calls to exit(). so like… your exit could just be standing there waiting?? yeah. noted. 😭


πŸ‘‘ RATIONALE πŸ‘‘

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(). sibling energy 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 iconic, we love to see it ✨

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. πŸ’€ DEADLOCK. it's giving two besties each waiting for the other to text back first. forever. 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. input first, then output. write that down. πŸ’…

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. and THAT is the whole point, periodt ✨


πŸ¦‹ FUTURE DIRECTIONS πŸ¦‹

None. the future is… unchanged, apparently 🫢


🌈 SEE ALSO 🌈

  • exit()
  • getc_unlocked()

The Base Definitions volume of POSIX.1‐2017, Section 3.291, Priority Inversion, <stdio.h>


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 .


and that's the whole page, bestie!! this one's brought to you by IEEE/The Open Group, dated 2017 πŸŽ€ mwah, go lock your FILEs responsibly πŸ’…βœ¨

πŸ“¦ where this page comes from