sem_initのヘルプ・マニュアル
日本語 英語
sem_init --help
man sem_init
SEM_INIT(3) Linux Programmer’s Manual SEM_INIT(3)
名前
sem_init - 名前なしセマフォを初期化する
書式
#include
int sem_init(sem_t *sem, int pshared, unsigned int value);
-lrt または -pthread でリンクする。
説明
sem_init() は、 sem が指すアドレスにある名前なしセマフォを初期化する。
value 引き数はそのセマフォの初期値を指定する。
pshared 引き数は、このセマフォがプロセス内のスレッド間で共有されるの か
、プロセス間で共有されるのか、を示す。
pshared が 0 の場合、セマフォはプロセス内のスレッド間で共有される。セマ
フォはすべてのスレッドから参照可能なアドレスに配置すべきである (例え ば
、大域変数や、ヒープ上に動的に割り当てられた変数など)。
pshared が 0 以外の場合、セマフォはプロセス間で共有される。セマフォは共
有メモリ領域に配置すべきである (shm_open(3), mmap(2), shmget(2) 参照)。
(fork(2) で生成された子プロセスは親プロセスのメモリマッピングを継承する
ため、子プロセスもセマフォにアクセスできる。) 共有メモリ領域にアクセ ス
で きるプロセスならば、どのプロセスでも sem_post(3) や sem_wait(3) など
を使ってセマフォを操作できる。
すでに初期化済のセマフォを初期化した場合の挙動は定義されていない。
返り値
成功すると、 sem_init() は 0 を返す。エラーの場合、-1 を返し、 errno に
エラーを示す値をセットする。
エラー
EINVAL value が SEM_VALUE_MAX を超えている。
ENOSYS pshared が 0 以外だが、システムがプロセス共有セマフォをサポート
していない (sem_overview(7) 参照)。
準拠
POSIX.1-2001.
注意
妙なことに、POSIX.1-2001 は sem_init() が成功した場合に返すべき値を規定
していない。 POSIX.1-2008 ではこれが修正され、成功時には 0 を返すことが
規定された。
関連項目
sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(7)
Linux 2008-07-27 SEM_INIT(3)
SEM_INIT(3) Linux Programmer’s Manual SEM_INIT(3)
NAME
sem_init - initialize an unnamed semaphore
SYNOPSIS
#include
int sem_init(sem_t *sem, int pshared, unsigned int value);
Link with -lrt or -pthread.
DESCRIPTION
sem_init() initializes the unnamed semaphore at the address pointed to
by sem. The value argument specifies the initial value for the
semaphore.
The pshared argument indicates whether this semaphore is to be shared
between the threads of a process, or between processes.
If pshared has the value 0, then the semaphore is shared between the
threads of a process, and should be located at some address that is
visible to all threads (e.g., a global variable, or a variable allo-
cated dynamically on the heap).
If pshared is non-zero, then the semaphore is shared between processes,
and should be located in a region of shared memory (see shm_open(3),
mmap(2), and shmget(2)). (Since a child created by fork(2) inherits
its parent’s memory mappings, it can also access the semaphore.) Any
process that can access the shared memory region can operate on the
semaphore using sem_post(3), sem_wait(3), etc.
Initializing a semaphore that has already been initialized results in
undefined behavior.
RETURN VALUE
sem_init() returns 0 on success; on error, -1 is returned, and errno is
set to indicate the error.
ERRORS
EINVAL value exceeds SEM_VALUE_MAX.
ENOSYS pshared is non-zero, but the system does not support process-
shared semaphores (see sem_overview(7)).
CONFORMING TO
POSIX.1-2001.
NOTES
Bizarrely, POSIX.1-2001 does not specify the value that should be
returned by a successful call to sem_init(). POSIX.1-2008 rectifies
this, specifying the zero return on success.
SEE ALSO
sem_destroy(3), sem_post(3), sem_wait(3), sem_overview(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 2008-07-27 SEM_INIT(3)