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"
33 sem_overview \- POSIX セマフォの概要
35 POSIX セマフォを使用すると、プロセスやスレッド間でその動作を
38 セマフォは整数であり、その値は決して 0 未満になることは許されない。
39 セマフォに対してできる操作は 2 つである:
44 セマフォの値がすでに 0 の場合、セマフォ値が 0 より大きくなるまで
48 POSIX セマフォには、名前付きセマフォ (named semaphore) と
49 名前なしセマフォ (unnamed semaphore) の 2つの形がある。
57 (すなわち 251) 文字の NULL 終端された文字列で、
58 .\" glibc allows the initial slash to be omitted, and makes
59 .\" multiple initial slashes equivalent to a single slash.
60 .\" This differs from the implementation of POSIX message queues.
61 スラッシュで始まり、スラッシュ以外の文字が 1 文字以上続く形式である。
62 .\" glibc allows subdirectory components in the name, in which
63 .\" case the subdirectory tree must exist under /dev/shm, and
64 .\" the fist subdirectory component must exist as the name
65 .\" sem.name, and all of the subdirectory components must allow the
66 .\" required permissions if a user wants to create a semaphore
67 .\" object in a subdirectory.
69 に同じ名前を渡すことにより、2 つのプロセス間で同じ名前のセマフォ
73 関数は、新しい名前付きセマフォを作成するか、既に存在する名前付き
83 あるセマフォをどのプロセスも使用しなくなると、
85 を使ってそのセマフォをシステムから削除することができる。
87 .B 名前なしセマフォ (メモリベース・セマフォ)
88 名前なしセマフォは名前を持たない。その代わり、セマフォは、
89 複数スレッド間で共有されるメモリ領域、もしくは複数プロセス間で
91 .IR "スレッド共有セマフォ (thread-shared semaphore)" 、
93 .IR "プロセス共有セマフォ (process-shared semaphore)"
94 と呼ぶ)。スレッド共有セマフォは、同じプロセス内のスレッド間で共有される
95 メモリ領域、例えば大域変数 (global variable) に配置される。
96 プロセス共有セマフォは、共有メモリ領域 (例えば、
98 を使って作成できる System V 共有メモリ・セグメントや
100 を使って作成できる POSIX 共有メモリ・オブジェクト)
112 セマフォが置かれているメモリを解放する前には、
116 この節の残りでは、POSIX セマフォの Linux の実装の詳細
119 バージョン 2.6 より前のカーネルでは、Linux は
120 名前なしのスレッド共有セマフォのみをサポートしていた。
121 Linux 2.6 と NPTL スレッド実装を提供している glibc が入った
122 システムでは、POSIX セマフォの完全な実装が提供される。
124 POSIX 名前付きセマフォはカーネル内で保持される。
127 システムがシャットダウンされるまで存在し続ける。
129 POSIX セマフォ API を使用したプログラムは
134 .SS ファイルシステム経由での名前付きセマフォへのアクセス
135 Linux では、名前付きセマフォは仮想ファイルシステム
136 (virtual file system) 内に
137 .I \fBsem.\fPsomename
138 という形の名前で作成される。仮想ファイルシステムは通常
147 Linux 2.6.19 以降では、このディレクトリ配下のファイルに対して ACL を
148 設定でき、オブジェクトへの許可をユーザ単位、グループ単位で制御することが
156 など) は古いセマフォ API である。 POSIX セマフォは System V よりも
157 簡単で、うまく設計されたインタフェースを提供している。
158 一方で、POSIX セマフォは System V セマフォと比べると
159 利用できるシステムが少ない (特に、古いシステムでは少ない)。
161 各種の POSIX セマフォ関数を使用した例が
167 .BR sem_getvalue (3),