semgetのヘルプ・マニュアル
日本語 英語
semget --help
man semget
SEMGET(2) Linux Programmer’s Manual SEMGET(2)
名前
semget - セマフォ集合の識別子を取得する
書式
#include
#include
#include
int semget(key_t key, int nsems, int semflg);
説明
semget() システムコールは、引き数 key に対応するセマフォ集合 (semaphore
set) の識別子 (identifier) を返す。 key の値が IPC_PRIVATE の場合、もし
く は semflg に IPC_CREAT が指定されていて、 key に対応するセマフォ集合
が存在しない場合、 nsems 個のセマフォからなる新しい集合が作成される。
semflg に IPC_CREAT と IPC_EXCL の両方が指定された場合、 key に対応する
セマフォ集合が既に存在すると、 semget() は失敗し、 errno に EEXIST が設
定される (これは open(2) に O_CREAT | O_EXCL が指定された場合の動作と同
じである)。
セマフォ集合作成時に、引き数 semflg の下位 9 ビットは、そのセマフォ集合
の (所有者 (owner)、グループ (group)、他人 (others) に対する) アクセ ス
許可の定義として使用される。これらのビットは open(2) の引き数 mode と同
じ形式で同じ意味である (但し、実行 (execute) 許可はセマフォでは意味を持
た ず、書き込み (write) 許可はセマフォ値の変更 (alter) 許可として機能す
る)。
新しく作成されたセマフォ集合の各セマフォの値は不定である ( こ の 点 は
POSIX.1-2001 に明記されている)。 Linux は他の多くの実装と同様にセマフォ
値を 0 に初期化するが、移植性を考慮したアプリケーションではこの動作を前
提 にすべきではない。アプリケーションは明示的にセマフォを希望の値で初期
化すべきである。
新規のセマフォ集合を作成する際、 semget() はセマフォ集合の情報を保持 す
る デ ー タ 構造体 semid_ds を次のように初期化する (semid_ds については
semctl(2) を参照):
sem_perm.cuid と sem_perm.uid に、呼び出し元のプロセ ス の 実 効
(effective) ユーザ ID を設定する。
sem_perm.cgid と sem_perm.gid に、呼び出し元のプロセスの実効
(effective) グループ ID を設定する。
sem_perm.mode の下位 9 ビットに semflg の下位 9 ビットを設定する
。
sem_nsems に nsems の値を設定する。
sem_otime に 0 を設定する。
sem_ctime に現在の時刻を設定する。
セ マフォ集合の作成を行わない場合は、引き数 nsems に (don’t care を意味
する) 0 を指定してもよい。そうでない場合は、 nsems は 0 より大きい値 で
な ければならず、セマフォ集合あたりのセマフォの最大数 (SEMMSL) 以下でな
ければならない。
セマフォ集合が既に存在した場合は、アクセス許可の検査が行われる。
返り値
成功した場合、セマフォ集合の識別子 (非負の整数) が返り値となる。失敗 し
た場合は -1 が返され、 errno にエラーを示す値が設定される。
エラー
失敗した場合、 errno には以下の値のいずれか一つが設定される:
EACCES key に対応するセマフォ集合は存在するが、呼び出し元のプロセスはそ
の集合へのアクセス許可がなく、 CAP_IPC_OWNER ケーパビリティも 持
っていない。
EEXIST key に 対応するセマフォ集合が存在し、 semflg には IPC_CREAT と
IPC_EXCL が指定されていた。
EINVAL nsems が 0 より小さいか、セマフォ集合あたりのセマフォの 最 大 数
(SEMMSL) より大きい。または、 key に対応するセマフォ集合が既に存
在し、 nsems がその集合のセマフォ数よりも大きい。
ENOENT key に対応するセマフォ集合が存在せず、 semflg に IPC_CREAT が 指
定されてもいない。
ENOMEM セマフォ集合を作成しようとしたが、新しいデータ構造体を作成するの
に十分なメモリがシステムに存在しない。
ENOSPC セマフォ集合を作成しようとすると、システムのセマフォ集合の最大数
(SEMMNI) か、システム全体のセマフォの最大数 (SEMMNS) のいずれか
を超えてしまう。
準拠
SVr4, POSIX.1-2001.
注意
IPC_PRIVATE はフラグ・フィールドに指定するものではなく、 key_t 型である
。この特別な値が key に指定されると、 semget() semflg の下位 9 ビット以
外は全て無視し、 (成功した場合は) 新しいセマフォ集合を作成する。
セマフォ集合のリソースに関する制限のうち、 semget() に影響を及ぼすも の
を以下に挙げる:
SEMMNI システム全体のセマフォ集合の最大数: 方針依存 (Linux では、この制
限値は /proc/sys/kernel/sem の第4フィールドに対応し、読み出し も
変更もできる)。
SEMMSL semid あたりのセマフォの最大数: 実装依存 (Linux では、この制限値
は /proc/sys/kernel/sem の第1フィールドに対応し、読み出しも変 更
もできる)。
SEMMNS システム全体のセマフォの最大数: 方針依存 (Linux では、この制限値
は /proc/sys/kernel/sem の第2フィールドに対応し、読み出しも変 更
もできる)。 SEMMSL * SEMMNI より大きな値は意味を持たない。
バグ
IPC_PRIVATE という名前を選んだのはおそらく失敗であろう。 IPC_NEW の方が
より明確にその機能を表しているだろう。
セマフォ集合内のセマフォは semget() では初期化されない。このセマフォ を
初 期 化 す る に は、セマフォ集合に対して semctl(2) を使って SETVAL か
SETALL 操作を実行する必要がある。 (複数箇所からセマフォ集合の操作が行わ
れ る場面では、誰が最初に集合を初期化すればよいか分からない。この状況を
避けるには、 semctl(2) の IPC_STAT 操作で取得できるセマフォのデータ構造
体の sem_otime が 0 以外になっているかをチェックすればよい。)
関連項目
semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7),
svipc(7)
Linux 2004-05-27 SEMGET(2)
SEMGET(2) Linux Programmer’s Manual SEMGET(2)
NAME
semget - get a semaphore set identifier
SYNOPSIS
#include
#include
#include
int semget(key_t key, int nsems, int semflg);
DESCRIPTION
The semget() system call returns the semaphore set identifier associ-
ated with the argument key. A new set of nsems semaphores is created
if key has the value IPC_PRIVATE or if no existing semaphore set is
associated with key and IPC_CREAT is specified in semflg.
If semflg specifies both IPC_CREAT and IPC_EXCL and a semaphore set
already exists for key, then semget() fails with errno set to EEXIST.
(This is analogous to the effect of the combination O_CREAT | O_EXCL
for open(2).)
Upon creation, the least significant 9 bits of the argument semflg
define the permissions (for owner, group and others) for the semaphore
set. These bits have the same format, and the same meaning, as the
mode argument of open(2) (though the execute permissions are not mean-
ingful for semaphores, and write permissions mean permission to alter
semaphore values).
The values of the semaphores in a newly created set are indeterminate.
(POSIX.1-2001 is explicit on this point.) Although Linux, like many
other implementations, initializes the semaphore values to 0, a
portable application cannot rely on this: it should explicitly initial-
ize the semaphores to the desired values.
When creating a new semaphore set, semget() initializes the set’s asso-
ciated data structure, semid_ds (see semctl(2)), as follows:
sem_perm.cuid and sem_perm.uid are set to the effective user ID
of the calling process.
sem_perm.cgid and sem_perm.gid are set to the effective group ID
of the calling process.
The least significant 9 bits of sem_perm.mode are set to the
least significant 9 bits of semflg.
sem_nsems is set to the value of nsems.
sem_otime is set to 0.
sem_ctime is set to the current time.
The argument nsems can be 0 (a don’t care) when a semaphore set is not
being created. Otherwise nsems must be greater than 0 and less than or
equal to the maximum number of semaphores per semaphore set (SEMMSL).
If the semaphore set already exists, the permissions are verified.
RETURN VALUE
If successful, the return value will be the semaphore set identifier (a
non-negative integer), otherwise -1 is returned, with errno indicating
the error.
ERRORS
On failure errno will be set to one of the following:
EACCES A semaphore set exists for key, but the calling process does not
have permission to access the set, and does not have the
CAP_IPC_OWNER capability.
EEXIST A semaphore set exists for key and semflg specified both
IPC_CREAT and IPC_EXCL.
EINVAL nsems is less than 0 or greater than the limit on the number of
semaphores per semaphore set (SEMMSL), or a semaphore set corre-
sponding to key already exists, and nsems is larger than the
number of semaphores in that set.
ENOENT No semaphore set exists for key and semflg did not specify
IPC_CREAT.
ENOMEM A semaphore set has to be created but the system does not have
enough memory for the new data structure.
ENOSPC A semaphore set has to be created but the system limit for the
maximum number of semaphore sets (SEMMNI), or the system wide
maximum number of semaphores (SEMMNS), would be exceeded.
CONFORMING TO
SVr4, POSIX.1-2001.
NOTES
IPC_PRIVATE isn’t a flag field but a key_t type. If this special value
is used for key, the system call ignores everything but the least sig-
nificant 9 bits of semflg and creates a new semaphore set (on success).
The following limits on semaphore set resources affect the semget()
call:
SEMMNI System wide maximum number of semaphore sets: policy dependent
(on Linux, this limit can be read and modified via the fourth
field of /proc/sys/kernel/sem).
SEMMSL Maximum number of semaphores per semid: implementation dependent
(on Linux, this limit can be read and modified via the first
field of /proc/sys/kernel/sem).
SEMMNS System wide maximum number of semaphores: policy dependent (on
Linux, this limit can be read and modified via the second field
of /proc/sys/kernel/sem). Values greater than SEMMSL * SEMMNI
makes it irrelevant.
BUGS
The name choice IPC_PRIVATE was perhaps unfortunate, IPC_NEW would more
clearly show its function.
The semaphores in a set are not initialized by semget(). In order to
initialize the semaphores, semctl(2) must be used to perform a SETVAL
or a SETALL operation on the semaphore set. (Where multiple peers do
not know who will be the first to initialize the set, checking for a
non-zero sem_otime in the associated data structure retrieved by a sem-
ctl(2) IPC_STAT operation can be used to avoid races.)
SEE ALSO
semctl(2), semop(2), ftok(3), capabilities(7), sem_overview(7),
svipc(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 2004-05-27 SEMGET(2)