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"
62 .\"O semop, semtimedop \- semaphore operations
63 semop, semtimedop \- セマフォの操作
67 .B #include <sys/types.h>
68 .B #include <sys/ipc.h>
69 .B #include <sys/sem.h>
71 .BI "int semop(int " semid ", struct sembuf *" sops ", unsigned " nsops );
73 .BI "int semtimedop(int " semid ", struct sembuf *" sops ", unsigned " nsops ,
74 .BI " struct timespec *" timeout );
78 .\"O Feature Test Macro Requirements for glibc (see
79 .\"O .BR feature_test_macros (7)):
81 .RB ( feature_test_macros (7)
89 .\"O Each semaphore in a semaphore set has the following associated values:
90 セマフォ集合 (semaphore set) のメンバーの各セマフォは
95 .\"O unsigned short semval; /* semaphore value */
96 .\"O unsigned short semzcnt; /* # waiting for zero */
97 .\"O unsigned short semncnt; /* # waiting for increase */
98 .\"O pid_t sempid; /* process that did last op */
99 unsigned short semval; /* セマフォ値 */
100 unsigned short semzcnt; /* ゼロを待つプロセス数 */
101 unsigned short semncnt; /* 増加を待つプロセス数 */
102 pid_t sempid; /* 最後に操作を行なったプロセス */
107 .\"O performs operations on selected semaphores in the set indicated by
112 で指定されたセマフォ集合の選択されたセマフォに対して操作を行う。
115 .\"O elements in the array pointed to by
117 .\"O specifies an operation to be performed on a single semaphore.
118 .\"O The elements of this structure are of type
119 .\"O .IR "struct sembuf" ,
120 .\"O containing the following members:
124 個の要素の配列を指し、配列の各要素は個々のセマフォに
131 .\"O unsigned short sem_num; /* semaphore number */
132 .\"O short sem_op; /* semaphore operation */
133 .\"O short sem_flg; /* operation flags */
134 unsigned short sem_num; /* セマフォ番号 */
135 short sem_op; /* セマフォ操作 */
136 short sem_flg; /* 操作フラグ */
140 .\"O Flags recognized in
146 .\"O If an operation specifies
148 .\"O it will be automatically undone when the process terminates.
156 が指定された操作は、そのプロセスが終了した時に自動的に取り消される。
158 .\"O The set of operations contained in
161 .\"O .IR "array order" ,
163 .\"O .IR atomically ,
164 .\"O that is, the operations are performed either as a complete unit,
172 すなわち、全ての操作が完全に実行されるか、全く実行されないかの
175 .\"O The behavior of the system call if not all operations can be
176 .\"O performed immediately depends on the presence of the
178 .\"O flag in the individual
180 .\"O fields, as noted below.
181 全ての操作が直ちに実行できない場合のこのシステムコールの振る舞いは
186 が存在するかによって決まり、後述のようになる。
188 .\"O Each operation is performed on the
189 .\"O .IR sem_num \-th
190 .\"O semaphore of the semaphore set, where the first semaphore of the set
194 のセマフォに対して実行される。セマフォ集合の最初のセマフォには
196 .\"O There are three types of operation, distinguished by the value of
204 .\"O is a positive integer, the operation adds this value to
205 .\"O the semaphore value
209 .\"O is specified for this operation, the system updates the process undo count
211 .\"O for this semaphore.
212 .\"O This operation can always proceed\(emit never forces a process to wait.
213 .\"O The calling process must have alter permission on the semaphore set.
215 が正の整数の場合、操作としてその値をセマフォの値
219 が指定されている場合は、システムはこのセマフォの プロセス・アンドゥ数
222 この操作は必ず実行でき、プロセスの停止は起こらない。
223 呼び出し元プロセスは対象のセマフォ集合を変更する許可がなければならない。
227 .\"O is zero, the process must have read permission on the semaphore
229 .\"O This is a "wait-for-zero" operation: if
231 .\"O is zero, the operation can immediately proceed.
233 が 0 の場合、「ゼロまで待つ」操作である。この場合、プロセスは
234 そのセマフォ集合に対する読み込み許可がなければならない。
246 .\"O (and none of the operations in
265 .\"O (the count of processes waiting until this semaphore's value becomes zero)
266 .\"O is incremented by one and the process sleeps until
267 .\"O one of the following occurs:
273 (セマフォ値が 0 になるのを待っているプロセスの数) を 1 増加させて、
274 以下のいずれかが起こるまでプロセスを停止 (sleep) する。
277 .\"O becomes 0, at which time the value of
285 .\"O The semaphore set
300 .\"O The calling process catches a signal:
303 .\"O is decremented and
309 呼び出し元プロセスがシグナルを捕獲した: このとき
319 .\"O The time limit specified by
322 .\"O .BR semtimedop ()
329 .\" motoki: semop () は semtimedop () の間違い?
333 で指定された制限時間が経過した: このとき
343 .\"O is less than zero, the process must have alter permission on the
347 .\"O is greater than or equal to the absolute value of
349 .\"O the operation can proceed immediately:
350 .\"O the absolute value of
352 .\"O is subtracted from
356 .\"O is specified for this operation, the system updates the process undo count
358 .\"O for this semaphore.
360 が 0 未満の場合、プロセスにはそのセマフォ集合を変更する許可がなければ
365 の絶対値以上の場合は、操作は直ちに実行される:
372 が指定されている場合は、このセマフォのプロセス・アンドゥ数
375 .\"O If the absolute value of
388 .\"O (and none of the operations in
409 .\"O (the counter of processes waiting for this semaphore's value to increase)
410 .\"O is incremented by one and the process sleeps until
411 .\"O one of the following occurs:
415 (このセマフォの値が増加するのを待っているプロセス数のカウンタ)
416 を 1 増加させて、以下のいずれかが起こるまでプロセスを停止 (sleep) する。
419 .\"O becomes greater than or equal to the absolute value of
421 .\"O at which time the value of
423 .\"O is decremented, the absolute value of
425 .\"O is subtracted from
429 .\"O is specified for this operation, the system updates the process undo count
431 .\"O for this semaphore.
444 が指定されていた場合にはこのセマフォのプロセス・アンドゥ数
448 .\"O The semaphore set is removed from the system:
454 セマフォ集合がシステムから削除された: このとき
462 .\"O The calling process catches a signal:
465 .\"O is decremented and
471 呼び出したプロセスがシグナルを捕獲した: このとき
481 .\"O The time limit specified by
484 .\"O .BR semtimedop ()
485 .\"O call expires: the system call fails, with
489 .\" motoki: the system call は semtimedop () のこと?
493 で指定された制限時間が経過した: このとき
501 .\"O On successful completion, the
503 .\"O value for each semaphore specified in the array pointed to by
505 .\"O is set to the process ID of the calling process.
508 が指す配列によって操作対象となった各セマフォの
510 メンバーには呼び出したプロセスのプロセス ID が設定される。
511 .\"O In addition, the
514 .\"O .\" .I sem_ctime
515 .\"O is set to the current time.
523 .\"O .BR semtimedop ()
524 .\"O behaves identically to
526 .\"O except that in those cases were the calling process would sleep,
527 .\"O the duration of that sleep is limited by the amount of elapsed
528 .\"O time specified by the
530 .\"O structure whose address is passed in the
537 呼び出し元プロセスが停止する場合、停止期間の上限が
541 構造体で指定された時間となる点だけが異なる。
542 .\"O If the specified time limit has been reached,
543 .\"O .BR semtimedop ()
548 .\"O (and none of the operations in
563 .\"O argument is NULL,
565 .\"O .BR semtimedop ()
566 .\"O behaves exactly like
574 .\"O .SH "RETURN VALUE"
579 .\"O .BR semtimedop ()
581 .\"O otherwise they return \-1
584 .\"O indicating the error.
589 は 0 を返す。そうでなければ \-1 を返し、
597 .\"O is set to one of the following:
607 .\"O the maximum number of operations allowed per system
614 は一回のシステムコールで許される操作の最大個数である。
617 .\"O The calling process does not have the permissions required
618 .\"O to perform the specified semaphore operations,
619 .\"O and does not have the
620 .\"O .B CAP_IPC_OWNER
622 呼び出し元プロセスには指定されたセマフォ操作を行うのに
628 .\"O An operation could not proceed immediately and either
630 .\"O was specified in
632 .\"O or the time limit specified in
644 .\"O An address specified in either the
648 .\"O arguments isn't accessible.
656 .\"O For some operation the value of
658 .\"O is less than 0 or greater than or equal to the number
659 .\"O of semaphores in the set.
662 の値が 0 未満か、集合内のセマフォの数以上である。
665 .\"O The semaphore set was removed.
669 .\"O While blocked in this system call, the process caught a signal; see
671 このシステムコールで停止している時にプロセスがシグナルを捕獲した。
676 .\"O The semaphore set doesn't exist, or
678 .\"O is less than zero, or
680 .\"O has a nonpositive value.
690 .\"O of some operation specified
692 .\"O and the system does not have enough memory to allocate the undo
698 が指定されたが、システムにアンドゥ構造体に割り当てる十分なメモリがない。
701 .\"O For some operation
702 .\"O .I sem_op+semval
705 .\"O the implementation dependent maximum value for
718 .\"O .BR semtimedop ()
719 .\"O first appeared in Linux 2.5.52,
720 .\"O and was subsequently backported into kernel 2.4.22.
722 は Linux 2.5.52 で初めて登場し、
723 それからカーネル 2.4.22 にも移植された。
724 .\"O Glibc support for
725 .\"O .BR semtimedop ()
726 .\"O first appeared in version 2.3.3.
728 の glibc でのサポートはバージョン 2.3.3 で初めて登場した。
729 .\"O .SH "CONFORMING TO"
732 .\"O .\" SVr4 documents additional error conditions EINVAL, EFBIG, ENOSPC.
733 .\" SVr4 には、追加で EINVAL, EFBIG, ENOSPC のエラー状態についての
739 .\"O structures of a process aren't inherited by the child produced by
741 .\"O but they are inherited across an
748 で生成された子プロセスには継承されないが、
753 .\"O is never automatically restarted after being interrupted by a signal handler,
754 .\"O regardless of the setting of the
756 .\"O flag when establishing a signal handler.
758 はシグナルハンドラによって中断された後に、
762 フラグがセットされていても再開することはない
765 .\"O is a per-process integer which is simply the (negative) count
766 .\"O of all semaphore operations performed specifying the
772 フラグを設定して実行された全てのセマフォ操作の(負数の)カウンタである。
773 .\"O When a semaphore's value is directly set using the
779 .\"O the corresponding
781 .\"O values in all processes are cleared.
788 直接設定された場合には、全てのプロセスにおいて対応する
792 .\"O The \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, and \fIsemnct\fP values
793 .\"O for a semaphore can all be retrieved using appropriate
796 あるセマフォの \fIsemval\fP, \fIsempid\fP, \fIsemzcnt\fP, \fIsemnct\fP
801 .\"O The following limits on semaphore set resources affect the
804 セマフォ集合のリソースに関する制限のうち、
809 .\"O Maximum number of operations allowed for one
812 .\"O (on Linux, this limit can be read and modified via the third field of
813 .\"O .IR /proc/sys/kernel/sem ).
814 .\"O .\" This /proc file is not available in Linux 2.2 and earlier -- MTK
819 .I /proc/sys/kernel/sem
820 の第3フィールドに対応し、読み出しも変更もできる)。
821 .\" この /proc ファイルは Linux 2.2 以前では利用できない -- MTK
824 .\"O Maximum allowable value for
826 .\"O implementation dependent (32767).
828 が取り得る最大値: 実装依存 (32767)。
830 .\"O The implementation has no intrinsic limits for
831 .\"O the adjust on exit maximum value
833 .\"O the system wide maximum number of undo structures
835 .\"O and the per-process maximum number of undo entries system parameters.
837 終了時の調整 (adjust on exit) の最大値
844 .\"O When a process terminates, its set of associated
846 .\"O structures is used to undo the effect of all of the
847 .\"O semaphore operations it performed with the
854 フラグ付きで実行された全てのセマフォ操作の影響を取り消す。
855 .\"O This raises a difficulty: if one (or more) of these semaphore adjustments
856 .\"O would result in an attempt to decrease a semaphore's value below zero,
857 .\"O what should an implementation do?
858 これによりある問題が発生する: これらのセマフォの調整を行っていると、
859 中にはセマフォの値が 0 未満の値にしようとする場合が出てくる。
860 このような場合、どのように実装するべきか?
861 .\"O One possible approach would be to block until all the semaphore
862 .\"O adjustments could be performed.
863 .\"O This is however undesirable since it could force process termination to
864 .\"O block for arbitrarily long periods.
865 ひとつの考えられる手法は、全てのセマフォ調整が実行されるまで
866 停止することである。しかし、この方法ではプロセスの終了が
867 長時間にわたって停止されることがあるので望ましくない。
868 しかもどれくらい長時間になるかは分からない。
869 .\"O Another possibility is that such semaphore adjustments could be ignored
870 .\"O altogether (somewhat analogously to failing when
872 .\"O is specified for a semaphore operation).
873 別の選択肢として、このようなセマフォ調整を完全に無視してしまう方法がある
877 .\"O Linux adopts a third approach: decreasing the semaphore value
878 .\"O as far as possible (i.e., to zero) and allowing process
879 .\"O termination to proceed immediately.
880 Linux は第三の手法を採用している: セマフォの値を出来るだけ (つまり
881 0 まで) 減少させて、プロセスの終了を直ちに続行できるようにしている。
883 .\"O In kernels 2.6.x, x <= 10, there is a bug that in some circumstances
884 .\"O prevents a process that is waiting for a semaphore value to become
885 .\"O zero from being woken up when the value does actually become zero.
886 .\"O This bug is fixed in kernel 2.6.11.
887 .\"O .\" The bug report:
888 .\"O .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
890 .\"O .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
891 カーネル 2.6.x (x <= 10) には、ある状況においてセマフォ値が 0 になるのを
892 待っているプロセスが、セマフォ値が実際に 0 になったときに起床 (wake up)
893 されない、というバグがある。このバグはカーネル 2.6.11 で修正されている。
895 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110260821123863&w=2
897 .\" http://marc.theaimsgroup.com/?l=linux-kernel&m=110261701025794&w=2
900 .\"O The following code segment uses
902 .\"O to atomically wait for the value of semaphore 0 to become zero,
903 .\"O and then increment the semaphore value by one.
905 セマフォ 0 の値が 0 になるのを待ってから、
908 を使ってアトミック (atomically) に行う。
911 struct sembuf sops[2];
914 /* Code to set \fIsemid\fP omitted */
916 sops[0].sem_num = 0; /* Operate on semaphore 0 */
917 sops[0].sem_op = 0; /* Wait for value to equal 0 */
920 sops[1].sem_num = 0; /* Operate on semaphore 0 */
921 sops[1].sem_op = 1; /* Increment value by one */
924 if (semop(semid, sops, 2) == \-1) {
934 .BR capabilities (7),
935 .BR sem_overview (7),