]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/flockfile.3
man*/: ffix (un-bracket tables)
[thirdparty/man-pages.git] / man3 / flockfile.3
CommitLineData
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
8flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
cfba38f4
AC
9.SH LIBRARY
10Standard 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
22Feature Test Macro Requirements for glibc (see
23.BR feature_test_macros (7)):
d39ad78f 24.RE
68e4db0a 25.PP
cfc2d88d 26All 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
33The stdio functions are thread-safe.
34This is achieved by assigning
2f0af33b 35to each
097585ed 36.I FILE
c7094399 37object a lockcount and (if the lockcount is nonzero)
fea681da 38an owning thread.
2f0af33b 39For each library call, these functions wait until the
097585ed 40.I FILE
2f0af33b 41object
fea681da
MK
42is no longer locked by a different thread, then lock it, do the
43requested 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
46by functions like
47.BR flock (2)
48and
49.BR lockf (3).)
dd3568a1 50.PP
fea681da 51All this is invisible to the C-programmer, but there may be two
c13182ef
MK
52reasons to wish for more detailed control.
53On the one hand, maybe
fea681da
MK
54a series of I/O actions by one thread belongs together, and should
55not be interrupted by the I/O of some other thread.
56On the other hand, maybe the locking overhead should be avoided
57for greater efficiency.
dd3568a1 58.PP
2f0af33b 59To this end, a thread can explicitly lock the
097585ed 60.I FILE
2f0af33b 61object,
c13182ef
MK
62then do its series of I/O actions, then unlock.
63This prevents
64other threads from coming in between.
65If the reason for doing
fea681da 66this was to achieve greater efficiency, one does the I/O with
24b74457 67the nonlocking versions of the stdio functions: with
d9c1ae64
MK
68.BR getc_unlocked (3)
69and
70.BR putc_unlocked (3)
71instead of
72.BR getc (3)
73and
74.BR putc (3).
dd3568a1 75.PP
60a90ecd
MK
76The
77.BR flockfile ()
c6fa0841
MK
78function waits for
79.I *filehandle
80to be
fea681da 81no longer locked by a different thread, then makes the
c6fa0841
MK
82current thread owner of
83.IR *filehandle ,
84and increments
fea681da 85the lockcount.
dd3568a1 86.PP
60a90ecd
MK
87The
88.BR funlockfile ()
89function decrements the lock count.
dd3568a1 90.PP
60a90ecd
MK
91The
92.BR ftrylockfile ()
ff40dbb3 93function is a nonblocking version
60a90ecd
MK
94of
95.BR flockfile ().
c13182ef 96It does nothing in case some other thread
c6fa0841
MK
97owns
98.IR *filehandle ,
99and it obtains ownership and increments
fea681da 100the lockcount otherwise.
47297adb 101.SH RETURN VALUE
60a90ecd
MK
102The
103.BR ftrylockfile ()
104function returns zero for success
c7094399 105(the lock was obtained), and nonzero for failure.
fea681da
MK
106.SH ERRORS
107None.
3cdc8f57 108.SH ATTRIBUTES
cadbf54b
MK
109For an explanation of the terms used in this section, see
110.BR attributes (7).
111.TS
112allbox;
c466875e 113lbx lb lb
cadbf54b
MK
114l l l.
115Interface Attribute Value
116T{
9e54434e
BR
117.na
118.nh
3cdc8f57
PH
119.BR flockfile (),
120.BR ftrylockfile (),
3cdc8f57 121.BR funlockfile ()
cadbf54b
MK
122T} Thread safety MT-Safe
123.TE
c466875e 124.sp 1
3113c7f3 125.SH STANDARDS
4131356c
AC
126POSIX.1-2008.
127.SH HISTORY
128POSIX.1-2001.
bd168648 129.PP
8c4f34f8
MK
130These functions are available when
131.B _POSIX_THREAD_SAFE_FUNCTIONS
c13182ef 132is defined.
47297adb 133.SH SEE ALSO
fea681da 134.BR unlocked_stdio (3)