2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
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 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
27 .\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\" Updated 2008-08-07, Akihiro MOTOKI, LDP v3.05
29 .\" Updated 2009-02-23, Akihiro MOTOKI, LDP v3.19
31 .TH SEM_OVERVIEW 7 2009-02-25 "Linux" "Linux Programmer's Manual"
34 .\"O sem_overview \- Overview of POSIX semaphores
35 sem_overview \- POSIX ¥»¥Þ¥Õ¥©¤Î³µÍ×
38 .\"O POSIX semaphores allow processes and threads to synchronize their actions.
39 POSIX ¥»¥Þ¥Õ¥©¤ò»ÈÍѤ¹¤ë¤È¡¢¥×¥í¥»¥¹¤ä¥¹¥ì¥Ã¥É´Ö¤Ç¤½¤ÎÆ°ºî¤ò
40 Ʊ´ü¤µ¤»¤ë¤³¤È¤¬¤Ç¤¤ë¡£
42 .\"O A semaphore is an integer whose value is never allowed to fall below zero.
43 .\"O Two operations can be performed on semaphores:
44 .\"O increment the semaphore value by one
45 .\"O .RB ( sem_post (3));
46 .\"O and decrement the semaphore value by one
47 .\"O .RB ( sem_wait (3)).
48 .\"O If the value of a semaphore is currently zero, then a
50 .\"O operation will block until the value becomes greater than zero.
51 ¥»¥Þ¥Õ¥©¤ÏÀ°¿ô¤Ç¤¢¤ê¡¢¤½¤ÎÃͤϷ褷¤Æ 0 ̤Ëþ¤Ë¤Ê¤ë¤³¤È¤Ïµö¤µ¤ì¤Ê¤¤¡£
52 ¥»¥Þ¥Õ¥©¤ËÂФ·¤Æ¤Ç¤¤ëÁàºî¤Ï 2 ¤Ä¤Ç¤¢¤ë:
57 ¥»¥Þ¥Õ¥©¤ÎÃͤ¬¤¹¤Ç¤Ë 0 ¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©Ãͤ¬ 0 ¤è¤êÂ礤¯¤Ê¤ë¤Þ¤Ç
59 Áàºî¤ÏÄä»ß (block) ¤¹¤ë¡£
61 .\"O POSIX semaphores come in two forms: named semaphores and
62 .\"O unnamed semaphores.
63 POSIX ¥»¥Þ¥Õ¥©¤Ë¤Ï¡¢Ì¾Á°ÉÕ¤¥»¥Þ¥Õ¥© (named semaphore) ¤È
64 ̾Á°¤Ê¤·¥»¥Þ¥Õ¥© (unnamed semaphore) ¤Î 2¤Ä¤Î·Á¤¬¤¢¤ë¡£
66 .\"O .B Named semaphores
68 .\"O A named semaphore is identified by a name of the form
70 .\"O that is, a null-terminated string of up to
72 .\"O (i.e., 251) characters consisting of an initial slash,
75 ¤È¤¤¤¦·Á¼°¤Î̾Á°¤Ç¼±Ê̤µ¤ì¤ë¡£
78 (¤¹¤Ê¤ï¤Á 251) ʸ»ú¤Î NULL ½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ç¡¢
79 .\" glibc allows the initial slash to be omitted, and makes
80 .\" multiple initial slashes equivalent to a single slash.
81 .\" This differs from the implementation of POSIX message queues.
82 .\"O followed by one or more characters, none of which are slashes.
83 ¥¹¥é¥Ã¥·¥å¤Ç»Ï¤Þ¤ê¡¢¥¹¥é¥Ã¥·¥å°Ê³°¤Îʸ»ú¤¬ 1 ʸ»ú°Ê¾å³¤¯·Á¼°¤Ç¤¢¤ë¡£
84 .\" glibc allows subdirectory components in the name, in which
85 .\" case the subdirectory tree must exist under /dev/shm, and
86 .\" the fist subdirectory component must exist as the name
87 .\" sem.name, and all of the subdirectory components must allow the
88 .\" required permissions if a user wants to create a semaphore
89 .\" object in a subdirectory.
90 .\"O Two processes can operate on the same named semaphore by passing
92 .\"O .BR sem_open (3).
94 ¤ËƱ¤¸Ì¾Á°¤òÅϤ¹¤³¤È¤Ë¤è¤ê¡¢2 ¤Ä¤Î¥×¥í¥»¥¹´Ö¤ÇƱ¤¸Ì¾Á°¤Î¥»¥Þ¥Õ¥©
95 ¤ËÂФ·Áàºî¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¡£
99 .\"O function creates a new named semaphore or opens an existing
100 .\"O named semaphore.
101 .\"O After the semaphore has been opened, it can be operated on using
102 .\"O .BR sem_post (3)
104 .\"O .BR sem_wait (3).
105 .\"O When a process has finished using the semaphore, it can use
106 .\"O .BR sem_close (3)
107 .\"O to close the semaphore.
108 .\"O When all processes have finished using the semaphore,
109 .\"O it can be removed from the system using
110 .\"O .BR sem_unlink (3).
112 ´Ø¿ô¤Ï¡¢¿·¤·¤¤Ì¾Á°ÉÕ¤¥»¥Þ¥Õ¥©¤òºîÀ®¤¹¤ë¤«¡¢´û¤Ë¸ºß¤¹¤ë̾Á°ÉÕ¤
113 ¥»¥Þ¥Õ¥©¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
114 ¥»¥Þ¥Õ¥©¤ò¥ª¡¼¥×¥ó¤·¤¿¸å¤Ï¡¢
118 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÁàºî¤Ç¤¤ë¡£
119 ¥×¥í¥»¥¹¤¬¥»¥Þ¥Õ¥©¤Î»ÈÍѤò½ª¤¨¤¿ºÝ¤Ï¡¢
121 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤ò¥¯¥í¡¼¥º¤Ç¤¤ë¡£
122 ¤¢¤ë¥»¥Þ¥Õ¥©¤ò¤É¤Î¥×¥í¥»¥¹¤â»ÈÍѤ·¤Ê¤¯¤Ê¤ë¤È¡¢
124 ¤ò»È¤Ã¤Æ¤½¤Î¥»¥Þ¥Õ¥©¤ò¥·¥¹¥Æ¥à¤«¤éºï½ü¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
126 .\"O .B Unnamed semaphores (memory-based semaphores)
127 .B ̾Á°¤Ê¤·¥»¥Þ¥Õ¥© (¥á¥â¥ê¥Ù¡¼¥¹¡¦¥»¥Þ¥Õ¥©)
128 .\"O An unnamed semaphore does not have a name.
129 .\"O Instead the semaphore is placed in a region of memory that
130 .\"O is shared between multiple threads (a
131 .\"O .IR "thread-shared semaphore" )
133 .\"O .IR "process-shared semaphore" ).
134 .\"O A thread-shared semaphore is placed in an area of memory shared
135 .\"O between by the threads of a process, for example, a global variable.
136 .\"O A process-shared semaphore must be placed in a shared memory region
137 .\"O (e.g., a System V shared memory segment created using
139 .\"O or a POSIX shared memory object built created using
140 .\"O .BR shm_open (3)).
141 ̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤Ï̾Á°¤ò»ý¤¿¤Ê¤¤¡£¤½¤ÎÂå¤ï¤ê¡¢¥»¥Þ¥Õ¥©¤Ï¡¢
142 Ê£¿ô¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í¤µ¤ì¤¿¥á¥â¥êÎΰ衢¤â¤·¤¯¤ÏÊ£¿ô¥×¥í¥»¥¹´Ö¤Ç
143 ¶¦Í¤µ¤ì¤¿¥á¥â¥êÎΰè¤ËÃÖ¤«¤ì¤ë (Á°¼Ô¤ò
144 .IR "¥¹¥ì¥Ã¥É¶¦Í¥»¥Þ¥Õ¥© (thread-shared semaphore)" ¡¢
146 .IR "¥×¥í¥»¥¹¶¦Í¥»¥Þ¥Õ¥© (process-shared semaphore)"
147 ¤È¸Æ¤Ö)¡£¥¹¥ì¥Ã¥É¶¦Í¥»¥Þ¥Õ¥©¤Ï¡¢Æ±¤¸¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í¤µ¤ì¤¿
148 ¥á¥â¥êÎΰ衢Î㤨¤ÐÂç°èÊÑ¿ô (global variable) ¤ËÇÛÃÖ¤µ¤ì¤ë¡£
149 ¥×¥í¥»¥¹¶¦Í¥»¥Þ¥Õ¥©¤Ï¡¢¶¦Í¥á¥â¥êÎΰè (Î㤨¤Ð¡¢
151 ¤ò»È¤Ã¤ÆºîÀ®¤Ç¤¤ë System V ¶¦Í¥á¥â¥ê¡¦¥»¥°¥á¥ó¥È¤ä
153 ¤ò»È¤Ã¤ÆºîÀ®¤Ç¤¤ë POSIX ¶¦Í¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È)
154 Æâ¤ËÇÛÃÖ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
156 .\"O Before being used, an unnamed semaphore must be initialized using
157 .\"O .BR sem_init (3).
158 .\"O It can then be operated on using
159 .\"O .BR sem_post (3)
161 .\"O .BR sem_wait (3).
162 .\"O When the semaphore is no longer required,
163 .\"O and before the memory in which it is located is deallocated,
164 .\"O the semaphore should be destroyed using
165 .\"O .BR sem_destroy (3).
166 ̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤Ï¡¢»ÈÍѤ¹¤ëÁ°¤Ë
168 ¤ò»È¤Ã¤Æ½é´ü²½¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
174 ¥»¥Þ¥Õ¥©¤¬¤â¤Ï¤äɬÍפʤ¯¤Ê¤Ã¤¿¤È¤¤ä¡¢
175 ¥»¥Þ¥Õ¥©¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¥á¥â¥ê¤ò²òÊü¤¹¤ëÁ°¤Ë¤Ï¡¢
177 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÇË´þ¤¹¤Ù¤¤Ç¤¢¤ë¡£
179 .\"O The remainder of this section describes some specific details
180 .\"O of the Linux implementation of POSIX semaphores.
181 ¤³¤ÎÀá¤Î»Ä¤ê¤Ç¤Ï¡¢POSIX ¥»¥Þ¥Õ¥©¤Î Linux ¤Î¼ÂÁõ¤Î¾ÜºÙ
185 .\"O Prior to kernel 2.6, Linux only supported unnamed,
186 .\"O thread-shared semaphores.
187 .\"O On a system with Linux 2.6 and a glibc that provides the NPTL
188 .\"O threading implementation,
189 .\"O a complete implementation of POSIX semaphores is provided.
190 ¥Ð¡¼¥¸¥ç¥ó 2.6 ¤è¤êÁ°¤Î¥«¡¼¥Í¥ë¤Ç¤Ï¡¢Linux ¤Ï
191 ̾Á°¤Ê¤·¤Î¥¹¥ì¥Ã¥É¶¦Í¥»¥Þ¥Õ¥©¤Î¤ß¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤¿¡£
192 Linux 2.6 ¤È NPTL ¥¹¥ì¥Ã¥É¼ÂÁõ¤òÄ󶡤·¤Æ¤¤¤ë glibc ¤¬Æþ¤Ã¤¿
193 ¥·¥¹¥Æ¥à¤Ç¤Ï¡¢POSIX ¥»¥Þ¥Õ¥©¤Î´°Á´¤Ê¼ÂÁõ¤¬Ä󶡤µ¤ì¤ë¡£
196 .\"O POSIX named semaphores have kernel persistence:
197 .\"O if not removed by
198 .\"O .BR sem_unlink (3),
199 .\"O a semaphore will exist until the system is shut down.
200 POSIX ̾Á°ÉÕ¤¥»¥Þ¥Õ¥©¤Ï¥«¡¼¥Í¥ëÆâ¤ÇÊÝ»ý¤µ¤ì¤ë¡£
202 ¤Çºï½ü¤µ¤ì¤Ê¤±¤ì¤Ð¡¢¥»¥Þ¥Õ¥©¤Ï
203 ¥·¥¹¥Æ¥à¤¬¥·¥ã¥Ã¥È¥À¥¦¥ó¤µ¤ì¤ë¤Þ¤Ç¸ºß¤·Â³¤±¤ë¡£
206 .\"O Programs using the POSIX semaphores API must be compiled with
208 .\"O to link against the real-time library,
210 POSIX ¥»¥Þ¥Õ¥© API ¤ò»ÈÍѤ·¤¿¥×¥í¥°¥é¥à¤Ï
212 ¤Ç¥³¥ó¥Ñ¥¤¥ë¤·¡¢¥ê¥¢¥ë¥¿¥¤¥à¥é¥¤¥Ö¥é¥ê
214 ¤È¥ê¥ó¥¯¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
215 .\"O .SS Accessing named semaphores via the file system
216 .SS ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à·Ðͳ¤Ç¤Î̾Á°ÉÕ¤¥»¥Þ¥Õ¥©¤Ø¤Î¥¢¥¯¥»¥¹
217 .\"O On Linux, named semaphores are created in a virtual file system,
218 .\"O normally mounted under
220 .\"O with names of the form
221 .\"O .IR \fBsem.\fPsomename .
222 Linux ¤Ç¤Ï¡¢Ì¾Á°ÉÕ¤¥»¥Þ¥Õ¥©¤Ï²¾ÁÛ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à
223 (virtual file system) Æâ¤Ë
224 .I \fBsem.\fPsomename
225 ¤È¤¤¤¦·Á¤Î̾Á°¤ÇºîÀ®¤µ¤ì¤ë¡£²¾ÁÛ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤ÏÄ̾ï
227 °Ê²¼¤Ë¥Þ¥¦¥ó¥È¤µ¤ì¤ë¡£
228 .\"O (This is the reason that semaphore names are limited to
229 .\"O .BI NAME_MAX \-4
233 (¤³¤ì¤¬¡¢¥»¥Þ¥Õ¥©¤Î̾Á°¤Îʸ»ú¿ô¤Î¾å¸Â¤¬
237 ¤È¤Ê¤Ã¤Æ¤¤¤ëÍýͳ¤Ç¤¢¤ë¡£)
239 .\"O Since Linux 2.6.19, ACLs can be placed on files under this directory,
240 .\"O to control object permissions on a per-user and per-group basis.
241 Linux 2.6.19 °Ê¹ß¤Ç¤Ï¡¢¤³¤Î¥Ç¥£¥ì¥¯¥È¥êÇÛ²¼¤Î¥Õ¥¡¥¤¥ë¤ËÂФ·¤Æ ACL ¤ò
242 ÀßÄê¤Ç¤¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Ø¤Îµö²Ä¤ò¥æ¡¼¥¶Ã±°Ì¡¢¥°¥ë¡¼¥×ñ°Ì¤ÇÀ©¸æ¤¹¤ë¤³¤È¤¬
244 .\"O .SH "CONFORMING TO"
249 .\"O System V semaphores
250 .\"O .RB ( semget (2),
252 .\"O etc.) are an older semaphore API.
253 .\"O POSIX semaphores provide a simpler, and better designed interface than
254 .\"O System V semaphores;
255 .\"O on the other hand POSIX semaphores are less widely available
256 .\"O (especially on older systems) than System V semaphores.
260 ¤Ê¤É) ¤Ï¸Å¤¤¥»¥Þ¥Õ¥© API ¤Ç¤¢¤ë¡£ POSIX ¥»¥Þ¥Õ¥©¤Ï System V ¤è¤ê¤â
261 ´Êñ¤Ç¡¢¤¦¤Þ¤¯À߷פµ¤ì¤¿¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤òÄ󶡤·¤Æ¤¤¤ë¡£
262 °ìÊý¤Ç¡¢POSIX ¥»¥Þ¥Õ¥©¤Ï System V ¥»¥Þ¥Õ¥©¤ÈÈæ¤Ù¤ë¤È
263 ÍøÍѤǤ¤ë¥·¥¹¥Æ¥à¤¬¾¯¤Ê¤¤ (Æäˡ¢¸Å¤¤¥·¥¹¥Æ¥à¤Ç¤Ï¾¯¤Ê¤¤)¡£
266 .\"O An example of the use of various POSIX semaphore functions is shown in
267 .\"O .BR sem_wait (3).
268 ³Æ¼ï¤Î POSIX ¥»¥Þ¥Õ¥©´Ø¿ô¤ò»ÈÍѤ·¤¿Î㤬
275 .BR sem_getvalue (3),