Changes: Ready for 5.13
[man-pages.git] / man3 / flockfile.3
blobca371b524462e3d4e7f4f68e5667bfe1d0c2cde4
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH FLOCKFILE 3  2021-03-22 "" "Linux Programmer's Manual"
26 .SH NAME
27 flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdio.h>
31 .PP
32 .BI "void flockfile(FILE *" filehandle );
33 .BI "int ftrylockfile(FILE *" filehandle );
34 .BI "void funlockfile(FILE *" filehandle );
35 .fi
36 .PP
37 .RS -4
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .RE
41 .PP
42 All functions shown above:
43 .nf
44     /* Since glibc 2.24: */ _POSIX_C_SOURCE >= 199309L
45         || /* Glibc <= 2.23: */ _POSIX_C_SOURCE
46         || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
47 .fi
48 .SH DESCRIPTION
49 The stdio functions are thread-safe.
50 This is achieved by assigning
51 to each
52 .I FILE
53 object a lockcount and (if the lockcount is nonzero)
54 an owning thread.
55 For each library call, these functions wait until the
56 .I FILE
57 object
58 is no longer locked by a different thread, then lock it, do the
59 requested I/O, and unlock the object again.
60 .PP
61 (Note: this locking has nothing to do with the file locking done
62 by functions like
63 .BR flock (2)
64 and
65 .BR lockf (3).)
66 .PP
67 All this is invisible to the C-programmer, but there may be two
68 reasons to wish for more detailed control.
69 On the one hand, maybe
70 a series of I/O actions by one thread belongs together, and should
71 not be interrupted by the I/O of some other thread.
72 On the other hand, maybe the locking overhead should be avoided
73 for greater efficiency.
74 .PP
75 To this end, a thread can explicitly lock the
76 .I FILE
77 object,
78 then do its series of I/O actions, then unlock.
79 This prevents
80 other threads from coming in between.
81 If the reason for doing
82 this was to achieve greater efficiency, one does the I/O with
83 the nonlocking versions of the stdio functions: with
84 .BR getc_unlocked (3)
85 and
86 .BR putc_unlocked (3)
87 instead of
88 .BR getc (3)
89 and
90 .BR putc (3).
91 .PP
92 The
93 .BR flockfile ()
94 function waits for
95 .I *filehandle
96 to be
97 no longer locked by a different thread, then makes the
98 current thread owner of
99 .IR *filehandle ,
100 and increments
101 the lockcount.
104 .BR funlockfile ()
105 function decrements the lock count.
108 .BR ftrylockfile ()
109 function is a nonblocking version
111 .BR flockfile ().
112 It does nothing in case some other thread
113 owns
114 .IR *filehandle ,
115 and it obtains ownership and increments
116 the lockcount otherwise.
117 .SH RETURN VALUE
119 .BR ftrylockfile ()
120 function returns zero for success
121 (the lock was obtained), and nonzero for failure.
122 .SH ERRORS
123 None.
124 .SH ATTRIBUTES
125 For an explanation of the terms used in this section, see
126 .BR attributes (7).
127 .ad l
130 allbox;
131 lbx lb lb
132 l l l.
133 Interface       Attribute       Value
135 .BR flockfile (),
136 .BR ftrylockfile (),
137 .BR funlockfile ()
138 T}      Thread safety   MT-Safe
142 .sp 1
143 .SH CONFORMING TO
144 POSIX.1-2001, POSIX.1-2008.
146 These functions are available when
147 .B _POSIX_THREAD_SAFE_FUNCTIONS
148 is defined.
149 .SH SEE ALSO
150 .BR unlocked_stdio (3)