OSDN Git Service

56a280cba2af3861a4a8b7290633f95a11a2e345
[linuxjm/LDP_man-pages.git] / release / man2 / semop.2
1 .\" Copyright 1993 Giorgio Ciucci (giorgio@crcc.it)
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Modified 1996-10-22, Eric S. Raymond <esr@thyrsus.com>
26 .\" Modified 2002-01-08, Michael Kerrisk <mtk.manpages@gmail.com>
27 .\" Modified 2003-04-28, Ernie Petrides <petrides@redhat.com>
28 .\" Modified 2004-05-27, Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Modified, 11 Nov 2004, Michael Kerrisk <mtk.manpages@gmail.com>
30 .\"     Language and formatting clean-ups
31 .\"     Added notes on /proc files
32 .\" 2005-04-08, mtk, Noted kernel version numbers for semtimedop()
33 .\" 2007-07-09, mtk, Added an EXAMPLE code segment.
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" This file was generated with po4a. Translate the source file.
38 .\"
39 .\"*******************************************************************
40 .TH SEMOP 2 2013\-04\-17 Linux "Linux Programmer's Manual"
41 .SH 名前
42 semop, semtimedop \- System V セマフォの操作
43 .SH 書式
44 .nf
45 \fB#include <sys/types.h>\fP
46 \fB#include <sys/ipc.h>\fP
47 \fB#include <sys/sem.h>\fP
48 .sp
49 \fBint semop(int \fP\fIsemid\fP\fB, struct sembuf *\fP\fIsops\fP\fB, unsigned \fP\fInsops\fP\fB);\fP
50 .sp
51 \fBint semtimedop(int \fP\fIsemid\fP\fB, struct sembuf *\fP\fIsops\fP\fB, unsigned \fP\fInsops\fP\fB,\fP
52 \fB               struct timespec *\fP\fItimeout\fP\fB);\fP
53 .fi
54 .sp
55 .in -4n
56 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
57 .in
58 .sp
59 \fBsemtimedop\fP(): _GNU_SOURCE
60 .SH 説明
61 System V セマフォ集合 (semaphore set) のメンバーの各セマフォは 以下の関連情報を持っている:
62 .sp
63 .in +4n
64 .nf
65 unsigned short  semval;   /* セマフォ値 */
66 unsigned short  semzcnt;  /* ゼロを待つプロセス数 */
67 unsigned short  semncnt;  /* 増加を待つプロセス数 */
68 pid_t           sempid;   /* 最後に操作を行なったプロセスの ID */
69 .sp
70 .in -4n
71 .fi
72 \fBsemop\fP()  は \fIsemid\fP で指定されたセマフォ集合の選択されたセマフォに対して操作を行う。 \fIsops\fP は \fInsops\fP
73 個の要素の配列を指し、配列の各要素は個々のセマフォに 対する操作を示す。その型は \fIstruct sembuf\fP で、次のメンバを持つ:
74 .sp
75 .in +4n
76 .nf
77 unsigned short sem_num;  /* セマフォ番号 */
78 short          sem_op;   /* セマフォ操作 */
79 short          sem_flg;  /* 操作フラグ */
80 .sp
81 .in -4n
82 .fi
83 \fIsem_flg\fP には \fBIPC_NOWAIT\fP と \fBSEM_UNDO\fP が設定できる。 \fBSEM_UNDO\fP
84 が指定された操作は、そのプロセスが終了した時に自動的に取り消される。
85 .PP
86 \fIsops\fP に含まれる操作の集合は、 \fI配列の順序\fP で、 \fIアトミックに\fP 実行される。
87 すなわち、全ての操作が完全に実行されるか、全く実行されないかの どちらかとなる。 全ての操作が直ちに実行できない場合のこのシステムコールの振る舞いは
88 個々の操作の \fIsem_flg\fP フィールドに \fBIPC_NOWAIT\fP が存在するかによって決まり、後述のようになる。
89
90 それぞれの操作はセマフォ集合の \fIsem_num\fP番目 のセマフォに対して実行される。セマフォ集合の最初のセマフォには 番号 0 が振られる。
91 そして操作は三種類あり、 \fIsem_op\fP の値で区別される。
92 .PP
93 If \fIsem_op\fP is a positive integer, the operation adds this value to the
94 semaphore value (\fIsemval\fP).  Furthermore, if \fBSEM_UNDO\fP is specified for
95 this operation, the system subtracts the value \fIsem_op\fP from the semaphore
96 adjustment (\fIsemadj\fP)  value for this semaphore.  This operation can always
97 proceed\(emit never forces a thread to wait.  The calling process must have
98 alter permission on the semaphore set.
99 .PP
100 \fIsem_op\fP が 0 の場合、「ゼロまで待つ」操作である。この場合、プロセスは そのセマフォ集合に対する読み込み許可がなければならない。
101 \fIsemval\fP が 0 ならば、操作は直ちに行われる。 \fIsemval\fP が 0 でない場合、 \fIsem_flg\fP に
102 \fBIPC_NOWAIT\fP が指定されていれば、 \fBsemop\fP()  は失敗し、 \fBerrno\fP に \fBEAGAIN\fP が設定される (このとき
103 \fIsops\fP に対する操作は全く実行されない)。 \fIsem_flg\fP に \fBIPC_NOWAIT\fP が指定されていない場合、 \fIsemzcnt\fP
104 (セマフォ値が 0 になるのを待っているスレッドの数) を 1 増加させて、 以下のいずれかが起こるまでスレッドを停止 (sleep) する。
105 .IP \(bu 3
106 \fIsemval\fP が 0 になった: このとき \fIsemzcnt\fP の値は 1 減算される。
107 .IP \(bu
108 セマフォ集合が削除された: このとき \fBsemop\fP()  は失敗し、 \fIerrno\fP に \fBEIDRM\fP が設定される。
109 .IP \(bu
110 呼び出し元スレッドがシグナルを捕獲した: このとき \fIsemzcnt\fP の値は 1 減算され、 \fBsemop\fP()  は失敗し \fIerrno\fP に
111 \fBEINTR\fP が設定される。
112 .IP \(bu
113 \fBsemtimedop\fP()  の \fItimeout\fP で指定された制限時間が経過した: このとき \fBsemtimedop\fP()  は失敗し、
114 \fIerrno\fP に \fBEAGAIN\fP が設定される。
115 .PP
116 If \fIsem_op\fP is less than zero, the process must have alter permission on
117 the semaphore set.  If \fIsemval\fP is greater than or equal to the absolute
118 value of \fIsem_op\fP, the operation can proceed immediately: the absolute
119 value of \fIsem_op\fP is subtracted from \fIsemval\fP, and, if \fBSEM_UNDO\fP is
120 specified for this operation, the system adds the absolute value of
121 \fIsem_op\fP to the semaphore adjustment (\fIsemadj\fP)  value for this
122 semaphore.  If the absolute value of \fIsem_op\fP is greater than \fIsemval\fP,
123 and \fBIPC_NOWAIT\fP is specified in \fIsem_flg\fP, \fBsemop\fP()  fails, with
124 \fIerrno\fP set to \fBEAGAIN\fP (and none of the operations in \fIsops\fP is
125 performed).  Otherwise \fIsemncnt\fP (the counter of threads waiting for this
126 semaphore's value to increase)  is incremented by one and the thread sleeps
127 until one of the following occurs:
128 .IP \(bu 3
129 \fIsemval\fP becomes greater than or equal to the absolute value of \fIsem_op\fP:
130 the operation now proceeds, as described above.
131 .IP \(bu
132 セマフォ集合がシステムから削除された: このとき \fBsemop\fP()  は失敗し \fIerrno\fP に \fBEIDRM\fP が設定される。
133 .IP \(bu
134 呼び出したスレッドがシグナルを捕獲した: このとき \fIsemncnt\fP が 1 減算され、 \fBsemop\fP()  は失敗し \fIerrno\fP に
135 \fBEINTR\fP が設定される。
136 .IP \(bu
137 \fBsemtimedop\fP()  の \fItimeout\fP で指定された制限時間が経過した: このとき \fBsemtimedop\fP()  は失敗し、
138 \fIerrno\fP に \fBEAGAIN\fP が設定される。
139 .PP
140 .\" and
141 .\" .I sem_ctime
142 操作が成功した場合、 \fIsops\fP が指す配列によって操作対象となった各セマフォの \fIsempid\fP メンバーには呼び出し元のプロセス ID
143 が設定される。さらに \fIsem_otime\fP に現在時刻が設定される。
144 .PP
145 \fBsemtimedop\fP() 関数の振る舞いは \fBsemop\fP() と全く同じだが、呼び出し元
146 スレッドが停止する場合、停止期間の上限が \fItimeout\fP 引き数の指す
147 \fItimespec\fP 構造体で指定された時間となる点だけが異なる (この停止期間は
148 システムクロックの粒度に切り上げられ、カーネルのスケジューリング遅延に
149 より、この停止期間は少しだけ長くなる可能性がある)。
150 指定した制限時間に達した場合は、 \fBsemtimedop\fP() は失敗し、 \fIerrno\fP に
151 \fBEAGAIN\fP が設定される (このとき \fIsops\fP の操作は実行されない)。
152 \fItimeout\fP 引き数が NULL の場合、 \fBsemtimedop\fP() 関数の振る舞いは
153 \fBsemop\fP() 関数と全く同じになる。
154 .SH 返り値
155 成功した場合、 \fBsemop\fP()  と \fBsemtimedop\fP()  は 0 を返す。そうでなければ \-1 を返し、 エラーを示す
156 \fIerrno\fP を設定する。
157 .SH エラー
158 失敗した場合、 \fIerrno\fP に以下のどれかが設定される:
159 .TP 
160 \fBE2BIG\fP
161 \fInsops\fP 引き数が \fBSEMOPM\fP より大きい。 \fBSEMOPM\fP は一回のシステムコールで許される操作の最大個数である。
162 .TP 
163 \fBEACCES\fP
164 呼び出し元プロセスには指定されたセマフォ操作を行うのに 必要なアクセス許可がなく、 \fBCAP_IPC_OWNER\fP ケーパビリティもない。
165 .TP 
166 \fBEAGAIN\fP
167 操作を直ちに処理することができず、かつ \fIsem_flg\fP に \fBIPC_NOWAIT\fP が指定されているか \fItimeout\fP
168 で指定された制限時間が経過した。
169 .TP 
170 \fBEFAULT\fP
171 引き数 \fIsops\fP か \fItimeout\fP が指しているアドレスにアクセスできない。
172 .TP 
173 \fBEFBIG\fP
174 ある操作で、 \fIsem_num\fP の値が 0 未満か、集合内のセマフォの数以上である。
175 .TP 
176 \fBEIDRM\fP
177 セマフォ集合が削除された。
178 .TP 
179 \fBEINTR\fP
180 このシステムコールで停止している時にスレッドがシグナルを捕獲した。 \fBsingle\fP(7) 参照。
181 .TP 
182 \fBEINVAL\fP
183 セマフォ集合が存在しないか、 \fIsemid\fP が 0 未満であるか、 \fInsops\fP が正の数でない。
184 .TP 
185 \fBENOMEM\fP
186 ある操作で \fIsem_flg\fP に \fBSEM_UNDO\fP が指定されたが、システムにアンドゥ構造体に割り当てる十分なメモリがない。
187 .TP 
188 \fBERANGE\fP
189 ある操作で \fIsem_op+semval\fP が \fBSEMVMX\fP より大きい。 \fBSEMVMX\fP は \fIsemval\fP
190 の最大値で、その値は実装依存である。
191 .SH バージョン
192 \fBsemtimedop\fP()  は Linux 2.5.52 で初めて登場し、 それからカーネル 2.4.22 にも移植された。
193 \fBsemtimedop\fP()  の glibc でのサポートはバージョン 2.3.3 で初めて登場した。
194 .SH 準拠
195 .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
196 SVr4, POSIX.1\-2001.
197 .SH 注意
198 .\" Like Linux, the FreeBSD man pages still document
199 .\" the inclusion of these header files.
200 Linux や POSIX の全てのバージョンでは、 \fI<sys/types.h>\fP と \fI<sys/ipc.h>\fP
201 のインクルードは必要ない。しかしながら、いくつかの古い実装ではこれらのヘッダファイルのインクルードが必要であり、 SVID
202 でもこれらのインクルードをするように記載されている。このような古いシステムへの移植性を意図したアプリケーションではこれらのファイルをインクルードする必要があるかもしれない。
203
204 あるプロセスの \fIsem_undo\fP 構造体は \fBfork\fP(2)  で生成された子プロセスには継承されないが、 \fBexecve\fP(2)
205 システムコールの場合は継承される。
206 .PP
207 \fBsemop\fP()  はシグナルハンドラによって中断された後に、 決して自動的に再開することはない。 たとえシグナルハンドラの設定時に
208 \fBSA_RESTART\fP フラグがセットされていても再開することはない
209
210 A semaphore adjustment (\fIsemadj\fP)  value is a per\-process, per\-semaphore
211 integer that is the negated sum of all operations performed on a semaphore
212 specifying the \fBSEM_UNDO\fP flag.  Each process has a list of \fIsemadj\fP
213 values\(emone value for each semaphore on which it has operated using
214 \fBSEM_UNDO\fP.  When a process terminates, each of its per\-semaphore \fIsemadj\fP
215 values is added to the corresponding semaphore, thus undoing the effect of
216 that process's operations on the semaphore (but see BUGS below).  When a
217 semaphore's value is directly set using the \fBSETVAL\fP or \fBSETALL\fP request
218 to \fBsemctl\fP(2), the corresponding \fIsemadj\fP values in all processes are
219 cleared.
220 .PP
221 あるセマフォの \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, \fIsemnct\fP の値はいずれも、適切な操作を指定して
222 \fBsemctl\fP(2)  を呼び出すことで取得できる。
223 .PP
224 セマフォ集合のリソースに関する制限のうち、 \fBsemop\fP()  に影響を及ぼすものを以下に挙げる:
225 .TP 
226 \fBSEMOPM\fP
227 .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
228 一回の \fBsemop\fP()  で許される操作の最大数 (32)。 (Linux では、この制限値は \fI/proc/sys/kernel/sem\fP
229 の第3フィールドに対応し、読み出しも変更もできる)。
230 .TP 
231 \fBSEMVMX\fP
232 \fIsemval\fP が取り得る最大値: 実装依存 (32767)。
233 .PP
234 以下の値に関しては実装依存の制限はない。 終了時の調整 (adjust on exit) の最大値 (\fBSEMAEM\fP)、
235 システム全体のアンドゥ構造体の最大数 (\fBSEMMNU\fP)、 プロセスあたりのアンドゥ構造体の最大数。
236 .SH バグ
237 プロセスが終了する際、プロセスに対応する \fIsemadj\fP の集合を使って、 \fBSEM_UNDO\fP
238 フラグ付きで実行された全てのセマフォ操作の影響を取り消す。 これによりある問題が発生する: これらのセマフォの調整を行っていると、 中にはセマフォの値が
239 0 未満の値にしようとする場合が出てくる。 このような場合、どのように実装するべきか? ひとつの考えられる手法は、全てのセマフォ調整が実行されるまで
240 停止することである。しかし、この方法ではプロセスの終了が 長時間にわたって停止されることがあるので望ましくない。
241 しかもどれくらい長時間になるかは分からない。 別の選択肢として、このようなセマフォ調整を完全に無視してしまう方法がある (これはセマフォ操作として
242 \fBIPC_NOWAIT\fP が指定するのと少し似ている)。 Linux は第三の手法を採用している: セマフォの値を出来るだけ (つまり 0 まで)
243 減少させて、プロセスの終了を直ちに続行できるようにしている。
244
245 .\" The bug report:
246 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
247 .\" the fix:
248 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
249 カーネル 2.6.x (x <= 10) には、ある状況においてセマフォ値が 0 になるのを 待っているスレッドが、セマフォ値が実際に 0
250 になったときに起床 (wake up)  されない、というバグがある。このバグはカーネル 2.6.11 で修正されている。
251 .SH 例
252 以下の部分的なコードは、 セマフォ 0 の値が 0 になるのを待ってから、 セマフォの値を 1 加算する処理を、 \fBsemop\fP()
253 を使ってアトミック (atomically) に行う。
254 .nf
255
256     struct sembuf sops[2];
257     int semid;
258
259     /* Code to set \fIsemid\fP omitted */
260
261     sops[0].sem_num = 0;        /* Operate on semaphore 0 */
262     sops[0].sem_op = 0;         /* Wait for value to equal 0 */
263     sops[0].sem_flg = 0;
264
265     sops[1].sem_num = 0;        /* Operate on semaphore 0 */
266     sops[1].sem_op = 1;         /* Increment value by one */
267     sops[1].sem_flg = 0;
268
269     if (semop(semid, sops, 2) == \-1) {
270         perror("semop");
271         exit(EXIT_FAILURE);
272     }
273 .fi
274 .SH 関連項目
275 \fBclone\fP(2), \fBsemctl\fP(2), \fBsemget\fP(2), \fBsigaction\fP(2),
276 \fBcapabilities\fP(7), \fBsem_overview\fP(7), \fBsvipc\fP(7), \fBtime\fP(7)
277 .SH この文書について
278 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.51 の一部
279 である。プロジェクトの説明とバグ報告に関する情報は
280 http://www.kernel.org/doc/man\-pages/ に書かれている。