]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/flockfile.3
encrypt.3: srcfix: rewrap source lines
[thirdparty/man-pages.git] / man3 / flockfile.3
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 2017-07-13 "" "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 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41 .ad l
42 .PP
43 All functions shown above:
44 .RS 4
45 /* Since glibc 2.24: */ _POSIX_C_SOURCE\ >=\ 199309L
46 || /* Glibc versions <= 2.23: */ _POSIX_C_SOURCE
47 || /* Glibc versions <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE
48 .RE
49 .ad b
50 .SH DESCRIPTION
51 The stdio functions are thread-safe.
52 This is achieved by assigning
53 to each
54 .I FILE
55 object a lockcount and (if the lockcount is nonzero)
56 an owning thread.
57 For each library call, these functions wait until the
58 .I FILE
59 object
60 is no longer locked by a different thread, then lock it, do the
61 requested I/O, and unlock the object again.
62 .PP
63 (Note: this locking has nothing to do with the file locking done
64 by functions like
65 .BR flock (2)
66 and
67 .BR lockf (3).)
68 .PP
69 All this is invisible to the C-programmer, but there may be two
70 reasons to wish for more detailed control.
71 On the one hand, maybe
72 a series of I/O actions by one thread belongs together, and should
73 not be interrupted by the I/O of some other thread.
74 On the other hand, maybe the locking overhead should be avoided
75 for greater efficiency.
76 .PP
77 To this end, a thread can explicitly lock the
78 .I FILE
79 object,
80 then do its series of I/O actions, then unlock.
81 This prevents
82 other threads from coming in between.
83 If the reason for doing
84 this was to achieve greater efficiency, one does the I/O with
85 the nonlocking versions of the stdio functions: with
86 .BR getc_unlocked (3)
87 and
88 .BR putc_unlocked (3)
89 instead of
90 .BR getc (3)
91 and
92 .BR putc (3).
93 .PP
94 The
95 .BR flockfile ()
96 function waits for
97 .I *filehandle
98 to be
99 no longer locked by a different thread, then makes the
100 current thread owner of
101 .IR *filehandle ,
102 and increments
103 the lockcount.
104 .PP
105 The
106 .BR funlockfile ()
107 function decrements the lock count.
108 .PP
109 The
110 .BR ftrylockfile ()
111 function is a nonblocking version
112 of
113 .BR flockfile ().
114 It does nothing in case some other thread
115 owns
116 .IR *filehandle ,
117 and it obtains ownership and increments
118 the lockcount otherwise.
119 .SH RETURN VALUE
120 The
121 .BR ftrylockfile ()
122 function returns zero for success
123 (the lock was obtained), and nonzero for failure.
124 .SH ERRORS
125 None.
126 .SH ATTRIBUTES
127 For an explanation of the terms used in this section, see
128 .BR attributes (7).
129 .TS
130 allbox;
131 lbw29 lb lb
132 l l l.
133 Interface Attribute Value
134 T{
135 .BR flockfile (),
136 .BR ftrylockfile (),
137 .BR funlockfile ()
138 T} Thread safety MT-Safe
139 .TE
140 .SH CONFORMING TO
141 POSIX.1-2001, POSIX.1-2008.
142 .SH AVAILABILITY
143 These functions are available when
144 .B _POSIX_THREAD_SAFE_FUNCTIONS
145 is defined.
146 .SH SEE ALSO
147 .BR unlocked_stdio (3)