]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>. |
2 | .\" | |
5fbde956 | 3 | .\" SPDX-License-Identifier: Linux-man-pages-copyleft |
fea681da | 4 | .\" |
1ae6b2c7 | 5 | .TH FLOCKFILE 3 2021-03-22 GNU "Linux Programmer's Manual" |
fea681da MK |
6 | .SH NAME |
7 | flockfile, ftrylockfile, funlockfile \- lock FILE for stdio | |
cfba38f4 AC |
8 | .SH LIBRARY |
9 | Standard C library | |
10 | .RI ( libc ", " \-lc ) | |
fea681da MK |
11 | .SH SYNOPSIS |
12 | .nf | |
13 | .B #include <stdio.h> | |
68e4db0a | 14 | .PP |
fea681da | 15 | .BI "void flockfile(FILE *" filehandle ); |
fea681da | 16 | .BI "int ftrylockfile(FILE *" filehandle ); |
fea681da MK |
17 | .BI "void funlockfile(FILE *" filehandle ); |
18 | .fi | |
68e4db0a | 19 | .PP |
d39ad78f | 20 | .RS -4 |
cc4615cc MK |
21 | Feature Test Macro Requirements for glibc (see |
22 | .BR feature_test_macros (7)): | |
d39ad78f | 23 | .RE |
68e4db0a | 24 | .PP |
cfc2d88d | 25 | All functions shown above: |
9d2adbae | 26 | .nf |
5c10d2c5 | 27 | /* Since glibc 2.24: */ _POSIX_C_SOURCE >= 199309L |
9d2adbae MK |
28 | || /* Glibc <= 2.23: */ _POSIX_C_SOURCE |
29 | || /* Glibc <= 2.19: */ _BSD_SOURCE || _SVID_SOURCE | |
30 | .fi | |
fea681da | 31 | .SH DESCRIPTION |
c13182ef MK |
32 | The stdio functions are thread-safe. |
33 | This is achieved by assigning | |
2f0af33b | 34 | to each |
097585ed | 35 | .I FILE |
c7094399 | 36 | object a lockcount and (if the lockcount is nonzero) |
fea681da | 37 | an owning thread. |
2f0af33b | 38 | For each library call, these functions wait until the |
097585ed | 39 | .I FILE |
2f0af33b | 40 | object |
fea681da MK |
41 | is no longer locked by a different thread, then lock it, do the |
42 | requested I/O, and unlock the object again. | |
dd3568a1 | 43 | .PP |
fea681da MK |
44 | (Note: this locking has nothing to do with the file locking done |
45 | by functions like | |
46 | .BR flock (2) | |
47 | and | |
48 | .BR lockf (3).) | |
dd3568a1 | 49 | .PP |
fea681da | 50 | All this is invisible to the C-programmer, but there may be two |
c13182ef MK |
51 | reasons to wish for more detailed control. |
52 | On the one hand, maybe | |
fea681da MK |
53 | a series of I/O actions by one thread belongs together, and should |
54 | not be interrupted by the I/O of some other thread. | |
55 | On the other hand, maybe the locking overhead should be avoided | |
56 | for greater efficiency. | |
dd3568a1 | 57 | .PP |
2f0af33b | 58 | To this end, a thread can explicitly lock the |
097585ed | 59 | .I FILE |
2f0af33b | 60 | object, |
c13182ef MK |
61 | then do its series of I/O actions, then unlock. |
62 | This prevents | |
63 | other threads from coming in between. | |
64 | If the reason for doing | |
fea681da | 65 | this was to achieve greater efficiency, one does the I/O with |
24b74457 | 66 | the nonlocking versions of the stdio functions: with |
d9c1ae64 MK |
67 | .BR getc_unlocked (3) |
68 | and | |
69 | .BR putc_unlocked (3) | |
70 | instead of | |
71 | .BR getc (3) | |
72 | and | |
73 | .BR putc (3). | |
dd3568a1 | 74 | .PP |
60a90ecd MK |
75 | The |
76 | .BR flockfile () | |
c6fa0841 MK |
77 | function waits for |
78 | .I *filehandle | |
79 | to be | |
fea681da | 80 | no longer locked by a different thread, then makes the |
c6fa0841 MK |
81 | current thread owner of |
82 | .IR *filehandle , | |
83 | and increments | |
fea681da | 84 | the lockcount. |
dd3568a1 | 85 | .PP |
60a90ecd MK |
86 | The |
87 | .BR funlockfile () | |
88 | function decrements the lock count. | |
dd3568a1 | 89 | .PP |
60a90ecd MK |
90 | The |
91 | .BR ftrylockfile () | |
ff40dbb3 | 92 | function is a nonblocking version |
60a90ecd MK |
93 | of |
94 | .BR flockfile (). | |
c13182ef | 95 | It does nothing in case some other thread |
c6fa0841 MK |
96 | owns |
97 | .IR *filehandle , | |
98 | and it obtains ownership and increments | |
fea681da | 99 | the lockcount otherwise. |
47297adb | 100 | .SH RETURN VALUE |
60a90ecd MK |
101 | The |
102 | .BR ftrylockfile () | |
103 | function returns zero for success | |
c7094399 | 104 | (the lock was obtained), and nonzero for failure. |
fea681da MK |
105 | .SH ERRORS |
106 | None. | |
3cdc8f57 | 107 | .SH ATTRIBUTES |
cadbf54b MK |
108 | For an explanation of the terms used in this section, see |
109 | .BR attributes (7). | |
c466875e MK |
110 | .ad l |
111 | .nh | |
cadbf54b MK |
112 | .TS |
113 | allbox; | |
c466875e | 114 | lbx lb lb |
cadbf54b MK |
115 | l l l. |
116 | Interface Attribute Value | |
117 | T{ | |
3cdc8f57 PH |
118 | .BR flockfile (), |
119 | .BR ftrylockfile (), | |
3cdc8f57 | 120 | .BR funlockfile () |
cadbf54b MK |
121 | T} Thread safety MT-Safe |
122 | .TE | |
c466875e MK |
123 | .hy |
124 | .ad | |
125 | .sp 1 | |
47297adb | 126 | .SH CONFORMING TO |
af8b5e04 | 127 | POSIX.1-2001, POSIX.1-2008. |
bd168648 | 128 | .PP |
8c4f34f8 MK |
129 | These functions are available when |
130 | .B _POSIX_THREAD_SAFE_FUNCTIONS | |
c13182ef | 131 | is defined. |
47297adb | 132 | .SH SEE ALSO |
fea681da | 133 | .BR unlocked_stdio (3) |