2 .\" Hey Emacs! This file is -*- nroff -*- source.
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
27 .\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
29 .TH SEM_WAIT 3 2010-02-25 "Linux" "Linux Programmer's Manual"
31 sem_wait, sem_timedwait, sem_trywait \- セマフォをロックする
34 .B #include <semaphore.h>
36 .BI "int sem_wait(sem_t *" sem );
38 .BI "int sem_trywait(sem_t *" sem );
40 .BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
44 \fI\-lrt\fP または \fI\-pthread\fP でリンクする。
47 .RB ( feature_test_macros (7)
52 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
57 が指すセマフォの値を 1 減らす (ロックする)。
58 セマフォの値が 0 より大きい場合、減算が実行され、関数は直ちに復帰する。
59 セマフォの現在値が 0 の場合には、減算を実行できるようになる
60 (つまり、セマフォの値が 0 より大きな値になる) まで、もしくは
61 シグナルハンドラによって呼び出しが中断されるまで、
67 と同じだが、セマフォ値の減算をすぐに実行できなかった場合に、
68 停止 (block) するのではなくエラーで復帰する
77 と同じだが、セマフォ値の減算をすぐに実行できなかった場合に
82 引き数は、タイムアウト時刻を指定する構造体へのポインタである。
83 この構造体には、タイムアウト時刻を時刻紀元 (Epoch;
84 1970-01-01 00:00:00 +0000 (UTC)) からの
91 time_t tv_sec; /* Seconds */
92 long tv_nsec; /* Nanoseconds [0 .. 999999999] */
97 関数呼び出し時点ですでにタイムアウトに指定した時刻が過ぎており、
98 かつセマフォをすぐにロックできなかった場合は、
110 が失敗することは決してない。さらにいうと、この場合には
115 エラーの場合、セマフォの値を変更せずに、\-1 を返し、
121 呼び出しはシグナルハンドラにより中断された。
130 の場合には、上記に加えて以下のエラーも起こる。
133 停止 (block) せずにロック操作を完了できなかった (つまり、
140 .I abs_timeout.tv_nsecs
141 の値が 0 未満、もしくは 1,000,000,000 以上である。
144 セマフォのロックに成功する前に時間切れとなった。
145 .\" POSIX.1-2001 ではエラーとして EDEADLK も用意している。
146 .\" これはデッドロック条件を検出したことを示すものだが、
147 .\" Linux で発生することはない(?)
154 フラグを使用しているかどうかに関わらず、これらの関数の呼び出しが
155 停止している場合、シグナルハンドラにより常に中断される。
156 .\" sem_wait() は他のほとんどの実装では常に中断されるが、
157 .\" FreeBSD 5.4 では SA_RESTART を指定しておくと実行が再開される。
160 以下に示す (ちょっとした) プログラムは名前なしセマフォの操作を行う。
161 プログラムはコマンドライン引き数を 2 つ取る。
164 シグナルを生成するためのアラームタイマーの設定に使われる値を
165 秒単位で指定する。このシグナルハンドラは、
174 に渡すタイムアウトまでの時間を秒単位で指定する。
178 .RB "$" " ./a.out 2 3"
179 About to call sem_timedwait()
180 sem_post() from handler
181 sem_getvalue() from handler; value = 1
182 sem_timedwait() succeeded
183 .RB "$" " ./a.out 2 1"
184 About to call sem_timedwait()
185 sem_timedwait() timed out
194 #include <semaphore.h>
202 #define handle_error(msg) \\
203 do { perror(msg); exit(EXIT_FAILURE); } while (0)
208 write(STDOUT_FILENO, "sem_post() from handler\\n", 24);
209 if (sem_post(&sem) == \-1) {
210 write(STDERR_FILENO, "sem_post() failed\\n", 18);
216 main(int argc, char *argv[])
223 fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
228 if (sem_init(&sem, 0, 0) == \-1)
229 handle_error("sem_init");
231 /* Establish SIGALRM handler; set alarm timer using argv[1] */
233 sa.sa_handler = handler;
234 sigemptyset(&sa.sa_mask);
236 if (sigaction(SIGALRM, &sa, NULL) == \-1)
237 handle_error("sigaction");
239 alarm(atoi(argv[1]));
241 /* Calculate relative interval as current time plus
242 number of seconds given argv[2] */
244 if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
245 handle_error("clock_gettime");
247 ts.tv_sec += atoi(argv[2]);
249 printf("main() about to call sem_timedwait()\\n");
250 while ((s = sem_timedwait(&sem, &ts)) == \-1 && errno == EINTR)
251 continue; /* Restart if interrupted by handler */
253 /* Check what happened */
256 if (errno == ETIMEDOUT)
257 printf("sem_timedwait() timed out\\n");
259 perror("sem_timedwait");
261 printf("sem_timedwait() succeeded\\n");
263 exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
267 .BR clock_gettime (2),
268 .BR sem_getvalue (3),
270 .BR sem_overview (7),