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 versions <= 2.23: */ _POSIX_C_SOURCE
20               || /* Glibc versions <= 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
53       obtains ownership and increments the lockcount otherwise.
54

RETURN VALUE

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

ERRORS

60       None.
61

ATTRIBUTES

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

CONFORMING TO

73       POSIX.1-2001, POSIX.1-2008.
74

AVAILABILITY

76       These functions  are  available  when  _POSIX_THREAD_SAFE_FUNCTIONS  is
77       defined.
78

SEE ALSO

80       unlocked_stdio(3)
81

COLOPHON

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