#include <sys/sem.h>
int semop(int semid, struct sembuf *sops, size_t nsops);
int semtimedop(int semid, struct sembuf *sops, size_t nsops,
const struct timespec *_Nullable timeout);
glibc 向けの機能検査マクロの要件 (feature_test_macros(7) 参照):
semtimedop():
_GNU_SOURCE
unsigned short semval; /* semaphore value */
unsigned short semzcnt; /* # waiting for zero */
unsigned short semncnt; /* # waiting for increase */
pid_t sempid; /* PID of process that last
modified the semaphore value */
semop() は semid で指定されたセマフォ集合の選択されたセマフォに対して操作を行う。 sops は nsops 個の要素の配列を指し、配列の各要素は個々のセマフォに対する操作を示す構造体である。その型は struct sembuf で、次のメンバを持つ:
unsigned short sem_num; /* セマフォ番号 */ short sem_op; /* セマフォ操作 */ short sem_flg; /* 操作フラグ */
sem_flg には IPC_NOWAIT と SEM_UNDO が設定できる。 SEM_UNDO が指定された操作は、そのプロセスが終了した時に自動的に取り消される。
sops に含まれる操作の集合は、 配列の順序 で、 アトミックに 実行される。 すなわち、全ての操作が完全に実行されるか、全く実行されないかの どちらかとなる。 全ての操作が直ちに実行できない場合のこのシステムコールの振る舞いは 個々の操作の sem_flg フィールドに IPC_NOWAIT が存在するかによって決まり、後述のようになる。
それぞれの操作はセマフォ集合の sem_num番目 のセマフォに対して実行される。セマフォ集合の最初のセマフォには 番号 0 が振られる。 そして操作は三種類あり、 sem_op の値で区別される。
If sem_op is a positive integer, the operation adds this value to the semaphore value (semval). Furthermore, if SEM_UNDO is specified for this operation, the system subtracts the value sem_op from the semaphore adjustment (semadj) value for this semaphore. This operation can always proceed---it never forces a thread to wait. The calling process must have alter permission on the semaphore set.
sem_op が 0 の場合、「ゼロまで待つ」操作である。この場合、プロセスは そのセマフォ集合に対する読み込み許可がなければならない。 semval が 0 ならば、操作は直ちに行われる。 semval が 0 でない場合、 sem_flg に IPC_NOWAIT が指定されていれば、 semop() は失敗し、 errno に EAGAIN が設定される (このとき sops に対する操作は全く実行されない)。 sem_flg に IPC_NOWAIT が指定されていない場合、 semzcnt (セマフォ値が 0 になるのを待っているスレッドの数) を 1 増加させて、 以下のいずれかが起こるまでスレッドを停止 (sleep) する。
sem_op が 0 未満の場合、プロセスにはそのセマフォ集合を変更する許可がなければ ならない。 semval が sem_op の絶対値以上の場合は、操作は直ちに実行される: semval から sem_op の絶対値を減算し、さらに、この操作に SEM_UNDO が指定されている場合は、このセマフォの調整値 (semadj) に sem_op の絶対値を加算する。 semval が sem_op の絶対値より小さく、 sem_flg に IPC_NOWAIT が指定された場合は、 semop() は失敗し、 errno に EAGAIN が設定される (このとき sops の操作は全く実行されない)。 semval が sem_op の絶対値より小さく、 IPC_WAIT が指定されていない場合は、 semncnt (このセマフォの値が増加するのを待っているスレッド数のカウンター) を 1 増加させて、以下のいずれかが起こるまでスレッドを停止 (sleep) する。
操作が成功した場合、 sops が指す配列によって操作対象となった各セマフォの sempid メンバーには呼び出し元のプロセス ID が設定される。さらに sem_otime に現在時刻が設定される。
semtimedop() がシグナルにより割り込まれた場合、呼び出しはエラー EINTR で失敗し、 timeout の内容は変更されないままとなる点に注意すること。
semop() はシグナルハンドラーによって中断された後に、 決して自動的に再開することはない。 たとえシグナルハンドラーの設定時に SA_RESTART フラグがセットされていても再開することはない
A semaphore adjustment (semadj) value is a per-process, per-semaphore integer that is the negated sum of all operations performed on a semaphore specifying the SEM_UNDO flag. Each process has a list of semadj values---one value for each semaphore on which it has operated using SEM_UNDO. When a process terminates, each of its per-semaphore semadj values is added to the corresponding semaphore, thus undoing the effect of that process's operations on the semaphore (but see BUGS below). When a semaphore's value is directly set using the SETVAL or SETALL request to semctl(2), the corresponding semadj values in all processes are cleared. The clone(2) CLONE_SYSVSEM flag allows more than one process to share a semadj list; see clone(2) for details.
あるセマフォの semval, sempid, semzcnt, semnct の値はいずれも、適切な操作を指定して semctl(2) を呼び出すことで取得できる。
以下の値に関しては実装依存の制限はない。 終了時の調整 (adjust on exit) の最大値 (SEMAEM)、 システム全体のアンドゥ構造体の最大数 (SEMMNU)、 プロセスあたりのアンドゥ構造体の最大数。
In Linux 2.6.x, x <= 10, there is a bug that in some circumstances prevents a thread that is waiting for a semaphore value to become zero from being woken up when the value does actually become zero. This bug is fixed in Linux 2.6.11.
struct sembuf sops[2];
int semid;
/* Code to set semid omitted */
sops[0].sem_num = 0; /* Operate on semaphore 0 */
sops[0].sem_op = 0; /* Wait for value to equal 0 */
sops[0].sem_flg = 0;
sops[1].sem_num = 0; /* Operate on semaphore 0 */
sops[1].sem_op = 1; /* Increment value by one */
sops[1].sem_flg = 0;
if (semop(semid, sops, 2) == -1) {
perror("semop");
exit(EXIT_FAILURE);
}
A further example of the use of semop() can be found in shmop(2).