.\" Copyright (C) 2006 Michael Kerrisk .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved. .\" Translated 2006-04-18, Akihiro MOTOKI .\" Updated 2008-08-07, Akihiro MOTOKI, LDP v3.05 .\" Updated 2009-02-23, Akihiro MOTOKI, LDP v3.19 .\" .TH SEM_OVERVIEW 7 2020\-06\-09 Linux "Linux Programmer's Manual" .SH 名前 sem_overview \- POSIX セマフォの概要 .SH 説明 POSIX セマフォを使用すると、プロセスやスレッド間でその動作を 同期させることができる。 .PP セマフォは整数であり、その値は決して 0 未満になることは許されない。 セマフォに対してできる操作は 2 つである: セマフォ値を 1 増やす (\fBsem_post\fP(3)); セマフォ値を 1 減らす (\fBsem_wait\fP(3))。 セマフォの値がすでに 0 の場合、セマフォ値が 0 より大きくなるまで \fBsem_wait\fP(3) 操作は停止 (block) する。 .PP POSIX セマフォには、名前付きセマフォ (named semaphore) と 名前なしセマフォ (unnamed semaphore) の 2つの形がある。 .TP \fB名前付きセマフォ\fP .\" glibc allows the initial slash to be omitted, and makes .\" multiple initial slashes equivalent to a single slash. .\" This differs from the implementation of POSIX message queues. .\" glibc allows subdirectory components in the name, in which .\" case the subdirectory tree must exist under /dev/shm, and .\" the fist subdirectory component must exist as the name .\" sem.name, and all of the subdirectory components must allow the .\" required permissions if a user wants to create a semaphore .\" object in a subdirectory. 名前付きセマフォは \fI/somename\fP という形式の名前で識別される。 その名前は、最大で \fBNAME_MAX\fP\fI\-4\fP (すなわち 251) 文字のヌル終端された文字列で、 スラッシュで始まり、スラッシュ以外の文字が 1 文字以上続く形式である。 \fBsem_open\fP(3) に同じ名前を渡すことにより、2 つのプロセス間で同じ名前のセマフォ に対し操作を行うことができる。 .IP \fBsem_open\fP(3) 関数は、新しい名前付きセマフォを作成するか、既に存在する名前付き セマフォをオープンする。 セマフォをオープンした後は、 \fBsem_post\fP(3) と \fBsem_wait\fP(3) を使ってセマフォを操作できる。 プロセスがセマフォの使用を終えた際は、 \fBsem_close\fP(3) を使ってセマフォをクローズできる。 あるセマフォをどのプロセスも使用しなくなると、 \fBsem_unlink\fP(3) を使ってそのセマフォをシステムから削除することができる。 .TP \fB名前なしセマフォ (メモリーベースセマフォ)\fP 名前なしセマフォは名前を持たない。その代わり、セマフォは、 複数スレッド間で共有されるメモリー領域、もしくは複数プロセス間で 共有されたメモリー領域に置かれる (前者を \fIスレッド共有セマフォ (thread\-shared semaphore)\fP、 後者を \fIプロセス共有セマフォ (process\-shared semaphore)\fP と呼ぶ)。スレッド共有セマフォは、同じプロセス内のスレッド間で共有される メモリー領域、例えば大域変数 (global variable) に配置される。 プロセス共有セマフォは、共有メモリー領域 (例えば、 \fBshmget\fP(2) を使って作成できる System V 共有メモリーセグメントや \fBshm_open\fP(3) を使って作成できる POSIX 共有メモリーオブジェクト) 内に配置しなければならない。 .IP 名前なしセマフォは、使用する前に \fBsem_init\fP(3) を使って初期化しなければならない。 セマフォは \fBsem_post\fP(3) と \fBsem_wait\fP(3) を使って操作できる。 セマフォがもはや必要なくなったときや、 セマフォが置かれているメモリーを解放する前には、 \fBsem_destroy\fP(3) を使ってセマフォを破棄すべきである。 .PP この節の残りでは、POSIX セマフォの Linux の実装の詳細 について説明する。 .SS Versions バージョン 2.6 より前のカーネルでは、Linux は 名前なしのスレッド共有セマフォのみをサポートしていた。 Linux 2.6 と NPTL スレッド実装を提供している glibc が入った システムでは、POSIX セマフォの完全な実装が提供される。 .SS 持続性 POSIX 名前付きセマフォはカーネル内で保持される。 \fBsem_unlink\fP(3) で削除されなければ、セマフォは システムがシャットダウンされるまで存在し続ける。 .SS リンク POSIX セマフォ API を使用したプログラムは \fIcc \-pthread\fP でコンパイルし、リアルタイムライブラリ \fIlibrt\fP とリンクしなければならない。 .SS ファイルシステム経由での名前付きセマフォへのアクセス Linux では、名前付きセマフォは仮想ファイルシステム (virtual file system) 内に \fBsem.\fP\fIsomename\fP という形の名前で作成される。仮想ファイルシステムは通常 \fI/dev/shm\fP 以下にマウントされる。 (これが、セマフォの名前の文字数の上限が \fBNAME_MAX\fP ではなく \fBNAME_MAX\fP\fI\-4\fP となっている理由である。) .PP Linux 2.6.19 以降では、このディレクトリ配下のファイルに対して ACL を 設定でき、オブジェクトへの許可をユーザー単位、グループ単位で制御することが できる。 .SH 注意 System V セマフォ (\fBsemget\fP(2), \fBsemop\fP(2) など) は古いセマフォ API である。 POSIX セマフォは System V よりも 簡単で、うまく設計されたインターフェースを提供している。 一方で、POSIX セマフォは System V セマフォと比べると 利用できるシステムが少ない (特に、古いシステムでは少ない)。 .SH 例 各種の POSIX セマフォ関数を使用した例が \fBsem_wait\fP(3) に記載されている。 .SH 関連項目 \fBsem_close\fP(3), \fBsem_destroy\fP(3), \fBsem_getvalue\fP(3), \fBsem_init\fP(3), \fBsem_open\fP(3), \fBsem_post\fP(3), \fBsem_unlink\fP(3), \fBsem_wait\fP(3), \fBpthreads\fP(7), \fBshm_overview\fP(7) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 5.10 の一部である。プロジェクトの説明とバグ報告に関する情報は \%https://www.kernel.org/doc/man\-pages/ に書かれている。