1FLOCKFILE(3) Linux Programmer's Manual FLOCKFILE(3)
2
3
4
6 flockfile, ftrylockfile, funlockfile - lock FILE for stdio
7
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 _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
19 _SVID_SOURCE || _POSIX_SOURCE
20
22 The stdio functions are thread-safe. This is achieved by assigning to
23 each FILE object a lockcount and (if the lockcount is nonzero) an own‐
24 ing thread. For each library call, these functions wait until the FILE
25 object is no longer locked by a different thread, then lock it, do the
26 requested I/O, and unlock the object again.
27
28 (Note: this locking has nothing to do with the file locking done by
29 functions like flock(2) and lockf(3).)
30
31 All this is invisible to the C-programmer, but there may be two reasons
32 to wish for more detailed control. On the one hand, maybe a series of
33 I/O actions by one thread belongs together, and should not be inter‐
34 rupted by the I/O of some other thread. On the other hand, maybe the
35 locking overhead should be avoided for greater efficiency.
36
37 To this end, a thread can explicitly lock the FILE object, then do its
38 series of I/O actions, then unlock. This prevents other threads from
39 coming in between. If the reason for doing this was to achieve greater
40 efficiency, one does the I/O with the nonlocking versions of the stdio
41 functions: with getc_unlocked(3) and putc_unlocked(3) instead of
42 getc(3) and putc(3).
43
44 The flockfile() function waits for *filehandle to be no longer locked
45 by a different thread, then makes the current thread owner of *filehan‐
46 dle, and increments the lockcount.
47
48 The funlockfile() function decrements the lock count.
49
50 The ftrylockfile() function is a nonblocking version of flockfile().
51 It does nothing in case some other thread owns *filehandle, and it
52 obtains ownership and increments the lockcount otherwise.
53
55 The ftrylockfile() function returns zero for success (the lock was
56 obtained), and nonzero for failure.
57
59 None.
60
62 Multithreading (see pthreads(7))
63 The flockfile(), ftrylockfile(), and funlockfile() functions are
64 thread-safe.
65
67 POSIX.1-2001.
68
70 These functions are available when _POSIX_THREAD_SAFE_FUNCTIONS is
71 defined. They are in libc since libc 5.1.1 and in glibc since glibc
72 2.0.
73
75 unlocked_stdio(3)
76
78 This page is part of release 3.53 of the Linux man-pages project. A
79 description of the project, and information about reporting bugs, can
80 be found at http://www.kernel.org/doc/man-pages/.
81
82
83
84 2013-07-23 FLOCKFILE(3)