OSDN Git Service

f6714c92008d10ebc34dff82111685a70b411092
[linuxjm/LDP_man-pages.git] / draft / man7 / sem_overview.7
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
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
30 .\"
31 .TH SEM_OVERVIEW 7 2009-02-25 "Linux" "Linux Programmer's Manual"
32 .\"O .SH NAME
33 .SH Ì¾Á°
34 .\"O sem_overview \- Overview of POSIX semaphores
35 sem_overview \- POSIX ¥»¥Þ¥Õ¥©¤Î³µÍ×
36 .\"O .SH DESCRIPTION
37 .SH ÀâÌÀ
38 .\"O POSIX semaphores allow processes and threads to synchronize their actions.
39 POSIX ¥»¥Þ¥Õ¥©¤ò»ÈÍѤ¹¤ë¤È¡¢¥×¥í¥»¥¹¤ä¥¹¥ì¥Ã¥É´Ö¤Ç¤½¤ÎÆ°ºî¤ò
40 Ʊ´ü¤µ¤»¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
41
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
49 .\"O .BR sem_wait (3)
50 .\"O operation will block until the value becomes greater than zero.
51 ¥»¥Þ¥Õ¥©¤ÏÀ°¿ô¤Ç¤¢¤ê¡¢¤½¤ÎÃͤϷ褷¤Æ 0 Ì¤Ëþ¤Ë¤Ê¤ë¤³¤È¤Ïµö¤µ¤ì¤Ê¤¤¡£
52 ¥»¥Þ¥Õ¥©¤ËÂФ·¤Æ¤Ç¤­¤ëÁàºî¤Ï 2 ¤Ä¤Ç¤¢¤ë:
53 ¥»¥Þ¥Õ¥©Ãͤò 1 Áý¤ä¤¹
54 .RB ( sem_post (3));
55 ¥»¥Þ¥Õ¥©Ãͤò 1 ¸º¤é¤¹
56 .RB ( sem_wait (3))¡£
57 ¥»¥Þ¥Õ¥©¤ÎÃͤ¬¤¹¤Ç¤Ë 0 ¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©Ãͤ¬ 0 ¤è¤êÂ礭¤¯¤Ê¤ë¤Þ¤Ç
58 .BR sem_wait (3)
59 Áàºî¤ÏÄä»ß (block) ¤¹¤ë¡£
60
61 .\"O POSIX semaphores come in two forms: named semaphores and
62 .\"O unnamed semaphores.
63 POSIX ¥»¥Þ¥Õ¥©¤Ë¤Ï¡¢Ì¾Á°ÉÕ¤­¥»¥Þ¥Õ¥© (named semaphore) ¤È
64 ̾Á°¤Ê¤·¥»¥Þ¥Õ¥© (unnamed semaphore) ¤Î 2¤Ä¤Î·Á¤¬¤¢¤ë¡£
65 .TP
66 .\"O .B Named semaphores
67 .B Ì¾Á°ÉÕ¤­¥»¥Þ¥Õ¥©
68 .\"O A named semaphore is identified by a name of the form
69 .\"O .IR /somename ;
70 .\"O that is, a null-terminated string of up to
71 .\"O .BI NAME_MAX \-4
72 .\"O (i.e., 251) characters consisting of an initial slash,
73 ̾Á°ÉÕ¤­¥»¥Þ¥Õ¥©¤Ï
74 .I /somename
75 ¤È¤¤¤¦·Á¼°¤Î̾Á°¤Ç¼±Ê̤µ¤ì¤ë¡£
76 ¤½¤Î̾Á°¤Ï¡¢ºÇÂç¤Ç
77 .BI NAME_MAX \-4
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
91 .\"O the same name to
92 .\"O .BR sem_open (3).
93 .BR sem_open (3)
94 ¤ËƱ¤¸Ì¾Á°¤òÅϤ¹¤³¤È¤Ë¤è¤ê¡¢2 ¤Ä¤Î¥×¥í¥»¥¹´Ö¤ÇƱ¤¸Ì¾Á°¤Î¥»¥Þ¥Õ¥©
95 ¤ËÂФ·Áàºî¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤­¤ë¡£
96
97 .\"O The
98 .\"O .BR sem_open (3)
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)
103 .\"O and
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).
111 .BR sem_open (3)
112 ´Ø¿ô¤Ï¡¢¿·¤·¤¤Ì¾Á°ÉÕ¤­¥»¥Þ¥Õ¥©¤òºîÀ®¤¹¤ë¤«¡¢´û¤Ë¸ºß¤¹¤ë̾Á°ÉÕ¤­
113 ¥»¥Þ¥Õ¥©¤ò¥ª¡¼¥×¥ó¤¹¤ë¡£
114 ¥»¥Þ¥Õ¥©¤ò¥ª¡¼¥×¥ó¤·¤¿¸å¤Ï¡¢
115 .BR sem_post (3)
116 ¤È
117 .BR sem_wait (3)
118 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÁàºî¤Ç¤­¤ë¡£
119 ¥×¥í¥»¥¹¤¬¥»¥Þ¥Õ¥©¤Î»ÈÍѤò½ª¤¨¤¿ºÝ¤Ï¡¢
120 .BR sem_close (3)
121 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤ò¥¯¥í¡¼¥º¤Ç¤­¤ë¡£
122 ¤¢¤ë¥»¥Þ¥Õ¥©¤ò¤É¤Î¥×¥í¥»¥¹¤â»ÈÍѤ·¤Ê¤¯¤Ê¤ë¤È¡¢
123 .BR sem_unlink (3)
124 ¤ò»È¤Ã¤Æ¤½¤Î¥»¥Þ¥Õ¥©¤ò¥·¥¹¥Æ¥à¤«¤éºï½ü¤¹¤ë¤³¤È¤¬¤Ç¤­¤ë¡£
125 .TP
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" )
132 .\"O or processes (a
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
138 .\"O .BR semget (2),
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)" ¡¢
145 ¸å¼Ô¤ò
146 .IR "¥×¥í¥»¥¹¶¦Í­¥»¥Þ¥Õ¥© (process-shared semaphore)"
147 ¤È¸Æ¤Ö)¡£¥¹¥ì¥Ã¥É¶¦Í­¥»¥Þ¥Õ¥©¤Ï¡¢Æ±¤¸¥×¥í¥»¥¹Æâ¤Î¥¹¥ì¥Ã¥É´Ö¤Ç¶¦Í­¤µ¤ì¤¿
148 ¥á¥â¥êÎΰ衢Î㤨¤ÐÂç°èÊÑ¿ô (global variable) ¤ËÇÛÃÖ¤µ¤ì¤ë¡£
149 ¥×¥í¥»¥¹¶¦Í­¥»¥Þ¥Õ¥©¤Ï¡¢¶¦Í­¥á¥â¥êÎΰè (Î㤨¤Ð¡¢
150 .BR semget (2)
151 ¤ò»È¤Ã¤ÆºîÀ®¤Ç¤­¤ë System V ¶¦Í­¥á¥â¥ê¡¦¥»¥°¥á¥ó¥È¤ä
152 .BR shm_open (3)
153 ¤ò»È¤Ã¤ÆºîÀ®¤Ç¤­¤ë POSIX ¶¦Í­¥á¥â¥ê¡¦¥ª¥Ö¥¸¥§¥¯¥È)
154 Æâ¤ËÇÛÃÖ¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
155
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)
160 .\"O and
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 ̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤Ï¡¢»ÈÍѤ¹¤ëÁ°¤Ë
167 .BR sem_init (3)
168 ¤ò»È¤Ã¤Æ½é´ü²½¤·¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
169 ¥»¥Þ¥Õ¥©¤Ï
170 .BR sem_post (3)
171 ¤È
172 .BR sem_wait (3)
173 ¤ò»È¤Ã¤ÆÁàºî¤Ç¤­¤ë¡£
174 ¥»¥Þ¥Õ¥©¤¬¤â¤Ï¤äɬÍפʤ¯¤Ê¤Ã¤¿¤È¤­¤ä¡¢
175 ¥»¥Þ¥Õ¥©¤¬ÃÖ¤«¤ì¤Æ¤¤¤ë¥á¥â¥ê¤ò²òÊü¤¹¤ëÁ°¤Ë¤Ï¡¢
176 .BR sem_destroy (3)
177 ¤ò»È¤Ã¤Æ¥»¥Þ¥Õ¥©¤òÇË´þ¤¹¤Ù¤­¤Ç¤¢¤ë¡£
178 .PP
179 .\"O The remainder of this section describes some specific details
180 .\"O of the Linux implementation of POSIX semaphores.
181 ¤³¤ÎÀá¤Î»Ä¤ê¤Ç¤Ï¡¢POSIX ¥»¥Þ¥Õ¥©¤Î Linux ¤Î¼ÂÁõ¤Î¾ÜºÙ
182 ¤Ë¤Ä¤¤¤ÆÀâÌÀ¤¹¤ë¡£
183 .\"O .SS Versions
184 .SS ¥Ð¡¼¥¸¥ç¥ó
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 ¥»¥Þ¥Õ¥©¤Î´°Á´¤Ê¼ÂÁõ¤¬Ä󶡤µ¤ì¤ë¡£
194 .\"O .SS Persistence
195 .SS »ý³À­
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 Ì¾Á°ÉÕ¤­¥»¥Þ¥Õ¥©¤Ï¥«¡¼¥Í¥ëÆâ¤ÇÊÝ»ý¤µ¤ì¤ë¡£
201 .BR sem_unlink (3)
202 ¤Çºï½ü¤µ¤ì¤Ê¤±¤ì¤Ð¡¢¥»¥Þ¥Õ¥©¤Ï
203 ¥·¥¹¥Æ¥à¤¬¥·¥ã¥Ã¥È¥À¥¦¥ó¤µ¤ì¤ë¤Þ¤Ç¸ºß¤·Â³¤±¤ë¡£
204 .\"O .SS Linking
205 .SS ¥ê¥ó¥¯
206 .\"O Programs using the POSIX semaphores API must be compiled with
207 .\"O .I cc \-lrt
208 .\"O to link against the real-time library,
209 .\"O .IR librt .
210 POSIX ¥»¥Þ¥Õ¥© API ¤ò»ÈÍѤ·¤¿¥×¥í¥°¥é¥à¤Ï
211 .I cc \-lrt
212 ¤Ç¥³¥ó¥Ñ¥¤¥ë¤·¡¢¥ê¥¢¥ë¥¿¥¤¥à¥é¥¤¥Ö¥é¥ê
213 .I librt
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
219 .\"O .IR /dev/shm ,
220 .\"O with names of the form
221 .\"O .IR \fBsem.\fPsomename .
222 Linux ¤Ç¤Ï¡¢Ì¾Á°ÉÕ¤­¥»¥Þ¥Õ¥©¤Ï²¾ÁÛ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à
223 (virtual file system) Æâ¤Ë
224 .I \fBsem.\fPsomename
225 ¤È¤¤¤¦·Á¤Î̾Á°¤ÇºîÀ®¤µ¤ì¤ë¡£²¾ÁÛ¥Õ¥¡¥¤¥ë¥·¥¹¥Æ¥à¤ÏÄ̾ï
226 .I /dev/shm
227 °Ê²¼¤Ë¥Þ¥¦¥ó¥È¤µ¤ì¤ë¡£
228 .\"O (This is the reason that semaphore names are limited to
229 .\"O .BI NAME_MAX \-4
230 .\"O rather than
231 .\"O .B NAME_MAX
232 .\"O characters.)
233 (¤³¤ì¤¬¡¢¥»¥Þ¥Õ¥©¤Î̾Á°¤Îʸ»ú¿ô¤Î¾å¸Â¤¬
234 .B NAME_MAX
235 ¤Ç¤Ï¤Ê¤¯
236 .BI NAME_MAX \-4
237 ¤È¤Ê¤Ã¤Æ¤¤¤ëÍýͳ¤Ç¤¢¤ë¡£)
238
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 ÀßÄê¤Ç¤­¡¢¥ª¥Ö¥¸¥§¥¯¥È¤Ø¤Îµö²Ä¤ò¥æ¡¼¥¶Ã±°Ì¡¢¥°¥ë¡¼¥×ñ°Ì¤ÇÀ©¸æ¤¹¤ë¤³¤È¤¬
243 ¤Ç¤­¤ë¡£
244 .\"O .SH "CONFORMING TO"
245 .SH ½àµò
246 POSIX.1-2001.
247 .\"O .SH NOTES
248 .SH Ãí°Õ
249 .\"O System V semaphores
250 .\"O .RB ( semget (2),
251 .\"O .BR semop (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.
257 System V ¥»¥Þ¥Õ¥©
258 .RB ( semget (2),
259 .BR semop (2)
260 ¤Ê¤É) ¤Ï¸Å¤¤¥»¥Þ¥Õ¥© API ¤Ç¤¢¤ë¡£ POSIX ¥»¥Þ¥Õ¥©¤Ï System V ¤è¤ê¤â
261 ´Êñ¤Ç¡¢¤¦¤Þ¤¯À߷פµ¤ì¤¿¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤òÄ󶡤·¤Æ¤¤¤ë¡£
262 °ìÊý¤Ç¡¢POSIX ¥»¥Þ¥Õ¥©¤Ï System V ¥»¥Þ¥Õ¥©¤ÈÈæ¤Ù¤ë¤È
263 ÍøÍѤǤ­¤ë¥·¥¹¥Æ¥à¤¬¾¯¤Ê¤¤ (Æäˡ¢¸Å¤¤¥·¥¹¥Æ¥à¤Ç¤Ï¾¯¤Ê¤¤)¡£
264 .\"O .SH EXAMPLE
265 .SH Îã
266 .\"O An example of the use of various POSIX semaphore functions is shown in
267 .\"O .BR sem_wait (3).
268 ³Æ¼ï¤Î POSIX ¥»¥Þ¥Õ¥©´Ø¿ô¤ò»ÈÍѤ·¤¿Î㤬
269 .BR sem_wait (3)
270 ¤Ëµ­ºÜ¤µ¤ì¤Æ¤¤¤ë¡£
271 .\"O .SH "SEE ALSO"
272 .SH ´ØÏ¢¹àÌÜ
273 .BR sem_close (3),
274 .BR sem_destroy (3),
275 .BR sem_getvalue (3),
276 .BR sem_init (3),
277 .BR sem_open (3),
278 .BR sem_post (3),
279 .BR sem_unlink (3),
280 .BR sem_wait (3),
281 .BR pthreads (7)