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:
60 _POSIX_C_SOURCE\ >=\ 1 || _XOPEN_SOURCE || _BSD_SOURCE ||
61 _SVID_SOURCE || _POSIX_SOURCE
66 .\"O The stdio functions are thread-safe.
67 .\"O This is achieved by assigning
70 .\"O object a lockcount and (if the lockcount is nonzero)
71 .\"O an owning thread.
72 ɸ½àÆþ½ÐÎÏ´Ø¿ô¤Ï¥¹¥ì¥Ã¥É¥»¡¼¥Õ¤Ç¤¢¤ë¡£¤³¤ì¤Ï¡¢³Æ
74 ¥ª¥Ö¥¸¥§¥¯¥È¤ËÂФ·¡¢¥í¥Ã¥¯¿ô (lockcount) ¤È
75 (¥í¥Ã¥¯¿ô¤¬ 0 ¤Ç¤Ê¤¤¾ì¹ç¤Ï) ½êͼԥ¹¥ì¥Ã¥É (owner thread)
76 ¤ò´ÉÍý¤¹¤ë¤³¤È¤Ç¼Â¸½¤µ¤ì¤ë¡£
77 .\"O For each library call, these functions wait until the
80 .\"O is no longer locked by a different thread, then lock it, do the
81 .\"O requested I/O, and unlock the object again.
82 ¥é¥¤¥Ö¥é¥ê¤Î¸Æ¤Ó½Ð¤·¤¬¹Ô¤ï¤ì¤ëËè¤Ë¡¢É¸½àÆþ½ÐÎÏ´Ø¿ô¤Ï
84 ¥ª¥Ö¥¸¥§¥¯¥È¤¬Â¾¤Î¥¹¥ì¥Ã¥É¤Ë¤è¤Ã¤Æ¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤Ê¤¤¾õÂ֤ˤʤë¤Þ¤ÇÂÔ¤Á¡¢
86 ¥ª¥Ö¥¸¥§¥¯¥È¤ò¥í¥Ã¥¯¤·¡¢Í׵ᤵ¤ì¤ÆÆþ½ÐÎϤò¹Ô¤¤¡¢
87 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥í¥Ã¥¯¤ò²ò½ü¤¹¤ë¡£
89 .\"O (Note: this locking has nothing to do with the file locking done
90 .\"O by functions like
98 ¤È¤¤¤Ã¤¿´Ø¿ô¤¬¹Ô¤¦¥í¥Ã¥¯¤È¤ÏÁ´¤¯Ìµ´Ø·¸¤Ç¤¢¤ë¡£)
100 .\"O All this is invisible to the C-programmer, but there may be two
101 .\"O reasons to wish for more detailed control.
102 .\"O On the one hand, maybe
103 .\"O a series of I/O actions by one thread belongs together, and should
104 .\"O not be interrupted by the I/O of some other thread.
105 .\"O On the other hand, maybe the locking overhead should be avoided
106 .\"O for greater efficiency.
107 ¤³¤ì¤é¤Î¤³¤È¤Ï¤¹¤Ù¤Æ C ¥×¥í¥°¥é¥Þ¤Ë¤Ï¸«¤¨¤Ê¤¤Éôʬ¤Ç¹Ô¤ï¤ì¤ë¤¬¡¢
108 ¤è¤êºÙ¤«¤¤À©¸æ¤¬¤Ç¤¤¿Êý¤¬¤è¤¤Íýͳ¤¬2¤Ä¤¢¤ë¤À¤í¤¦¡£°ì¤Ä¤Ï¡¢°ì¤Ä¤Î¥¹¥ì¥Ã¥É¤¬
109 ¹Ô¤¦°ìÏ¢¤ÎÆþ½ÐÎÏÆ°ºî¤Ï°ì½ï¤Ë¹Ô¤ï¤ì¡¢Â¾¤Î¥¹¥ì¥Ã¥É¤ÎÆþ½ÐÎϤˤè¤Ã¤ÆÃæÃǤµ¤ì¤Ê¤¤
110 Êý¤¬¤è¤¤¤È¤¤¤¦¤³¤È¤Ç¤¢¤í¤¦¡£¤â¤¦°ì¤Ä¤Ï¡¢¸úΨ¤òÂ礤¯¾å¤²¤ë¤¿¤á¤Ë¤Ï
111 ¥í¥Ã¥¯¤Î¥ª¡¼¥Ð¥Ø¥Ã¥É¤òÈò¤±¤ëɬÍפ¬¤¢¤ë¤È¤¤¤¦¤³¤È¤Ç¤¢¤í¤¦¡£
113 .\"O To this end, a thread can explicitly lock the
116 .\"O then do its series of I/O actions, then unlock.
118 .\"O other threads from coming in between.
119 .\"O If the reason for doing
120 .\"O this was to achieve greater efficiency, one does the I/O with
121 .\"O the nonlocking versions of the stdio functions: with
122 .\"O .BR getc_unlocked (3)
124 .\"O .BR putc_unlocked (3)
129 ¤³¤ÎÌÜŪ¤ò¼Â¸½¤¹¤ë¤¿¤á¤Ë¡¢
131 ¥ª¥Ö¥¸¥§¥¯¥È¤Î¥í¥Ã¥¯¡¢°ìÏ¢¤ÎÆþ½ÐÎÏÆ°ºî¤Î¼Â¹Ô¡¢
132 ¥í¥Ã¥¯¤Î²ò½ü¤ò¥¹¥ì¥Ã¥É¤¬ÌÀ¼¨Åª¤Ë»Ø¼¨¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
133 ¤³¤ì¤Ë¤è¤ê¡¢Â¾¤Î¥¹¥ì¥Ã¥É¤¬ÅÓÃæ¤ÇÆþ½ÐÎϤò¹Ô¤¦¤Î¤òËɻߤ¹¤ë¡£
134 ¤³¤Î¤è¤¦¤Ê¤³¤È¤ò¹Ô¤¦Íýͳ¤¬¸úΨ¤Î¸þ¾å¤Ç¤¢¤ë¤Ê¤é¤Ð¡¢
135 ¥í¥Ã¥¯¤ò¹Ô¤ï¤Ê¤¤¥Ð¡¼¥¸¥ç¥ó¤Îɸ½àÆþ½ÐÎÏ´Ø¿ô¤ò»È¤¦¤³¤È¤â¤Ç¤¤ë¡£
141 .BR getc_unlocked (3)
143 .BR putc_unlocked (3)
147 .\"O .BR flockfile ()
148 .\"O function waits for \fI*filehandle\fP to be
149 .\"O no longer locked by a different thread, then makes the
150 .\"O current thread owner of \fI*filehandle\fP, and increments
153 ´Ø¿ô¤Ï¡¢\fI*filehandle\fP ¤¬Â¾¤Î¥¹¥ì¥Ã¥É¤Ë¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤Ê
154 ¤¤¾õÂ֤ˤʤë¤Þ¤ÇÂԤ俤ΤÁ¡¢¸½ºß¤Î¥¹¥ì¥Ã¥É¤ò \fI*filehandle\fP ¤Î¥ª¡¼¥Ê¤ËÀß
155 Äꤷ¡¢¥í¥Ã¥¯¿ô¤ò²Ã»»¤¹¤ë¡£
158 .\"O .BR funlockfile ()
159 .\"O function decrements the lock count.
161 ´Ø¿ô¤Ï¡¢¥í¥Ã¥¯¿ô¤ò¸º»»¤¹¤ë¡£
164 .\"O .BR ftrylockfile ()
165 .\"O function is a nonblocking version
167 .\"O .BR flockfile ().
168 .\"O It does nothing in case some other thread
169 .\"O owns \fI*filehandle\fP, and it obtains ownership and increments
170 .\"O the lockcount otherwise.
174 ¤Î¥Ö¥í¥Ã¥¥ó¥°¤ò¹Ô¤ï¤Ê¤¤
175 ¥Ð¡¼¥¸¥ç¥ó¤Ç¤¢¤ë¡£Â¾¤Î¥¹¥ì¥Ã¥É¤¬ \fI*filehandle\fP ¤ò¥í¥Ã¥¯¤·¤Æ¤¤¤ë»þ¤Ï
176 ²¿¤â¹Ô¤ï¤º¡¢¤½¤¦¤Ç¤Ê¤¤¾ì¹ç¤Ï \fI*filehandle\fP ¤Î½ê͸¢¤ò³ÍÆÀ¤·¡¢
178 .\"O .SH "RETURN VALUE"
181 .\"O .BR ftrylockfile ()
182 .\"O function returns zero for success
183 .\"O (the lock was obtained), and nonzero for failure.
185 ´Ø¿ô¤Ï¥í¥Ã¥¯¤ËÀ®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢
186 ¼ºÇÔ¤·¤¿¾ì¹ç¤Ï 0 °Ê³°¤ÎÃͤòÊÖ¤¹¡£
191 .\"O .SH "CONFORMING TO"
194 .\"O .SH AVAILABILITY
196 .\"O These functions are available when
197 .\"O .B _POSIX_THREAD_SAFE_FUNCTIONS
199 .\"O They are in libc since libc 5.1.1 and in glibc
200 .\"O since glibc 2.0.
201 .B _POSIX_THREAD_SAFE_FUNCTIONS
202 ¤¬ÄêµÁ¤µ¤ì¤Æ¤¤¤ë¤È¤¤Ë¤³¤ì¤é¤Î´Ø¿ô¤ò»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
203 5.1.1 °Ê¹ß¤Î libc ¤È 2.0 °Ê¹ß¤Î glibc ¤Ë¸ºß¤¹¤ë¡£
206 .BR unlocked_stdio (3)