OSDN Git Service

(split) LDP man-pages の original/ を v3.29 に更新。
[linuxjm/LDP_man-pages.git] / original / man3 / flockfile.3
1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .TH FLOCKFILE 3  2008-08-29 "" "Linux Programmer's Manual"
24 .SH NAME
25 flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
26 .SH SYNOPSIS
27 .nf
28 .B #include <stdio.h>
29 .sp
30 .BI "void flockfile(FILE *" filehandle );
31 .br
32 .BI "int ftrylockfile(FILE *" filehandle );
33 .br
34 .BI "void funlockfile(FILE *" filehandle );
35 .fi
36 .sp
37 .in -4n
38 Feature Test Macro Requirements for glibc (see
39 .BR feature_test_macros (7)):
40 .in
41 .ad l
42 .sp
43 All functions shown above:
44 .RS 4
45 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
46 _SVID_SOURCE || _POSIX_SOURCE
47 .RE
48 .ad b
49 .SH DESCRIPTION
50 The stdio functions are thread-safe.
51 This is achieved by assigning
52 to each
53 .I FILE
54 object a lockcount and (if the lockcount is nonzero)
55 an owning thread.
56 For each library call, these functions wait until the
57 .I FILE
58 object
59 is no longer locked by a different thread, then lock it, do the
60 requested I/O, and unlock the object again.
61 .LP
62 (Note: this locking has nothing to do with the file locking done
63 by functions like
64 .BR flock (2)
65 and
66 .BR lockf (3).)
67 .LP
68 All this is invisible to the C-programmer, but there may be two
69 reasons to wish for more detailed control.
70 On the one hand, maybe
71 a series of I/O actions by one thread belongs together, and should
72 not be interrupted by the I/O of some other thread.
73 On the other hand, maybe the locking overhead should be avoided
74 for greater efficiency.
75 .LP
76 To this end, a thread can explicitly lock the
77 .I FILE
78 object,
79 then do its series of I/O actions, then unlock.
80 This prevents
81 other threads from coming in between.
82 If the reason for doing
83 this was to achieve greater efficiency, one does the I/O with
84 the nonlocking versions of the stdio functions: with
85 .BR getc_unlocked (3)
86 and
87 .BR putc_unlocked (3)
88 instead of
89 .BR getc (3)
90 and
91 .BR putc (3).
92 .LP
93 The
94 .BR flockfile ()
95 function waits for \fI*filehandle\fP to be
96 no longer locked by a different thread, then makes the
97 current thread owner of \fI*filehandle\fP, and increments
98 the lockcount.
99 .LP
100 The
101 .BR funlockfile ()
102 function decrements the lock count.
103 .LP
104 The
105 .BR ftrylockfile ()
106 function is a nonblocking version
107 of
108 .BR flockfile ().
109 It does nothing in case some other thread
110 owns \fI*filehandle\fP, and it obtains ownership and increments
111 the lockcount otherwise.
112 .SH "RETURN VALUE"
113 The
114 .BR ftrylockfile ()
115 function returns zero for success
116 (the lock was obtained), and nonzero for failure.
117 .SH ERRORS
118 None.
119 .SH "CONFORMING TO"
120 POSIX.1-2001.
121 .SH AVAILABILITY
122 These functions are available when
123 .B _POSIX_THREAD_SAFE_FUNCTIONS
124 is defined.
125 They are in libc since libc 5.1.1 and in glibc
126 since glibc 2.0.
127 .SH "SEE ALSO"
128 .BR unlocked_stdio (3)