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