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 2010-05-22 "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 セマフォを使用すると、プロセスやスレッド間でその動作を
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 より大きくなるまで
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,
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 つのプロセス間で同じ名前のセマフォ
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 関数は、新しい名前付きセマフォを作成するか、既に存在する名前付き
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 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 複数スレッド間で共有されるメモリ領域、もしくは複数プロセス間で
144 .IR "スレッド共有セマフォ (thread-shared semaphore)" 、
146 .IR "プロセス共有セマフォ (process-shared semaphore)"
147 と呼ぶ)。スレッド共有セマフォは、同じプロセス内のスレッド間で共有される
148 メモリ領域、例えば大域変数 (global variable) に配置される。
149 プロセス共有セマフォは、共有メモリ領域 (例えば、
151 を使って作成できる System V 共有メモリ・セグメントや
153 を使って作成できる POSIX 共有メモリ・オブジェクト)
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).
175 セマフォが置かれているメモリを解放する前には、
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 名前付きセマフォはカーネル内で保持される。
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 を使用したプログラムは
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 という形の名前で作成される。仮想ファイルシステムは通常
228 .\"O (This is the reason that semaphore names are limited to
229 .\"O .BI NAME_MAX \-4
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),