1FLOCKFILE(3)               Linux Programmer's Manual              FLOCKFILE(3)
2
3
4

NAME

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

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

56       The  ftrylockfile() function returns zero for success (the lock was ob‐
57       tained), and nonzero for failure.
58

ERRORS

60       None.
61

ATTRIBUTES

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

CONFORMING TO

73       POSIX.1-2001, POSIX.1-2008.
74
75       These  functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is de‐
76       fined.
77

SEE ALSO

79       unlocked_stdio(3)
80

COLOPHON

82       This page is part of release 5.12 of the Linux  man-pages  project.   A
83       description  of  the project, information about reporting bugs, and the
84       latest    version    of    this    page,    can     be     found     at
85       https://www.kernel.org/doc/man-pages/.
86
87
88
89                                  2021-03-22                      FLOCKFILE(3)
Impressum