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