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