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: _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE ||
18       _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
19

DESCRIPTION

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

RETURN VALUE

54       The ftrylockfile() function returns zero  for  success  (the  lock  was
55       obtained), and non-zero for failure.
56

ERRORS

58       None.
59

CONFORMING TO

61       POSIX.1-2001.
62

AVAILABILITY

64       These  functions  are  available  when  _POSIX_THREAD_SAFE_FUNCTIONS is
65       defined.  They are in libc since libc 5.1.1 and in  glibc  since  glibc
66       2.0.
67

SEE ALSO

69       unlocked_stdio(3)
70

COLOPHON

72       This  page  is  part of release 3.22 of the Linux man-pages project.  A
73       description of the project, and information about reporting  bugs,  can
74       be found at http://www.kernel.org/doc/man-pages/.
75
76
77
78                                  2008-08-29                      FLOCKFILE(3)
Impressum