1flockfile(3)               Library Functions Manual               flockfile(3)
2
3
4

NAME

6       flockfile, ftrylockfile, funlockfile - lock FILE for stdio
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdio.h>
13
14       void flockfile(FILE *filehandle);
15       int ftrylockfile(FILE *filehandle);
16       void funlockfile(FILE *filehandle);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       All functions shown above:
21           /* Since glibc 2.24: */ _POSIX_C_SOURCE >= 199309L
22               || /* glibc <= 2.23: */ _POSIX_C_SOURCE
23               || /* glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
24

DESCRIPTION

26       The  stdio functions are thread-safe.  This is achieved by assigning to
27       each FILE object a lockcount and (if the lockcount is nonzero) an  own‐
28       ing thread.  For each library call, these functions wait until the FILE
29       object is no longer locked by a different thread, then lock it, do  the
30       requested I/O, and unlock the object again.
31
32       (Note:  this  locking  has  nothing to do with the file locking done by
33       functions like flock(2) and lockf(3).)
34
35       All this is invisible to the C-programmer, but there may be two reasons
36       to  wish for more detailed control.  On the one hand, maybe a series of
37       I/O actions by one thread belongs together, and should  not  be  inter‐
38       rupted  by  the I/O of some other thread.  On the other hand, maybe the
39       locking overhead should be avoided for greater efficiency.
40
41       To this end, a thread can explicitly lock the FILE object, then do  its
42       series  of  I/O actions, then unlock.  This prevents other threads from
43       coming in between.  If the reason for doing this was to achieve greater
44       efficiency,  one does the I/O with the nonlocking versions of the stdio
45       functions:  with  getc_unlocked(3)  and  putc_unlocked(3)  instead   of
46       getc(3) and putc(3).
47
48       The  flockfile()  function waits for *filehandle to be no longer locked
49       by a different thread, then makes the current thread owner of *filehan‐
50       dle, and increments the lockcount.
51
52       The funlockfile() function decrements the lock count.
53
54       The  ftrylockfile()  function  is a nonblocking version of flockfile().
55       It does nothing in case some other thread owns *filehandle, and it  ob‐
56       tains ownership and increments the lockcount otherwise.
57

RETURN VALUE

59       The  ftrylockfile() function returns zero for success (the lock was ob‐
60       tained), and nonzero for failure.
61

ERRORS

63       None.
64

ATTRIBUTES

66       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
67       tributes(7).
68
69       ┌────────────────────────────────────────────┬───────────────┬─────────┐
70Interface                                   Attribute     Value   
71       ├────────────────────────────────────────────┼───────────────┼─────────┤
72flockfile(), ftrylockfile(), funlockfile()  │ Thread safety │ MT-Safe │
73       └────────────────────────────────────────────┴───────────────┴─────────┘
74

STANDARDS

76       POSIX.1-2008.
77

HISTORY

79       POSIX.1-2001.
80
81       These  functions  are  available  when  _POSIX_THREAD_SAFE_FUNCTIONS is
82       defined.
83

SEE ALSO

85       unlocked_stdio(3)
86
87
88
89Linux man-pages 6.05              2023-07-20                      flockfile(3)
Impressum