1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
24 .\" Modified 2002-01-08, Michael Kerrisk <mtk.manpages@gmail.com>
25 .\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
26 .\" Modified 2004-05-27, Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
28 .\" Language and formatting clean-ups
29 .\" Added notes on /proc files
30 .\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
31 .\" 2007-07-09, mtk, Added an EXAMPLE code segment.
33 .\" Japanese Version Copyright (c) 1997 HANATAKA Shinya
34 .\" all rights reserved.
35 .\" Translated 1997-02-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
36 .\" Updated 2003-02-09, Kentaro Shirakata <argrath@ub32.org>
37 .\" Updated 2003-09-12, Kentaro Shirakata <argrath@ub32.org>
38 .\" Updated 2005-03-01, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
39 .\" Updated 2005-04-17, Akihiro MOTOKI
40 .\" Updated 2005-09-06, Akihiro MOTOKI
41 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
43 .\"WORD: semaphore セマフォ
46 .\"WORD: system call システムコール
49 .\"WORD: catch 捕獲(catch)
50 .\"WORD: process-ID プロセスID
54 .\"WORD: free 解放(free).
56 .\"WORD: anonymous structure 無名構造体
57 .\"WORD: time limit 制限時間
59 .TH SEMOP 2 2008-10-04 "Linux" "Linux Programmer's Manual"
61 semop, semtimedop \- セマフォの操作
64 .B #include <sys/types.h>
65 .B #include <sys/ipc.h>
66 .B #include <sys/sem.h>
68 .BI "int semop(int " semid ", struct sembuf *" sops ", unsigned " nsops );
70 .BI "int semtimedop(int " semid ", struct sembuf *" sops ", unsigned " nsops ,
71 .BI " struct timespec *" timeout );
76 .RB ( feature_test_macros (7)
83 セマフォ集合 (semaphore set) のメンバーの各セマフォは
88 unsigned short semval; /* セマフォ値 */
89 unsigned short semzcnt; /* ゼロを待つプロセス数 */
90 unsigned short semncnt; /* 増加を待つプロセス数 */
91 pid_t sempid; /* 最後に操作を行なったプロセス */
98 で指定されたセマフォ集合の選択されたセマフォに対して操作を行う。
102 個の要素の配列を指し、配列の各要素は個々のセマフォに
109 unsigned short sem_num; /* セマフォ番号 */
110 short sem_op; /* セマフォ操作 */
111 short sem_flg; /* 操作フラグ */
122 が指定された操作は、そのプロセスが終了した時に自動的に取り消される。
130 すなわち、全ての操作が完全に実行されるか、全く実行されないかの
133 全ての操作が直ちに実行できない場合のこのシステムコールの振る舞いは
138 が存在するかによって決まり、後述のようになる。
142 のセマフォに対して実行される。セマフォ集合の最初のセマフォには
149 が正の整数の場合、操作としてその値をセマフォの値
153 が指定されている場合は、システムはこのセマフォの プロセス・アンドゥ数
156 この操作は必ず実行でき、プロセスの停止は起こらない。
157 呼び出し元プロセスは対象のセマフォ集合を変更する許可がなければならない。
160 が 0 の場合、「ゼロまで待つ」操作である。この場合、プロセスは
161 そのセマフォ集合に対する読み込み許可がなければならない。
183 (セマフォ値が 0 になるのを待っているプロセスの数) を 1 増加させて、
184 以下のいずれかが起こるまでプロセスを停止 (sleep) する。
199 呼び出し元プロセスがシグナルを捕獲した: このとき
209 .\" motoki: semop () は semtimedop () の間違い?
213 で指定された制限時間が経過した: このとき
222 が 0 未満の場合、プロセスにはそのセマフォ集合を変更する許可がなければ
227 の絶対値以上の場合は、操作は直ちに実行される:
234 が指定されている場合は、このセマフォのプロセス・アンドゥ数
256 (このセマフォの値が増加するのを待っているプロセス数のカウンタ)
257 を 1 増加させて、以下のいずれかが起こるまでプロセスを停止 (sleep) する。
271 が指定されていた場合にはこのセマフォのプロセス・アンドゥ数
275 セマフォ集合がシステムから削除された: このとき
283 呼び出したプロセスがシグナルを捕獲した: このとき
293 .\" motoki: the system call は semtimedop () のこと?
297 で指定された制限時間が経過した: このとき
307 が指す配列によって操作対象となった各セマフォの
309 メンバーには呼び出したプロセスのプロセス ID が設定される。
320 呼び出し元プロセスが停止する場合、停止期間の上限が
324 構造体で指定された時間となる点だけが異なる。
346 は 0 を返す。そうでなければ \-1 を返し、
361 は一回のシステムコールで許される操作の最大個数である。
364 呼び出し元プロセスには指定されたセマフォ操作を行うのに
388 の値が 0 未満か、集合内のセマフォの数以上である。
394 このシステムコールで停止している時にプロセスがシグナルを捕獲した。
410 が指定されたが、システムにアンドゥ構造体に割り当てる十分なメモリがない。
424 は Linux 2.5.52 で初めて登場し、
425 それからカーネル 2.4.22 にも移植された。
427 の glibc でのサポートはバージョン 2.3.3 で初めて登場した。
430 .\" SVr4 には、追加で EINVAL, EFBIG, ENOSPC のエラー状態についての
437 で生成された子プロセスには継承されないが、
442 はシグナルハンドラによって中断された後に、
446 フラグがセットされていても再開することはない
451 フラグを設定して実行された全てのセマフォ操作の(負数の)カウンタである。
458 直接設定された場合には、全てのプロセスにおいて対応する
462 あるセマフォの \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, \fIsemnct\fP
467 セマフォ集合のリソースに関する制限のうち、
476 .I /proc/sys/kernel/sem
477 の第3フィールドに対応し、読み出しも変更もできる)。
478 .\" この /proc ファイルは Linux 2.2 以前では利用できない -- MTK
482 が取り得る最大値: 実装依存 (32767)。
485 終了時の調整 (adjust on exit) の最大値
495 フラグ付きで実行された全てのセマフォ操作の影響を取り消す。
496 これによりある問題が発生する: これらのセマフォの調整を行っていると、
497 中にはセマフォの値が 0 未満の値にしようとする場合が出てくる。
498 このような場合、どのように実装するべきか?
499 ひとつの考えられる手法は、全てのセマフォ調整が実行されるまで
500 停止することである。しかし、この方法ではプロセスの終了が
501 長時間にわたって停止されることがあるので望ましくない。
502 しかもどれくらい長時間になるかは分からない。
503 別の選択肢として、このようなセマフォ調整を完全に無視してしまう方法がある
507 Linux は第三の手法を採用している: セマフォの値を出来るだけ (つまり
508 0 まで) 減少させて、プロセスの終了を直ちに続行できるようにしている。
510 カーネル 2.6.x (x <= 10) には、ある状況においてセマフォ値が 0 になるのを
511 待っているプロセスが、セマフォ値が実際に 0 になったときに起床 (wake up)
512 されない、というバグがある。このバグはカーネル 2.6.11 で修正されている。
514 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
516 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
519 セマフォ 0 の値が 0 になるのを待ってから、
522 を使ってアトミック (atomically) に行う。
525 struct sembuf sops[2];
528 /* Code to set \fIsemid\fP omitted */
530 sops[0].sem_num = 0; /* Operate on semaphore 0 */
531 sops[0].sem_op = 0; /* Wait for value to equal 0 */
534 sops[1].sem_num = 0; /* Operate on semaphore 0 */
535 sops[1].sem_op = 1; /* Increment value by one */
538 if (semop(semid, sops, 2) == \-1) {
547 .BR capabilities (7),
548 .BR sem_overview (7),