1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
4 .\" and Copyright 2002 Michael Kerrisk
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Fri Jan 31 16:26:07 1997 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Dec 11 17:57:27 1998 by Jamie Lokier <jamie@imbolc.ucc.ie>
28 .\" Modified 24 Apr 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Substantial rewrites and additions
30 .\" 2005-05-10 mtk, noted that lock conversions are not atomic.
32 .\" Japanese Version Copyright (c) 1996 Takeshi Ueno
33 .\" all rights reserved.
34 .\" Translated 1996-07-03, Takeshi Ueno <tueno@vio.co.jp>
35 .\" Modified 1997-12-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
36 .\" Modified 1999-08-14, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
37 .\" Modified 2002-09-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
38 .\" Modified 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
39 .\" Updated 2005-09-06, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
41 .TH FLOCK 2 2009-07-25 "Linux" "Linux Programmer's Manual"
43 .\"O flock \- apply or remove an advisory lock on an open file
45 flock \- ¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤ËÂФ¹¤ë¥¢¥É¥Ð¥¤¥¶¥ê¡¦¥í¥Ã¥¯¤ÎŬÍÑ¡¢²ò½ü¤ò¹Ô¤¦
48 .B #include <sys/file.h>
50 .BI "int flock(int " fd ", int " operation );
53 .\"O Apply or remove an advisory lock on the open file specified by
57 .\"O is one of the following:
58 ¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤Ë¥¢¥É¥Ð¥¤¥¶¥ê¡¦¥í¥Ã¥¯ (advisory lock) ¤ÎŬÍÑ
64 ¤Ë¤Ï°Ê²¼¤Î¤¤¤º¤ì¤«°ì¤Ä¤ò»ØÄꤹ¤ë:
68 .\"O Place a shared lock.
69 .\"O More than one process may hold a shared lock for a given file
71 ¶¦Í¥í¥Ã¥¯¤òŬÍѤ¹¤ë¡£ »ØÄꤷ¤¿¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ¡¢
72 °ì¤Ä°Ê¾å¤Î¥×¥í¥»¥¹¤¬Æ±»þ¤Ë¶¦Í¥í¥Ã¥¯¤òÊÝ»ý¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
75 .\"O Place an exclusive lock.
76 .\"O Only one process may hold an exclusive lock for a given
77 .\"O file at a given time.
78 ÇÓ¾¥í¥Ã¥¯¤òŬÍѤ¹¤ë¡£ »ØÄꤷ¤¿¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ¡¢
79 ¤¿¤À°ì¤Ä¤Î¥×¥í¥»¥¹¤À¤±¤¬Æ±»þ¤ËÇÓ¾¥í¥Ã¥¯¤òÊÝ»ý¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
82 .\"O Remove an existing lock held by this process.
83 ¤³¤Î¥×¥í¥»¥¹¤¬ÊÝ»ý¤·¤Æ¤¤¤ë´û¸¤Î¥í¥Ã¥¯¤ò²ò½ü¤¹¤ë¡£
88 .\"O may block if an incompatible lock is held by another process.
89 .\"O To make a nonblocking request, include
93 .\"O with any of the above operations.
95 ¤ò¸Æ¤Ó½Ð¤·¤¿¤È¤¤Ë¡¢»ØÄꤷ¤¿¥í¥Ã¥¯¼ïÊ̤Ȱۤʤë¥í¥Ã¥¯¤¬ÊÌ¥×¥í¥»¥¹¤Ë¤è¤Ã¤Æ
98 ¤ÏÄä»ß (block) ¤µ¤ì¤ë¤³¤È¤¬¤¢¤ë¡£
99 ÈóÄä»ß (nonblocking) ¥¿¥¤¥×¤ÎÍ×µá¤ò¹Ô¤¦¤¿¤á¤Ë¤Ï¡¢
100 ¾åµ¤ÎÁàºî (operation) ¤Ë
102 ¤òÏÀÍýϤηÁ¤Ç»ØÄꤹ¤ë¡£
104 .\"O A single file may not simultaneously have both shared and exclusive locks.
105 °ì¤Ä¤Î¥Õ¥¡¥¤¥ë¤Ë¶¦Í¥í¥Ã¥¯¤ÈÇÓ¾¥í¥Ã¥¯¤òƱ»þ¤ËÀßÄꤹ¤ë¤³¤È¤Ï¤Ç¤¤Ê¤¤¡£
107 .\"O Locks created by
109 .\"O are associated with an open file table entry.
110 .\"O This means that duplicate file descriptors (created by, for example,
114 .\"O refer to the same lock, and this lock may be modified
115 .\"O or released using any of these descriptors.
117 ¤Ë¤è¤Ã¤Æºî¤é¤ì¤ë¥í¥Ã¥¯¤Ï¡¢
118 ¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤Î¥Æ¡¼¥Ö¥ë¡¦¥¨¥ó¥È¥ê¤È´ØÏ¢ÉÕ¤±¤é¤ì¤ë¡£
119 ¤·¤¿¤¬¤Ã¤Æ¡¢¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ÎÊ£À½
123 ¤Ê¤É¤Ë¤è¤êºîÀ®¤µ¤ì¤ë) ¤ÏƱ¤¸¥í¥Ã¥¯¤ò»²¾È¤·¡¢
124 ¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤É¤ì¤ò»È¤Ã¤Æ¤â
125 ¤³¤Î¥í¥Ã¥¯¤òÊѹ¹¤·¤¿¤ê²òÊü¤·¤¿¤ê¤Ç¤¤ë¡£
126 .\"O Furthermore, the lock is released either by an explicit
128 .\"O operation on any of these duplicate descriptors, or when all
129 .\"O such descriptors have been closed.
130 ¤Þ¤¿¡¢¥í¥Ã¥¯¤Î²òÊü¤Ï¡¢
131 ¾åµ¤ÎÊ£¿ô¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î¤¤¤º¤ì¤«¤ËÂФ·¤Æ
134 Áàºî¤ò»Ø¼¨¤·¤¿¾ì¹ç¤«¡¢¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤¬¤¹¤Ù¤Æ
135 ÊĤ¸¤é¤ì¤¿¾ì¹ç¤Ë¹Ô¤ï¤ì¤ë¡£
137 .\"O If a process uses
139 .\"O (or similar) to obtain more than one descriptor for the same file,
140 .\"O these descriptors are treated independently by
142 .\"O An attempt to lock the file using one of these file descriptors
143 .\"O may be denied by a lock that the calling process has
144 .\"O already placed via another descriptor.
147 (¤â¤·¤¯¤ÏƱÍͤÎÊýË¡) ¤ò»È¤Ã¤ÆƱ¤¸¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ
148 Ê£¿ô¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò¼èÆÀ¤·¤¿¾ì¹ç¡¢
150 ¤Ï¤³¤ì¤éÊ£¿ô¤Î¥Ç¥£¥¹¥¯¥ê¥×¥¿¤ò³Æ¡¹ÆÈΩ¤Î¤â¤Î¤È¤·¤Æ°·¤¦¡£
151 ¤³¤ì¤é¤Î¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Î°ì¤Ä¤ò»È¤Ã¤Æ¥Õ¥¡¥¤¥ë¤ò¥í¥Ã¥¯¤·¤è¤¦¤È
152 ¤·¤¿ºÝ¡¢¤½¤Î¥í¥Ã¥¯Í×µá¤Ï¡¢¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤¬¤½¤Î¥Õ¥¡¥¤¥ë¤ÎÊ̤Î
153 ¥Ç¥£¥¹¥¯¥ê¥×¥¿·Ðͳ¤Ç¤¹¤Ç¤ËÀßÄꤷ¤Æ¤¤¤ë¥í¥Ã¥¯¤Ë¤è¤Ã¤ÆµñÈݤµ¤ì¤ë¾ì¹ç¤¬¤¢¤ë¡£
155 .\"O A process may only hold one type of lock (shared or exclusive)
159 .\"O calls on an already locked file will convert an existing lock to the new
161 °ì¤Ä¤Î¥×¥í¥»¥¹¤Ï¡¢°ì¤Ä¤Î¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ (¶¦Í¥í¥Ã¥¯¤ÈÇÓ¾¥í¥Ã¥¯¤Î¤¦¤Á)
162 ¤¤¤º¤ì¤«°ì¼ïÎà¤Î¥í¥Ã¥¯¤·¤«ÀßÄê¤Ç¤¤Ê¤¤¡£
163 ´û¤Ë¥í¥Ã¥¯¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ
165 ¤ò¸Æ¤Ó½Ð¤¹¤È¡¢´û¸¤Î¥í¥Ã¥¯¤ò¿·¤·¤¤¥í¥Ã¥¯¥â¡¼¥É¤ËÊѹ¹¤¹¤ë¤³¤È¤Ë¤Ê¤ë¡£
167 .\"O Locks created by
169 .\"O are preserved across an
172 ¤Ë¤è¤êºîÀ®¤µ¤ì¤¿¥í¥Ã¥¯¤Ï
176 .\"O A shared or exclusive lock can be placed on a file regardless of the
177 .\"O mode in which the file was opened.
178 ¶¦Í¥í¥Ã¥¯¤âÇÓ¾¥í¥Ã¥¯¤â¡¢¥Õ¥¡¥¤¥ë¤¬¤É¤Î¥â¡¼¥É¤Ç¥ª¡¼¥×¥ó¤µ¤ì¤¿¤«¤Ë
179 ´Ø·¸¤Ê¤¯Å¬ÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
180 .\"O .SH "RETURN VALUE"
182 .\"O On success, zero is returned.
183 .\"O On error, \-1 is returned, and
185 .\"O is set appropriately.
186 À®¸ù¤Î¾ì¹ç¡¢0 ¤¬ÊÖ¤µ¤ì¤ë¡£¥¨¥é¡¼¤Î¾ì¹ç¤Ï¡¢\-1 ¤¬ÊÖ¤µ¤ì¡¢
188 ¤ËŬÀÚ¤ÊÃͤ¬ÀßÄꤵ¤ì¤ë¡£
194 .\"O is not an open file descriptor.
196 ¤¬¥ª¡¼¥×¥ó¤µ¤ì¤¿¥Õ¥¡¥¤¥ë¡¦¥Ç¥£¥¹¥¯¥ê¥×¥¿¤Ç¤Ï¤Ê¤¤¡£
199 .\"O While waiting to acquire a lock, the call was interrupted by
200 .\"O delivery of a signal caught by a handler; see
202 ¥í¥Ã¥¯¤Î³ÍÆÀ¤òÂԤäƤ¤¤ë´Ö¤Ë¡¢¥Ï¥ó¥É¥é¤Ë¤è¤êÊ᪤µ¤ì¤¿¥·¥°¥Ê¥ë¤ò
216 .\"O The kernel ran out of memory for allocating lock records.
217 ¥í¥Ã¥¯¡¦¥ì¥³¡¼¥É¤ò³ä¤êÅö¤Æ¤ë¤¿¤á¤Î¥á¥â¥ê¤¬ÉÔ¤·¤Æ¤¤¤ë¡£
220 .\"O The file is locked and the
222 .\"O flag was selected.
223 »ØÄꤷ¤¿¥Õ¥¡¥¤¥ë¤¬¥í¥Ã¥¯¤µ¤ì¤Æ¤ª¤ê¡¢
225 ¥Õ¥é¥°¤¬»ØÄꤵ¤ì¤Æ¤¤¤ë¡£
226 .\"O .SH "CONFORMING TO"
230 .\"O call first appeared in 4.2BSD).
233 ¥³¡¼¥ë¤Ï 4.2BSD ¤ÇºÇ½é¤ËÅо줷¤¿)¡£
236 .\"O possibly implemented in terms of
238 .\"O appears on most Unix systems.
240 ¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¤â¤Î¤Ê¤É¤ò´Þ¤á¤ë¤È¡¢
242 ¤Îµ¡Ç½¤Ï¤Û¤È¤ó¤É¤Î Unix ¥·¥¹¥Æ¥à¤Ç¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
246 .\"O does not lock files over NFS.
249 .\"O instead: that does work over NFS, given a sufficiently recent version of
250 .\"O Linux and a server which supports locking.
252 ¤Ï NFS ¾å¤Î¥Õ¥¡¥¤¥ë¤Î¥í¥Ã¥¯¤ò¤·¤Ê¤¤¡£Âå¤ï¤ê¤Ë
254 ¤ò»ÈÍѤ¹¤ë¤³¤È¡£¤³¤ì¤Ë¤è¤ê¡¢½½Ê¬¤Ë¿·¤·¤¤¥Ð¡¼¥¸¥ç¥ó¤Î Linux ¤È¡¢¥í¥Ã¥¯µ¡Ç½¤ò
255 ¥µ¥Ý¡¼¥È¤·¤¿ NFS ¥µ¡¼¥Ð¤ò»ÈÍѤ¹¤ë¤³¤È¤Ë¤è¤ê¡¢NFS ¾å¤Ç¥í¥Ã¥¯¤¬¤Ç¤¤ë¡£
257 .\"O Since kernel 2.0,
259 .\"O is implemented as a system call in its own right rather
260 .\"O than being emulated in the GNU C library as a call to
262 kernel 2.0 °Ê¹ß¤Ç¤Ï¡¢
264 ¤Ï¡¢GNU C ¥é¥¤¥Ö¥é¥ê¤Ç¤Î
266 ¤ò¸Æ¤Ó½Ð¤·¤Æ¤Î¥¨¥ß¥å¥ì¡¼¥·¥ç¥ó¤Ç¤Ï¤Ê¤¯¡¢
267 ¤½¤ì¼«ÂΤ¬¥·¥¹¥Æ¥à¥³¡¼¥ë¤È¤·¤Æ¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¡£
268 .\"O This yields true BSD semantics:
269 .\"O there is no interaction between the types of lock
276 .\"O does not detect deadlock.
277 ¤³¤ì¤Ë¤è¤êÀµ¿¿ÀµÌäΠBSD ¤Ç¤ÎÆ°ºî¤¬Ã£À®¤µ¤ì¤ë:
281 ¤ÇŬÍѤµ¤ì¤ë¥í¥Ã¥¯¤Î¼ïÊ̤ˤÏÁê¸ßºîÍѤ¬¤Ê¤¯¤Ê¤ê¡¢
283 ¤¬¥Ç¥Ã¥É¥í¥Ã¥¯¤ò¸¡½Ð¤·¤Ê¤¯¤Ê¤ë¡£
286 .\"O places advisory locks only; given suitable permissions on a file,
287 .\"O a process is free to ignore the use of
289 .\"O and perform I/O on the file.
291 ¥¢¥É¥Ð¥¤¥¶¥ê¡¦¥í¥Ã¥¯¤À¤±¤òŬÍѤ¹¤ë¡£¤·¤¿¤¬¤Ã¤Æ¡¢¥Õ¥¡¥¤¥ë¤ËŬÀڤʥ¢¥¯¥»¥¹¸¢¤ò
292 ÉÕÍ¿¤·¤Æ¤¤¤ì¤Ð¡¢¥×¥í¥»¥¹¤Ï
294 ¤Î»ÈÍѤË̵»ë¤·¤Æ¡¢¥Õ¥¡¥¤¥ë¤Ø¤ÎÆþ½ÐÎϤò¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¡£
299 .\"O locks have different semantics with respect to forked processes and
304 ¤Ï fork ¤µ¤ì¤¿¥×¥í¥»¥¹¤È
307 .\"O On systems that implement
311 .\"O the semantics of
313 .\"O will be different from those described in this manual page.
317 ¤ò»È¤Ã¤Æ¼ÂÁõ¤·¤Æ¤¤¤ë¥·¥¹¥Æ¥à¤Ç¤Ï¡¢
319 ¤ÎÆ°ºî¤Ï¤³¤Î¥Þ¥Ë¥å¥¢¥ë¡¦¥Ú¡¼¥¸¤ËµºÜ¤µ¤ì¤Æ¤¤¤ë¤â¤Î¤È¤Ï°ã¤¦¤À¤í¤¦¡£
321 .\"O Converting a lock
322 .\"O (shared to exclusive, or vice versa) is not guaranteed to be atomic:
323 .\"O the existing lock is first removed, and then a new lock is established.
324 .\"O Between these two steps,
325 .\"O a pending lock request by another process may be granted,
326 .\"O with the result that the conversion either blocks, or fails if
329 .\"O (This is the original BSD behavior,
330 .\"O and occurs on many other implementations.)
331 .\"O .\" Kernel 2.5.21 changed things a little: during lock conversion
332 .\"O .\" it is now the highest priority process that will get the lock -- mtk
333 ¥í¥Ã¥¯¤ÎÊÑ´¹ (¶¦Í¥í¥Ã¥¯¤«¤éÇÓ¾¥í¥Ã¥¯¤Ø¡¢¤â¤·¤¯¤Ï¤½¤ÎÈ¿ÂÐ) ¤¬¥¢¥È¥ß¥Ã¥¯¤Ë
334 ¹Ô¤ï¤ì¤ë¤³¤È¤ÏÊݾڤµ¤ì¤Æ¤¤¤Ê¤¤: ´û¸¤Î¥í¥Ã¥¯¤¬¤Þ¤ººï½ü¤µ¤ì¡¢¤½¤ì¤«¤é¿·¤·¤¤
335 ¥í¥Ã¥¯¤¬ÀßÄꤵ¤ì¤ë¡£¤³¤Î 2¤Ä¤Î¥¹¥Æ¥Ã¥×¤Î´Ö¤Ë¡¢Â¾¤Î¥×¥í¥»¥¹¤«¤é¤Î½èÍýÂÔ¤Á¤Î
336 ¥í¥Ã¥¯Í׵᤬ǧ¤á¤é¤ì¤ë¤«¤â¤·¤ì¤º¡¢·ë²Ì¤È¤·¤ÆÊÑ´¹¤ÏÄä»ß (block) ¤·¤¿¤ê¡¢
338 ¤¬»ØÄꤵ¤ì¤¿¾ì¹ç¤Ë¤Ï) ¼ºÇÔ¤·¤¿¤ê¤¹¤ë¡£
339 (¤³¤ì¤Ï¸µ¡¹¤Î BSD ¤ÎÆ°ºî¤Ç¤¢¤ê¡¢Â¿¤¯¤Î¾¤Î¼ÂÁõ¤Ç¤âµ¯¤³¤ë¡£)
340 .\" ¥«¡¼¥Í¥ë 2.5.21 ¤Ç¾¯¤·Æ°ºî¤¬Êѹ¹¤µ¤ì¤¿: ¥í¥Ã¥¯ÊÑ´¹¤ÎºÇÃæ¤Ï
341 .\" ¤½¤Î¥í¥Ã¥¯¤ò¹Ô¤ª¤¦¤È¤·¤Æ¤¤¤ë¥×¥í¥»¥¹¤¬ºÇ¹âÍ¥Àè¤È¤Ê¤ë -- mtk
353 .\"O .I Documentation/filesystem/locks.txt
354 .\"O in the kernel source
355 .\"O .RI ( Documentation/locks.txt
356 .\"O in older kernels).
358 .I Documentation/filesystem/locks.txt
360 .IR Documentation/locks.txt )