OSDN Git Service

8265003d07b2393e183a0fe27d8b394573e31b4b
[linuxjm/LDP_man-pages.git] / original / man3 / flockfile.3
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
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.
12 .\"
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.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH FLOCKFILE 3  2013-07-23 "" "Linux Programmer's Manual"
26 .SH NAME
27 flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
28 .SH SYNOPSIS
29 .nf
30 .B #include <stdio.h>
31 .sp
32 .BI "void flockfile(FILE *" filehandle );
33 .br
34 .BI "int ftrylockfile(FILE *" filehandle );
35 .br
36 .BI "void funlockfile(FILE *" filehandle );
37 .fi
38 .sp
39 .in -4n
40 Feature Test Macro Requirements for glibc (see
41 .BR feature_test_macros (7)):
42 .in
43 .ad l
44 .sp
45 All functions shown above:
46 .RS 4
47 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
48 _SVID_SOURCE || _POSIX_SOURCE
49 .RE
50 .ad b
51 .SH DESCRIPTION
52 The stdio functions are thread-safe.
53 This is achieved by assigning
54 to each
55 .I FILE
56 object a lockcount and (if the lockcount is nonzero)
57 an owning thread.
58 For each library call, these functions wait until the
59 .I FILE
60 object
61 is no longer locked by a different thread, then lock it, do the
62 requested I/O, and unlock the object again.
63 .LP
64 (Note: this locking has nothing to do with the file locking done
65 by functions like
66 .BR flock (2)
67 and
68 .BR lockf (3).)
69 .LP
70 All this is invisible to the C-programmer, but there may be two
71 reasons to wish for more detailed control.
72 On the one hand, maybe
73 a series of I/O actions by one thread belongs together, and should
74 not be interrupted by the I/O of some other thread.
75 On the other hand, maybe the locking overhead should be avoided
76 for greater efficiency.
77 .LP
78 To this end, a thread can explicitly lock the
79 .I FILE
80 object,
81 then do its series of I/O actions, then unlock.
82 This prevents
83 other threads from coming in between.
84 If the reason for doing
85 this was to achieve greater efficiency, one does the I/O with
86 the nonlocking versions of the stdio functions: with
87 .BR getc_unlocked (3)
88 and
89 .BR putc_unlocked (3)
90 instead of
91 .BR getc (3)
92 and
93 .BR putc (3).
94 .LP
95 The
96 .BR flockfile ()
97 function waits for
98 .I *filehandle
99 to be
100 no longer locked by a different thread, then makes the
101 current thread owner of
102 .IR *filehandle ,
103 and increments
104 the lockcount.
105 .LP
106 The
107 .BR funlockfile ()
108 function decrements the lock count.
109 .LP
110 The
111 .BR ftrylockfile ()
112 function is a nonblocking version
113 of
114 .BR flockfile ().
115 It does nothing in case some other thread
116 owns
117 .IR *filehandle ,
118 and it obtains ownership and increments
119 the lockcount otherwise.
120 .SH RETURN VALUE
121 The
122 .BR ftrylockfile ()
123 function returns zero for success
124 (the lock was obtained), and nonzero for failure.
125 .SH ERRORS
126 None.
127 .SH ATTRIBUTES
128 .SS Multithreading (see pthreads(7))
129 The
130 .BR flockfile (),
131 .BR ftrylockfile (),
132 and
133 .BR funlockfile ()
134 functions are thread-safe.
135 .SH CONFORMING TO
136 POSIX.1-2001.
137 .SH AVAILABILITY
138 These functions are available when
139 .B _POSIX_THREAD_SAFE_FUNCTIONS
140 is defined.
141 They are in libc since libc 5.1.1 and in glibc
142 since glibc 2.0.
143 .SH SEE ALSO
144 .BR unlocked_stdio (3)
145 .SH COLOPHON
146 This page is part of release 3.67 of the Linux
147 .I man-pages
148 project.
149 A description of the project,
150 information about reporting bugs,
151 and the latest version of this page,
152 can be found at
153 \%http://www.kernel.org/doc/man\-pages/.