1 .\" Copyright (C) 1996-1999 Free Software Foundation, Inc.
3 .\" Permission is granted to make and distribute verbatim copies of
4 .\" this 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
8 .\" this manual under the conditions for verbatim copying, provided that
9 .\" the entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Permission is granted to copy and distribute translations of this
13 .\" manual into another language, under the above conditions for modified
14 .\" versions, except that this permission notice may be stated in a
15 .\" translation approved by the Foundation.
17 .\" Copyright (C) 1996 Xavier Leroy.
19 .\" Japanese Version Copyright (C) 2002-2003 Suzuki Takashi
20 .\" all rights reserved.
21 .\" Translated Fri Jan 3 23:23:30 JST 2003
22 .\" by Suzuki Takashi.
24 .\"WORD: semaphore ¥»¥Þ¥Õ¥©
25 .\"WORD: atomically °ì©¤Ç
26 .\"WORD: semaphore object ¥»¥Þ¥Õ¥©¥ª¥Ö¥¸¥§¥¯¥È
27 .\"WORD: atomic compare-and-swap ÉÔ²Äʬ¤ÎÈæ³Ó¸ò´¹Ì¿Îá
28 .\"WORD: atomic operation ÉÔ²Äʬ̿Îá
29 .\"WORD: wait on the semaphore ¥»¥Þ¥Õ¥©¤Î³ÍÆÀÂÔ¤Á
32 .TH SEMAPHORES 3 LinuxThreads
36 .\"O sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy \- operations on semaphores
38 sem_init, sem_wait, sem_trywait, sem_post, sem_getvalue, sem_destroy \- ¥»¥Þ¥Õ¥©¤ÎÁàºî
42 .B #include <semaphore.h>
44 .BI "int sem_init(sem_t *" sem ", int " pshared ", unsigned int " value ");"
46 .BI "int sem_wait(sem_t * " sem ");"
48 .BI "int sem_trywait(sem_t * " sem ");"
50 .BI "int sem_post(sem_t * " sem ");"
52 .BI "int sem_getvalue(sem_t * " sem ", int * " sval ");"
54 .BI "int sem_destroy(sem_t * " sem ");"
57 .\"O This manual page documents POSIX 1003.1b semaphores, not to be
58 .\"O confused with SystemV semaphores as described in
64 ¤³¤Î¥Þ¥Ë¥å¥¢¥ë¥Ú¡¼¥¸¤Ï POSIX 1003.1b ¤Î¥»¥Þ¥Õ¥©¤Ë¤Ä¤¤¤Æµ½Ò¤·¤Æ¤ª¤ê¡¢
70 ¤Ëµ½Ò¤µ¤ì¤Æ¤¤¤ë SystemV ¥»¥Þ¥Õ¥©¤Èº®Æ±¤·¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
72 .\"O Semaphores are counters for resources shared between threads. The
73 .\"O basic operations on semaphores are: increment the counter atomically,
74 .\"O and wait until the counter is non-null and decrement it atomically.
75 ¥»¥Þ¥Õ¥©¤Ï¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í¤µ¤ì¤ë»ñ¸»¤ò´ÉÍý¤¹¤ë¤¿¤á¤Î¥«¥¦¥ó¥¿¤Ç¤¢¤ë¡£
76 ¥»¥Þ¥Õ¥©¤Ë´Ø¤¹¤ë´ðËÜŪ¤ÊÁàºî¤Ï¼¡¤Î 2 ¤Ä¤Ç¤¢¤ë:
77 ¥«¥¦¥ó¥¿¤ò°ì©¤Ç 1 ¤À¤±Áý²Ã¤µ¤»¤ë¤³¤È¤È¡¢
78 ¥«¥¦¥ó¥¿¤¬ 0 ¤Ç¤Ê¤¯¤Ê¤ë¤Þ¤ÇÂԤ俤¢¤È°ì©¤Ç 1 ¤À¤±¸º¾¯¤µ¤»¤ë¤³¤È¤Ç¤¢¤ë¡£
81 .\"O initializes the semaphore object pointed to by
84 .\"O count associated with the semaphore is set initially to
88 .\"O argument indicates whether the semaphore is local to the
89 .\"O current process (
91 .\"O is zero) or is to be shared between several
94 .\"O is not zero). LinuxThreads currently does not
95 .\"O support process-shared semaphores, thus
97 .\"O always returns with
106 ¤Ç»ØÄꤵ¤ì¤ë¥»¥Þ¥Õ¥©¥ª¥Ö¥¸¥§¥¯¥È¤ò½é´ü²½¤¹¤ë¡£
107 ¥»¥Þ¥Õ¥©¤Ë·ë¤ÓÉÕ¤±¤é¤ì¤ë¥«¥¦¥ó¥È¤Ï
112 ¤Ï¥»¥Þ¥Õ¥©¤¬¸½ºß¤Î¥×¥í¥»¥¹¤À¤±¤Ç͸ú (
114 ¤¬ 0) ¤«Ê£¿ô¤Î¥×¥í¥»¥¹¤Ç¶¦Í¤µ¤ì¤ë (
116 ¤¬Èó 0) ¤«¤ò»ØÄꤹ¤ë¡£LinuxThreads ¤Ï¸½ºß¤Î¤È¤³¤í
117 ¥×¥í¥»¥¹´Ö¶¦Í¥»¥Þ¥Õ¥©¤Ï¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤Ê¤¤¡£
127 .\"O suspends the calling thread until the semaphore pointed to
130 .\"O has non-zero count. It then atomically decreases the
131 .\"O semaphore count.
135 ¤Ç»ØÄꤵ¤ì¤ë¥»¥Þ¥Õ¥©¤Î¥«¥¦¥ó¥È¤¬Èó 0 ¤Ë¤Ê¤ë¤Þ¤Ç
136 ¸Æ¤Ó½Ð¤·¥¹¥ì¥Ã¥É¤Î¼Â¹Ô¤òÄä»ß¤¹¤ë¡£
137 ¤½¤·¤Æ¥»¥Þ¥Õ¥©¥«¥¦¥ó¥È¤ò°ì©¤Ç 1 ¤À¤±¸º¾¯¤µ¤»¤ë¡£
139 .\"O .B "sem_trywait"
140 .\"O is a non-blocking variant of
141 .\"O .BR "sem_wait" .
143 .\"O semaphore pointed to by
145 .\"O has non-zero count, the count is
146 .\"O atomically decreased and
147 .\"O .B "sem_trywait"
148 .\"O immediately returns 0.
149 .\"O If the semaphore count is zero,
150 .\"O .B "sem_trywait"
151 .\"O immediately returns with
155 ¤Ï¥Ö¥í¥Ã¥¯¤ò¤È¤â¤Ê¤ï¤Ê¤¤
159 ¤Ç»ØÄꤵ¤ì¤ë¥»¥Þ¥Õ¥©¤Î¥«¥¦¥ó¥È¤¬Èó 0 ¤Î¾ì¹ç¤Ë¤Ï
160 °ì©¤Ç¥«¥¦¥ó¥È¤¬ 1 ¤À¤±¸º¾¯¤µ¤ì¤Æ
163 ¥»¥Þ¥Õ¥©¥«¥¦¥ó¥È¤¬ 0 ¤Î¾ì¹ç¤Ë¤Ï
170 .\"O atomically increases the count of the semaphore pointed to
173 .\"O This function never blocks and can safely be used in
174 .\"O asynchronous signal handlers.
178 ¤Ç»ØÄꤵ¤ì¤ë¥»¥Þ¥Õ¥©¤Î¥«¥¦¥ó¥È¤ò°ì©¤Ç 1 ¤À¤±Áý²Ã¤µ¤»¤ë¡£
179 ¤³¤Î´Ø¿ô¤Ï·è¤·¤Æ¥Ö¥í¥Ã¥¯¤¹¤ë¤³¤È¤Ï¤Ê¤¯¡¢
180 ÈóƱ´ü¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤ÎÃæ¤Ç¤â°ÂÁ´¤Ë»ÈÍѤ¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
182 .\"O .B "sem_getvalue"
183 .\"O stores in the location pointed to by
186 .\"O count of the semaphore
194 ¤Î¸½ºß¤Î¥«¥¦¥ó¥È¤ò³ÊǼ¤¹¤ë¡£
196 .\"O .B "sem_destroy"
197 .\"O destroys a semaphore object, freeing the resources it
198 .\"O might hold. No threads should be waiting on the semaphore at the time
199 .\"O .B "sem_destroy"
200 .\"O is called. In the LinuxThreads implementation, no
201 .\"O resources are associated with semaphore objects, thus
202 .\"O .B "sem_destroy"
203 .\"O actually does nothing except checking that no thread is waiting on the
206 ¤Ï¥»¥Þ¥Õ¥©¥ª¥Ö¥¸¥§¥¯¥È¤òÇ˲õ¤·¡¢
207 ¥»¥Þ¥Õ¥©¥ª¥Ö¥¸¥§¥¯¥È¤¬ÊÝ»ý¤·¤Æ¤¤¤¿»ñ¸»¤ò²òÊü¤¹¤ë¡£
209 ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¤È¤¤Ë¤½¤Î¥»¥Þ¥Õ¥©¤ò³ÍÆÀÂÔ¤Á¤·¤Æ¤¤¤ë
210 ¥¹¥ì¥Ã¥É¤¬¤¢¤Ã¤Æ¤Ï¤Ê¤é¤Ê¤¤¡£
211 LinuxThreads ¤Î¼ÂÁõ¤Ç¤Ï¡¢
212 ¥»¥Þ¥Õ¥©¥ª¥Ö¥¸¥§¥¯¥È¤Ï²¿¤â»ñ¸»¤òÊÝ»ý¤·¤Ê¤¤¤¿¤á¡¢
214 ¤Ï¤½¤Î¥¹¥ì¥Ã¥É¤ò³ÍÆÀÂÔ¤Á¤·¤Æ¤¤¤ë¥¹¥ì¥Ã¥É¤¬¤Ê¤¤¤³¤È¤ò
215 ¥Á¥§¥Ã¥¯¤¹¤ë°Ê³°¤Ë²¿¤â¤·¤Ê¤¤¡£
217 .\"O .SH CANCELLATION
221 .\"O is a cancellation point.
223 ¤Ï¼è¤ê¾Ã¤·¥Ý¥¤¥ó¥È¤Ç¤¢¤ë¡£
225 .\"O .SH "ASYNC-SIGNAL SAFETY"
226 .SH "ÈóƱ´ü¥·¥°¥Ê¥ë¤Î°ÂÁ´À"
228 .\"O On processors supporting atomic compare-and-swap (Intel 486, Pentium
229 .\"O and later, Alpha, PowerPC, MIPS II, Motorola 68k), the
231 .\"O function is async-signal safe and can therefore be
232 .\"O called from signal handlers. This is the only thread synchronization
233 .\"O function provided by POSIX threads that is async-signal safe.
234 ÉÔ²Äʬ¤ÎÈæ³Ó¸ò´¹Ì¿Îá (compare-and-swap) ¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ë
235 CPU (Intel 486 ¤ä Pentium ¤ª¤è¤Ó¤½¤Î¸å·Ñ¡¢
236 Alpha ¡¢ PowerPC ¡¢ MIPS II ¡¢ Motorola 68k) ¤Ç¤Ï¡¢
239 ¤ÏÈóƱ´ü¥·¥°¥Ê¥ë¤ËÂФ·¤Æ°ÂÁ´¤Ç¤¢¤ê¡¢
240 ¤½¤Î¤¿¤á¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤«¤é¸Æ¤Ó½Ð¤¹¤³¤È¤¬²Äǽ¤Ç¤¢¤ë¡£
242 ¤Ï¡¢ÈóƱ´ü¥·¥°¥Ê¥ë¤ËÂФ·¤Æ°ÂÁ´¤Ê¡¢
243 POSIX ¥¹¥ì¥Ã¥É¤ÇÄ󶡤µ¤ì¤ëÍ£°ì¤Î¥¹¥ì¥Ã¥ÉƱ´ü´Ø¿ô¤Ç¤¢¤ë¡£
245 .\"O On the Intel 386 and the Sparc, the current LinuxThreads
246 .\"O implementation of
248 .\"O is not async-signal safe by lack of the
249 .\"O required atomic operations.
250 Intel 386 ¤È Sparc ¤Ë¤ª¤¤¤Æ¤Ï¡¢
251 ¸½»þÅÀ¤Î LinuxThreads ¤Î¼ÂÁõ¤Ç¤Ï
253 ¤ÏÈóƱ´ü¥·¥°¥Ê¥ë¤ËÂФ·¤Æ°ÂÁ´¤Ç¤Ê¤¤¡£
254 ¤³¤ì¤ÏɬÍפÊÉÔ²Äʬ̿Î᤬¸ºß¤·¤Ê¤¤¤«¤é¤Ç¤¢¤ë¡£
256 .\"O .SH "RETURN VALUE"
262 .\"O .B "sem_getvalue"
263 .\"O functions always return 0.
264 .\"O All other semaphore functions return 0 on success and -1 on error, in
265 .\"O addition to writing an error code in
272 ¾¤Î¥»¥Þ¥Õ¥©´Ø¿ô¤Ï¤¹¤Ù¤Æ¡¢
273 À®¸ù¤¹¤ë¤È 0 ¤òÊÖ¤·¡¢
274 ¥¨¥é¡¼¤Ê¤é¤Ð -1 ¤òÊÖ¤·¤Æ¥¨¥é¡¼¥³¡¼¥É¤ò
285 .\"O to the following codes on error:
290 .\"O exceeds the maximal counter value
291 .\"O .B "SEM_VALUE_MAX"
299 ¤Ï¥¨¥é¡¼¤Î¾ì¹ç¤Ë¼¡¤Î¤è¤¦¤Ê¥¨¥é¡¼¥³¡¼¥É¤ò
316 .\"O .B "sem_trywait"
319 .\"O to the following error code on error:
323 .\"O the semaphore count is currently 0
327 ¤Ï¥¨¥é¡¼¤Î¾ì¹ç¤Ë¼¡¤Î¤è¤¦¤Ê¥¨¥é¡¼¥³¡¼¥É¤ò
333 ¸½»þÅÀ¤Î¥»¥Þ¥Õ¥©¥«¥¦¥ó¥È¤¬ 0 ¤Ç¤¢¤ë¡£
340 .\"O to the following error code on error:
344 .\"O after incrementation, the semaphore value would exceed
345 .\"O .B "SEM_VALUE_MAX"
346 .\"O (the semaphore count is left unchanged in this case)
350 ¤Ï¥¨¥é¡¼¤Î¾ì¹ç¤Ë¼¡¤Î¤è¤¦¤Ê¥¨¥é¡¼¥³¡¼¥É¤ò
356 ¥«¥¦¥ó¥¿¤òÁý²Ã¤¹¤ë¤È¡¢¥»¥Þ¥Õ¥©¤ÎÃͤ¬
359 (¤³¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¥«¥¦¥ó¥È¤ÏÊѹ¹¤µ¤ì¤Ê¤¤) ¡£
363 .\"O .B "sem_destroy"
366 .\"O to the following error code on error:
370 .\"O some threads are currently blocked waiting on the semaphore.
374 ¤Ï¥¨¥é¡¼¤Î¾ì¹ç¤Ë¼¡¤Î¤è¤¦¤Ê¥¨¥é¡¼¥³¡¼¥É¤ò
380 »ØÄꤷ¤¿¥»¥Þ¥Õ¥©¤Î³ÍÆÀÂÔ¤Á¤Ç¥Ö¥í¥Ã¥¯¤µ¤ì¤Æ¤¤¤ë¥¹¥ì¥Ã¥É¤¬¤¢¤ë¡£
385 Xavier Leroy <Xavier.Leroy@inria.fr>
389 .BR "pthread_mutex_init" (3),
390 .BR "pthread_cond_init" (3),
391 .BR "pthread_cancel" (3),