SEM_OVERVIEW(7) Linux Programmer’s Manual SEM_OVERVIEW(7) 名前 sem_overview - POSIX セマフォの概要 説明 POSIX セマフォを使用すると、プロセスやスレッド間でその動作を同期させる ことができる。 セマフォは整数であり、その値は決して 0 未満になることは許されない。セマ フ ォ に 対 し て で き る 操 作 は 2 つである: セマフォ値を 1 増やす (sem_post(3)); セマフォ値を 1 減らす (sem_wait(3))。セマフォの値がす で に 0 の場合、セマフォ値が 0 より大きくなるまで sem_wait(3) 操作は停止 (block) する。 POSIX セマフォには、名前付きセマフォ (named semaphore) と名前なしセマフ ォ (unnamed semaphore) の 2つの形がある。 名前付きセマフォ 名 前付きセマフォは /somename という形式の名前で識別される。その 名前は、最大で NAME_MAX-4 (すなわち 251) 文字の NULL 終端され た 文 字列で、スラッシュで始まり、スラッシュ以外の文字が 1 文字以上 続く形式である。 sem_open(3) に同じ名前を渡すことにより、2 つ の プロセス間で同じ名前のセマフォに対し操作を行うことができる。 sem_open(3) 関数は、新しい名前付きセマフォを作成するか、既に存在 する名前付きセマフォをオープンする。セマフォをオープンした後は、 sem_post(3) と sem_wait(3) を使ってセマフォを操作できる。プロセ スがセマフォの使用を終えた際は、 sem_close(3) を使ってセマフォを ク ロ ーズできる。あるセマフォをどのプロセスも使用しなくなると、 sem_unlink(3) を使ってそのセマフォをシステムから削除することがで きる。 名前なしセマフォ (メモリベース・セマフォ) 名前なしセマフォは名前を持たない。その代わり、セマフォは、複数ス レッド間で共有されたメモリ領域、もしくは複数プロセス間で共有され た メ モ リ 領域に置かれる (前者を スレッド共有セマフォ (thread- shared semaphore)、後者を プロセス共有セマフ ォ (process-shared semaphore) と呼ぶ)。スレッド共有セマフォは、同じプロセス内のスレ ッド間で共有されたメモリ領域、例えば大域変数 (global variable) に 配 置される。プロセス共有セマフォは、共有メモリ領域 (例えば、 semget(2) を使って作成できる System V 共有メモリ・セグメン ト や shm_open(3) を使って作成できる POSIX 共有メモリ・オブジェクト) 内に配置しなければならない。 名前なしセマフォは、使用する前に sem_init(3) を使って初期化し な ければならない。セマフォは sem_post(3) と sem_wait(3) を使って操 作できる。セマフォがもはや必要なくなったときや、セマフォが置かれ ているメモリを解放する前には、 sem_destroy(3) を使ってセマフォを 破棄すべきである。 この節の残りでは、POSIX セマフォの Linux の実装の詳細について説明する。 バージョン バージョン 2.6 より前のカーネルでは、Linux は名前なしのスレッド共有セマ フォのみをサポートしていた。 Linux 2.6 と NPTL スレッド実装を提供してい る glibc が入ったシステムでは、POSIX セマフォの完全な実装が提供される。 持続性 POSIX 名前付きセマフォはカーネル内で保持される。 sem_unlink(3) で削除さ れなければ、セマフォはシステムがシャットダウンされるまで存在し続ける。 リンク POSIX セマフォ API を使用したプログラムは cc -lrt でコンパイルし、リア ルタイムライブラリ librt とリンクしなければならない。 ファイルシステム経由での名前付きセマフォへのアクセス Linux では、名前付きセマフォは仮想ファイルシステム (virtual file sys- tem) 内に sem.somename という形の名前で作成される。仮想ファイルシステム は通常 /dev/shm 以下にマウントされる。 (これが、セマフォの名前の文字 数 の上限が NAME_MAX ではなく NAME_MAX-4 となっている理由である。) Linux 2.6.19 以降では、このディレクトリ配下のファイルに対して ACL を設 定でき、オブジェクトへの許可をユーザ単位、グループ単位で制御すること が できる。 準拠 POSIX.1-2001. 注意 System V セマフォ (semget(2), semop(2) など) は古いセマフォ API である 。 POSIX セマフォは System V よりも簡単で、うまく設計されたインタフェー ス を提供している。一方で、POSIX セマフォは System V セマフォと比べると 利用できるシステムが少ない (特に、古いシステムでは少ない)。 例 各種の POSIX セマフォ関数を使用した例が sem_wait(3) に記載されている。 関連項目 sem_close(3), sem_destroy(3), sem_getvalue(3), sem_init(3), sem_open(3), sem_post(3), sem_unlink(3), sem_wait(3), pthreads(7) Linux 2009-02-25 SEM_OVERVIEW(7)
SEM_OVERVIEW(7) Linux Programmer’s Manual SEM_OVERVIEW(7) NAME sem_overview - Overview of POSIX semaphores DESCRIPTION POSIX semaphores allow processes and threads to synchronize their actions. A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (sem_post(3)); and decrement the semaphore value by one (sem_wait(3)). If the value of a semaphore is currently zero, then a sem_wait(3) operation will block until the value becomes greater than zero. POSIX semaphores come in two forms: named semaphores and unnamed semaphores. Named semaphores A named semaphore is identified by a name of the form /somename; that is, a null-terminated string of up to NAME_MAX-4 (i.e., 251) characters consisting of an initial slash, followed by one or more characters, none of which are slashes. Two processes can operate on the same named semaphore by passing the same name to sem_open(3). The sem_open(3) function creates a new named semaphore or opens an existing named semaphore. After the semaphore has been opened, it can be operated on using sem_post(3) and sem_wait(3). When a process has finished using the semaphore, it can use sem_close(3) to close the semaphore. When all processes have finished using the semaphore, it can be removed from the system using sem_unlink(3). Unnamed semaphores (memory-based semaphores) An unnamed semaphore does not have a name. Instead the semaphore is placed in a region of memory that is shared between multiple threads (a thread-shared semaphore) or processes (a process-shared semaphore). A thread-shared semaphore is placed in an area of memory shared between by the threads of a process, for example, a global variable. A process-shared semaphore must be placed in a shared memory region (e.g., a System V shared memory segment created using semget(2), or a POSIX shared memory object built created using shm_open(3)). Before being used, an unnamed semaphore must be initialized using sem_init(3). It can then be operated on using sem_post(3) and sem_wait(3). When the semaphore is no longer required, and before the memory in which it is located is deallocated, the semaphore should be destroyed using sem_destroy(3). The remainder of this section describes some specific details of the Linux implementation of POSIX semaphores. Versions Prior to kernel 2.6, Linux only supported unnamed, thread-shared semaphores. On a system with Linux 2.6 and a glibc that provides the NPTL threading implementation, a complete implementation of POSIX semaphores is provided. Persistence POSIX named semaphores have kernel persistence: if not removed by sem_unlink(3), a semaphore will exist until the system is shut down. Linking Programs using the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library, librt. Accessing named semaphores via the file system On Linux, named semaphores are created in a virtual file system, nor- mally mounted under /dev/shm, with names of the form sem.somename. (This is the reason that semaphore names are limited to NAME_MAX-4 rather than NAME_MAX characters.) Since Linux 2.6.19, ACLs can be placed on files under this directory, to control object permissions on a per-user and per-group basis. CONFORMING TO POSIX.1-2001. NOTES System V semaphores (semget(2), semop(2), etc.) are an older semaphore API. POSIX semaphores provide a simpler, and better designed interface than System V semaphores; on the other hand POSIX semaphores are less widely available (especially on older systems) than System V semaphores. EXAMPLE An example of the use of various POSIX semaphore functions is shown in sem_wait(3). SEE ALSO sem_close(3), sem_destroy(3), sem_getvalue(3), sem_init(3), sem_open(3), sem_post(3), sem_unlink(3), sem_wait(3), pthreads(7) COLOPHON This page is part of release 3.22 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/. Linux 2009-02-25 SEM_OVERVIEW(7)
コロナウイルスの日ごとの感染者数・死者数をグラフ化してみました。どの国が増加傾向にあり、どの国が終息に向かっているかを視覚化しています。
Copyright(C) linux-cmd.com All Rights Reserved. Author Takayuki Yukawa