1 .\" Copyright (C) 2001 Andries Brouwer <aeb@cwi.nl>.
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Japanese Version Copyright (c) 2001 Yuichi SATO
24 .\" all rights reserved.
25 .\" Translated Sun Nov 4 14:09:45 2001
26 .\" by Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\"WORD: lockcount ¥í¥Ã¥¯¿ô
29 .\"WORD: owner thread ½êͼԥ¹¥ì¥Ã¥É
31 .TH FLOCKFILE 3 2008-08-29 "" "Linux Programmer's Manual"
33 .\"O flockfile, ftrylockfile, funlockfile \- lock FILE for stdio
35 flockfile, ftrylockfile, funlockfile \- ɸ½àÆþ½ÐÎÏ FILE ¤Î¥í¥Ã¥¯¤ò¹Ô¤¦
41 .BI "void flockfile(FILE *" filehandle );
43 .BI "int ftrylockfile(FILE *" filehandle );
45 .BI "void funlockfile(FILE *" filehandle );
49 .\"O Feature Test Macro Requirements for glibc (see
50 .\"O .BR feature_test_macros (7)):
51 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
52 .RB ( feature_test_macros (7)
57 .\"O All functions shown above:
59 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
60 _SVID_SOURCE || _POSIX_SOURCE
64 .\"O The stdio functions are thread-safe.
65 .\"O This is achieved by assigning
68 .\"O object a lockcount and (if the lockcount is nonzero)
69 .\"O an owning thread.
70 ɸ½àÆþ½ÐÎÏ´Ø¿ô¤Ï¥¹¥ì¥Ã¥É¥»¡¼¥Õ¤Ç¤¢¤ë¡£¤³¤ì¤Ï¡¢³Æ
72 ¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ·¡¢¥í¥Ã¥¯¿ô (lockcount) ¤È
73 (¥í¥Ã¥¯¿ô¤¬ 0 ¤Ç¤Ê¤¤¾ì¹ç¤Ï) ½êͼԥ¹¥ì¥Ã¥É (owner thread)
74 ¤ò´ÉÍý¤¹¤ë¤³¤È¤Ç¼Â¸½¤µ¤ì¤ë¡£
75 .\"O For each library call, these functions wait until the
78 .\"O is no longer locked by a different thread, then lock it, do the
79 .\"O requested I/O, and unlock the object again.
80 ¥é¥¤¥Ö¥é¥ê¤Î¸Æ¤Ó½Ð¤·¤¬¹Ô¤ï¤ì¤ëËè¤Ë¡¢É¸½àÆþ½ÐÎÏ´Ø¿ô¤Ï
82 ¥ª¥Ö¥¸¥§¥¯¥È¤¬Â¾¤Î¥¹¥ì¥Ã¥É¤Ë¤è¤Ã¤Æ¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤Ê¤¤¾õÂ֤ˤʤë¤Þ¤ÇÂÔ¤Á¡¢
84 ¥ª¥Ö¥¸¥§¥¯¥È¤ò¥í¥Ã¥¯¤·¡¢Í׵ᤵ¤ì¤ÆÆþ½ÐÎϤò¹Ô¤¤¡¢
85 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥í¥Ã¥¯¤ò²ò½ü¤¹¤ë¡£
87 .\"O (Note: this locking has nothing to do with the file locking done
88 .\"O by functions like
96 ¤È¤¤¤Ã¤¿´Ø¿ô¤¬¹Ô¤¦¥í¥Ã¥¯¤È¤ÏÁ´¤¯Ìµ´Ø·¸¤Ç¤¢¤ë¡£)
98 .\"O All this is invisible to the C-programmer, but there may be two
99 .\"O reasons to wish for more detailed control.
100 .\"O On the one hand, maybe
101 .\"O a series of I/O actions by one thread belongs together, and should
102 .\"O not be interrupted by the I/O of some other thread.
103 .\"O On the other hand, maybe the locking overhead should be avoided
104 .\"O for greater efficiency.
105 ¤³¤ì¤é¤Î¤³¤È¤Ï¤¹¤Ù¤Æ C ¥×¥í¥°¥é¥Þ¤Ë¤Ï¸«¤¨¤Ê¤¤Éôʬ¤Ç¹Ô¤ï¤ì¤ë¤¬¡¢
106 ¤è¤êºÙ¤«¤¤À©¸æ¤¬¤Ç¤¤¿Êý¤¬¤è¤¤Íýͳ¤¬2¤Ä¤¢¤ë¤À¤í¤¦¡£°ì¤Ä¤Ï¡¢°ì¤Ä¤Î¥¹¥ì¥Ã¥É¤¬
107 ¹Ô¤¦°ìÏ¢¤ÎÆþ½ÐÎÏÆ°ºî¤Ï°ì½ï¤Ë¹Ô¤ï¤ì¡¢Â¾¤Î¥¹¥ì¥Ã¥É¤ÎÆþ½ÐÎϤˤè¤Ã¤ÆÃæÃǤµ¤ì¤Ê¤¤
108 Êý¤¬¤è¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤í¤¦¡£¤â¤¦°ì¤Ä¤Ï¡¢¸úΨ¤òÂ礤¯¾å¤²¤ë¤¿¤á¤Ë¤Ï
109 ¥í¥Ã¥¯¤Î¥ª¡¼¥Ð¥Ø¥Ã¥É¤òÈò¤±¤ëɬÍפ¬¤¢¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤í¤¦¡£
111 .\"O To this end, a thread can explicitly lock the
114 .\"O then do its series of I/O actions, then unlock.
116 .\"O other threads from coming in between.
117 .\"O If the reason for doing
118 .\"O this was to achieve greater efficiency, one does the I/O with
119 .\"O the nonlocking versions of the stdio functions: with
120 .\"O .BR getc_unlocked (3)
122 .\"O .BR putc_unlocked (3)
127 ¤³¤ÎÌÜŪ¤ò¼Â¸½¤¹¤ë¤¿¤á¤Ë¡¢
129 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥í¥Ã¥¯¡¢°ìÏ¢¤ÎÆþ½ÐÎÏÆ°ºî¤Î¼Â¹Ô¡¢
130 ¥í¥Ã¥¯¤Î²ò½ü¤ò¥¹¥ì¥Ã¥É¤¬ÌÀ¼¨Åª¤Ë»Ø¼¨¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
131 ¤³¤ì¤Ë¤è¤ê¡¢Â¾¤Î¥¹¥ì¥Ã¥É¤¬ÅÓÃæ¤ÇÆþ½ÐÎϤò¹Ô¤¦¤Î¤òËɻߤ¹¤ë¡£
132 ¤³¤Î¤è¤¦¤Ê¤³¤È¤ò¹Ô¤¦Íýͳ¤¬¸úΨ¤Î¸þ¾å¤Ç¤¢¤ë¤Ê¤é¤Ð¡¢
133 ¥í¥Ã¥¯¤ò¹Ô¤ï¤Ê¤¤¥Ð¡¼¥¸¥ç¥ó¤Îɸ½àÆþ½ÐÎÏ´Ø¿ô¤ò»È¤¦¤³¤È¤â¤Ç¤¤ë¡£
139 .BR getc_unlocked (3)
141 .BR putc_unlocked (3)
145 .\"O .BR flockfile ()
146 .\"O function waits for \fI*filehandle\fP to be
147 .\"O no longer locked by a different thread, then makes the
148 .\"O current thread owner of \fI*filehandle\fP, and increments
151 ´Ø¿ô¤Ï¡¢\fI*filehandle\fP ¤¬Â¾¤Î¥¹¥ì¥Ã¥É¤Ë¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤Ê
152 ¤¤¾õÂ֤ˤʤë¤Þ¤ÇÂԤ俤ΤÁ¡¢¸½ºß¤Î¥¹¥ì¥Ã¥É¤ò \fI*filehandle\fP ¤Î¥ª¡¼¥Ê¤ËÀß
153 Äꤷ¡¢¥í¥Ã¥¯¿ô¤ò²Ã»»¤¹¤ë¡£
156 .\"O .BR funlockfile ()
157 .\"O function decrements the lock count.
159 ´Ø¿ô¤Ï¡¢¥í¥Ã¥¯¿ô¤ò¸º»»¤¹¤ë¡£
162 .\"O .BR ftrylockfile ()
163 .\"O function is a nonblocking version
165 .\"O .BR flockfile ().
166 .\"O It does nothing in case some other thread
167 .\"O owns \fI*filehandle\fP, and it obtains ownership and increments
168 .\"O the lockcount otherwise.
172 ¤Î¥Ö¥í¥Ã¥¥ó¥°¤ò¹Ô¤ï¤Ê¤¤
173 ¥Ð¡¼¥¸¥ç¥ó¤Ç¤¢¤ë¡£Â¾¤Î¥¹¥ì¥Ã¥É¤¬ \fI*filehandle\fP ¤ò¥í¥Ã¥¯¤·¤Æ¤¤¤ë»þ¤Ï
174 ²¿¤â¹Ô¤ï¤º¡¢¤½¤¦¤Ç¤Ê¤¤¾ì¹ç¤Ï \fI*filehandle\fP ¤Î½ê͸¢¤ò³ÍÆÀ¤·¡¢
176 .\"O .SH "RETURN VALUE"
179 .\"O .BR ftrylockfile ()
180 .\"O function returns zero for success
181 .\"O (the lock was obtained), and nonzero for failure.
183 ´Ø¿ô¤Ï¥í¥Ã¥¯¤ËÀ®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢
184 ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï 0 °Ê³°¤ÎÃͤòÊÖ¤¹¡£
189 .\"O .SH "CONFORMING TO"
192 .\"O .SH AVAILABILITY
194 .\"O These functions are available when
195 .\"O .B _POSIX_THREAD_SAFE_FUNCTIONS
197 .\"O They are in libc since libc 5.1.1 and in glibc
198 .\"O since glibc 2.0.
199 .B _POSIX_THREAD_SAFE_FUNCTIONS
200 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¤È¤¤Ë¤³¤ì¤é¤Î´Ø¿ô¤ò»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
201 5.1.1 °Ê¹ß¤Î libc ¤È 2.0 °Ê¹ß¤Î glibc ¤Ë¸ºß¤¹¤ë¡£
204 .BR unlocked_stdio (3)