OSDN Git Service

Import translated manuals from JM CVS Repository.
[linuxjm/jm.git] / manual / LDP_man-pages / draft / man3 / sem_wait.3
1 '\" t
2 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\"
4 .\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
5 .\"
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.
9 .\"
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.
14 .\"
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
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
27 .\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\"
29 .TH SEM_WAIT 3 2010-02-25 "Linux" "Linux Programmer's Manual"
30 .\"O .SH NAME
31 .SH Ì¾Á°
32 .\"O sem_wait, sem_timedwait, sem_trywait \- lock a semaphore
33 sem_wait, sem_timedwait, sem_trywait \- ¥»¥Þ¥Õ¥©¤ò¥í¥Ã¥¯¤¹¤ë
34 .\"O .SH SYNOPSIS
35 .SH ½ñ¼°
36 .nf
37 .B #include <semaphore.h>
38 .sp
39 .BI "int sem_wait(sem_t *" sem );
40 .sp
41 .BI "int sem_trywait(sem_t *" sem );
42 .sp
43 .BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
44 .fi
45 .sp
46 .sp
47 .\"O Link with \fI\-lrt\fP or \fI\-pthread\fP.
48 \fI\-lrt\fP ¤Þ¤¿¤Ï \fI\-pthread\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
49 .in -4n
50 .\"O Feature Test Macro Requirements for glibc (see
51 .\"O .BR feature_test_macros (7)):
52 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
53 .RB ( feature_test_macros (7)
54 »²¾È):
55 .in
56 .sp
57 .BR sem_timedwait ():
58 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
59 .\"O .SH DESCRIPTION
60 .SH ÀâÌÀ
61 .\"O .BR sem_wait ()
62 .\"O decrements (locks) the semaphore pointed to by
63 .\"O .IR sem .
64 .\"O If the semaphore's value is greater than zero,
65 .\"O then the decrement proceeds, and the function returns, immediately.
66 .\"O If the semaphore currently has the value zero,
67 .\"O then the call blocks until either it becomes possible to perform
68 .\"O the decrement (i.e., the semaphore value rises above zero),
69 .\"O or a signal handler interrupts the call.
70 .BR sem_wait ()
71 ¤Ï
72 .I sem
73 ¤¬»Ø¤¹¥»¥Þ¥Õ¥©¤ÎÃͤò 1 ¸º¤é¤¹ (¥í¥Ã¥¯¤¹¤ë)¡£
74 ¥»¥Þ¥Õ¥©¤ÎÃͤ¬ 0 ¤è¤êÂ礭¤¤¾ì¹ç¡¢¸º»»¤¬¼Â¹Ô¤µ¤ì¡¢´Ø¿ô¤Ïľ¤Á¤ËÉüµ¢¤¹¤ë¡£
75 ¥»¥Þ¥Õ¥©¤Î¸½ºßÃͤ¬ 0 ¤Î¾ì¹ç¤Ë¤Ï¡¢¸º»»¤ò¼Â¹Ô¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ë
76 (¤Ä¤Þ¤ê¡¢¥»¥Þ¥Õ¥©¤ÎÃͤ¬ 0 ¤è¤êÂ礭¤ÊÃͤˤʤë) ¤Þ¤Ç¡¢¤â¤·¤¯¤Ï
77 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤Ã¤Æ¸Æ¤Ó½Ð¤·¤¬ÃæÃǤµ¤ì¤ë¤Þ¤Ç¡¢
78 ´Ø¿ô¸Æ¤Ó½Ð¤·¤ÏÄä»ß (block) ¤¹¤ë¡£
79
80 .\"O .BR sem_trywait ()
81 .\"O is the same as
82 .\"O .BR sem_wait (),
83 .\"O except that if the decrement cannot be immediately performed,
84 .\"O then call returns an error
85 .\"O .RI ( errno
86 .\"O set to
87 .\"O .BR EAGAIN )
88 .\"O instead of blocking.
89 .BR sem_trywait ()
90 ¤Ï
91 .BR sem_wait ()
92 ¤ÈƱ¤¸¤À¤¬¡¢¥»¥Þ¥Õ¥©Ãͤθº»»¤ò¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¡¢
93 Ää»ß (block) ¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¥¨¥é¡¼¤ÇÉüµ¢¤¹¤ë
94 .RI ( errno
95 ¤Ë
96 .B EAGAIN
97 ¤¬¥»¥Ã¥È¤µ¤ì¤ë) ÅÀ¤¬°Û¤Ê¤ë¡£
98
99 .\"O .BR sem_timedwait ()
100 .\"O is the same as
101 .\"O .BR sem_wait (),
102 .\"O except that
103 .\"O .I abs_timeout
104 .\"O specifies a limit on the amount of time that the call
105 .\"O should block if the decrement cannot be immediately performed.
106 .\"O The
107 .\"O .I abs_timeout
108 .\"O argument points to a structure that specifies an absolute timeout
109 .\"O in seconds and nanoseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
110 .\"O This structure is defined as follows:
111 .BR sem_timedwait ()
112 ¤Ï
113 .BR sem_wait ()
114 ¤ÈƱ¤¸¤À¤¬¡¢¥»¥Þ¥Õ¥©Ãͤθº»»¤ò¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë
115 ´Ø¿ô¸Æ¤Ó½Ð¤·¤¬Ää»ß¤¹¤ë»þ´Ö¤Î¾å¸Â¤ò
116 .I abs_timeout
117 ¤Ç»ØÄꤹ¤ëÅÀ¤¬°Û¤Ê¤ë¡£
118 .I abs_timeout
119 °ú¤­¿ô¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È»þ¹ï¤ò»ØÄꤹ¤ë¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
120 ¤³¤Î¹½Â¤ÂΤˤϡ¢¥¿¥¤¥à¥¢¥¦¥È»þ¹ï¤ò»þ¹ïµª¸µ (Epoch;
121 1970-01-01 00:00:00 +0000 (UTC)) ¤«¤é¤Î
122 ·Ð²á»þ´Ö (ÉáܥʥÎÉÃ) ¤Ç»ØÄꤹ¤ë¡£
123 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
124
125 .nf
126 .in +4n
127 struct timespec {
128     time_t tv_sec;      /* Seconds */
129     long   tv_nsec;     /* Nanoseconds [0 .. 999999999] */
130 };
131 .in
132 .fi
133 .PP
134 .\"O If the timeout has already expired by the time of the call,
135 .\"O and the semaphore could not be locked immediately,
136 .\"O then
137 .\"O .BR sem_timedwait ()
138 .\"O fails with a timeout error
139 .\"O .RI ( errno
140 .\"O set to
141 .\"O .BR ETIMEDOUT ).
142 ´Ø¿ô¸Æ¤Ó½Ð¤·»þÅÀ¤Ç¤¹¤Ç¤Ë¥¿¥¤¥à¥¢¥¦¥È¤Ë»ØÄꤷ¤¿»þ¹ï¤¬²á¤®¤Æ¤ª¤ê¡¢
143 ¤«¤Ä¥»¥Þ¥Õ¥©¤ò¤¹¤°¤Ë¥í¥Ã¥¯¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢
144 .BR sem_timedwait ()
145 ¤Ï¥¿¥¤¥à¥¢¥¦¥È¥¨¥é¡¼
146 .RI ( errno
147 ¤Ë
148 .B ETIMEDOUT
149 ¤¬¥»¥Ã¥È¤µ¤ì¤ë) ¤Ç¼ºÇÔ¤¹¤ë¡£
150
151 .\"O If the operation can be performed immediately, then
152 .\"O .BR sem_timedwait ()
153 .\"O never fails with a timeout error, regardless of the value of
154 .\"O .IR abs_timeout .
155 .\"O Furthermore, the validity of
156 .\"O .I abs_timeout
157 .\"O is not checked in this case.
158 ¥»¥Þ¥Õ¥©Áàºî¤¬¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤ë¤È¤­¤Ï¡¢
159 .I abs_timeout
160 ¤¬¤É¤ó¤ÊÃͤǤ¢¤Ã¤Æ¤â
161 .BR sem_timedwait ()
162 ¤¬¼ºÇÔ¤¹¤ë¤³¤È¤Ï·è¤·¤Æ¤Ê¤¤¡£¤µ¤é¤Ë¤¤¤¦¤È¡¢¤³¤Î¾ì¹ç¤Ë¤Ï
163 .I abs_timeout
164 ¤ÎÀµÅöÀ­¤Î¸¡ºº¤Ï¹Ô¤ï¤ì¤Ê¤¤¡£
165 .\"O .SH RETURN VALUE
166 .SH ÊÖ¤êÃÍ
167 .\"O All of these functions return 0 on success;
168 .\"O on error, the value of the semaphore is left unchanged,
169 .\"O \-1 is returned, and
170 .\"O .I errno
171 .\"O is set to indicate the error.
172 À®¸ù¤¹¤ë¤È¡¢¤³¤ì¤é¤Î´Ø¿ô¤Ï 0 ¤òÊÖ¤¹¡£
173 ¥¨¥é¡¼¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤ÎÃͤòÊѹ¹¤»¤º¤Ë¡¢\-1 ¤òÊÖ¤·¡¢
174 .I errno
175 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
176 .\"O .SH ERRORS
177 .SH ¥¨¥é¡¼
178 .TP
179 .B EINTR
180 .\"O The call was interrupted by a signal handler; see
181 .\"O .BR signal (7).
182 ¸Æ¤Ó½Ð¤·¤Ï¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤êÃæÃǤµ¤ì¤¿¡£
183 .BR signal (7)
184 »²¾È¡£
185 .TP
186 .B EINVAL
187 .\"O .I sem
188 .\"O is not a valid semaphore.
189 .I sem
190 ¤ÏÍ­¸ú¤Ê¥»¥Þ¥Õ¥©¤Ç¤Ê¤¤¡£
191 .PP
192 .\"O The following additional error can occur for
193 .\"O .BR sem_trywait ():
194 .BR sem_trywait ()
195 ¤Î¾ì¹ç¤Ë¤Ï¡¢¾åµ­¤Ë²Ã¤¨¤Æ°Ê²¼¤Î¥¨¥é¡¼¤âµ¯¤³¤ë¡£
196 .TP
197 .B EAGAIN
198 .\"O The operation could not be performed without blocking (i.e., the
199 .\"O semaphore currently has the value zero).
200 Ää»ß (block) ¤»¤º¤Ë¥í¥Ã¥¯Áàºî¤ò´°Î»¤Ç¤­¤Ê¤«¤Ã¤¿ (¤Ä¤Þ¤ê¡¢
201 ¥»¥Þ¥Õ¥©¤Î¸½ºß¤ÎÃͤ¬ 0 ¤Ç¤¢¤Ã¤¿)¡£
202 .PP
203 .\"O The following additional errors can occur for
204 .\"O .BR sem_timedwait ():
205 .BR sem_timedwait ()
206 ¤Î¾ì¹ç¡¢°Ê²¼¤Î¥¨¥é¡¼¤âµ¯¤³¤ë¡£
207 .TP
208 .B EINVAL
209 .\"O The value of
210 .\"O .I abs_timeout.tv_nsecs
211 .\"O is less than 0, or greater than or equal to 1000 million.
212 .I abs_timeout.tv_nsecs
213 ¤ÎÃͤ¬ 0 Ì¤Ëþ¡¢¤â¤·¤¯¤Ï 1,000,000,000 °Ê¾å¤Ç¤¢¤ë¡£
214 .TP
215 .B ETIMEDOUT
216 .\"O The call timed out before the semaphore could be locked.
217 ¥»¥Þ¥Õ¥©¤Î¥í¥Ã¥¯¤ËÀ®¸ù¤¹¤ëÁ°¤Ë»þ´ÖÀÚ¤ì¤È¤Ê¤Ã¤¿¡£
218 .\"O .\" POSIX.1-2001 also allows EDEADLK -- "A deadlock condition
219 .\"O .\" was detected", but this does not occur on Linux(?).
220 .\" POSIX.1-2001 ¤Ç¤Ï¥¨¥é¡¼¤È¤·¤Æ EDEADLK ¤âÍÑ°Õ¤·¤Æ¤¤¤ë¡£
221 .\" ¤³¤ì¤Ï¥Ç¥Ã¥É¥í¥Ã¥¯¾ò·ï¤ò¸¡½Ð¤·¤¿¤³¤È¤ò¼¨¤¹¤â¤Î¤À¤¬¡¢
222 .\" Linux ¤ÇȯÀ¸¤¹¤ë¤³¤È¤Ï¤Ê¤¤(?)
223 .\"O .SH CONFORMING TO
224 POSIX.1-2001.
225 .\"O .SH NOTES
226 .SH Ãí°Õ
227 .\"O A signal handler always interrupts a blocked call to
228 .\"O one of these functions, regardless of the use of the
229 .\"O .BR sigaction (2)
230 .\"O .B SA_RESTART
231 .\"O flag.
232 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¡¢
233 .BR sigaction (2)
234 ¤Î
235 .B SA_RESTART
236 ¥Õ¥é¥°¤ò»ÈÍѤ·¤Æ¤¤¤ë¤«¤É¤¦¤«¤Ë´Ø¤ï¤é¤º¡¢¤³¤ì¤é¤Î´Ø¿ô¤Î¸Æ¤Ó½Ð¤·¤¬
237 Ää»ß¤·¤Æ¤¤¤ë¾ì¹ç¡¢¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê¾ï¤ËÃæÃǤµ¤ì¤ë¡£
238 .\"O .\" sem_wait() is always interrupted on most other implementations,
239 .\"O .\" but on FreeBSD 5.4 SA_RESTART does cause restarting.
240 .\" sem_wait() ¤Ï¾¤Î¤Û¤È¤ó¤É¤Î¼ÂÁõ¤Ç¤Ï¾ï¤ËÃæÃǤµ¤ì¤ë¤¬¡¢
241 .\" FreeBSD 5.4 ¤Ç¤Ï SA_RESTART ¤ò»ØÄꤷ¤Æ¤ª¤¯¤È¼Â¹Ô¤¬ºÆ³«¤µ¤ì¤ë¡£
242 .\"O .SH EXAMPLE
243 .SH Îã
244 .PP
245 .\"O The (somewhat trivial) program shown below operates on an
246 .\"O unnamed semaphore.
247 .\"O The program expects two command-line arguments.
248 .\"O The first argument specifies a seconds value that is used to
249 .\"O set an alarm timer to generate a
250 .\"O .B SIGALRM
251 .\"O signal.
252 .\"O This handler performs a
253 .\"O .BR sem_post (3)
254 .\"O to increment the semaphore that is being waited on in
255 .\"O .I main()
256 .\"O using
257 .\"O .BR sem_timedwait ().
258 .\"O The second command-line argument specifies the length
259 .\"O of the timeout, in seconds, for
260 .\"O .BR sem_timedwait ().
261 .\"O The following shows what happens on two different runs of the program:
262 °Ê²¼¤Ë¼¨¤¹ (¤Á¤ç¤Ã¤È¤·¤¿) ¥×¥í¥°¥é¥à¤Ï̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ÎÁàºî¤ò¹Ô¤¦¡£
263 ¥×¥í¥°¥é¥à¤Ï¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤ò 2 ¤Ä¼è¤ë¡£
264 ºÇ½é¤Î°ú¤­¿ô¤Ë¤Ï¡¢
265 .B SIGALRM
266 ¥·¥°¥Ê¥ë¤òÀ¸À®¤¹¤ë¤¿¤á¤Î¥¢¥é¡¼¥à¥¿¥¤¥Þ¡¼¤ÎÀßÄê¤Ë»È¤ï¤ì¤ëÃͤò
267 ÉÃñ°Ì¤Ç»ØÄꤹ¤ë¡£¤³¤Î¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¡¢
268 .I main()
269 Æâ¤Ç
270 .BR sem_timedwait ()
271 ¤ò»È¤Ã¤ÆÂԤäƤ¤¤ë¥»¥Þ¥Õ¥©¤ò¡¢
272 .BR sem_post (3)
273 ¤ò»È¤Ã¤Æ²Ã»»¤¹¤ë¡£
274 2ÈÖÌܤΰú¤­¿ô¤Ë¤Ï¡¢
275 .BR sem_timedwait ()
276 ¤ËÅϤ¹¥¿¥¤¥à¥¢¥¦¥È¤Þ¤Ç¤Î»þ´Ö¤òÉÃñ°Ì¤Ç»ØÄꤹ¤ë¡£
277
278 .in +4n
279 .nf
280 .RB "$" " ./a.out 2 3"
281 About to call sem_timedwait()
282 sem_post() from handler
283 sem_getvalue() from handler; value = 1
284 sem_timedwait() succeeded
285 .RB "$" " ./a.out 2 1"
286 About to call sem_timedwait()
287 sem_timedwait() timed out
288 .fi
289 .in
290 .\"O .SS Program source
291 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
292 \&
293 .nf
294 #include <unistd.h>
295 #include <stdio.h>
296 #include <stdlib.h>
297 #include <semaphore.h>
298 #include <time.h>
299 #include <assert.h>
300 #include <errno.h>
301 #include <signal.h>
302
303 sem_t sem;
304
305 #define handle_error(msg) \\
306     do { perror(msg); exit(EXIT_FAILURE); } while (0)
307
308 static void
309 handler(int sig)
310 {
311     write(STDOUT_FILENO, "sem_post() from handler\\n", 24);
312     if (sem_post(&sem) == \-1) {
313         write(STDERR_FILENO, "sem_post() failed\\n", 18);
314         _exit(EXIT_FAILURE);
315     }
316 }
317
318 int
319 main(int argc, char *argv[])
320 {
321     struct sigaction sa;
322     struct timespec ts;
323     int s;
324
325     if (argc != 3) {
326         fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
327                 argv[0]);
328         exit(EXIT_FAILURE);
329     }
330
331     if (sem_init(&sem, 0, 0) == \-1)
332         handle_error("sem_init");
333
334     /* Establish SIGALRM handler; set alarm timer using argv[1] */
335
336     sa.sa_handler = handler;
337     sigemptyset(&sa.sa_mask);
338     sa.sa_flags = 0;
339     if (sigaction(SIGALRM, &sa, NULL) == \-1)
340         handle_error("sigaction");
341
342     alarm(atoi(argv[1]));
343
344     /* Calculate relative interval as current time plus
345        number of seconds given argv[2] */
346
347     if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
348         handle_error("clock_gettime");
349
350     ts.tv_sec += atoi(argv[2]);
351
352     printf("main() about to call sem_timedwait()\\n");
353     while ((s = sem_timedwait(&sem, &ts)) == \-1 && errno == EINTR)
354         continue;       /* Restart if interrupted by handler */
355
356     /* Check what happened */
357
358     if (s == \-1) {
359         if (errno == ETIMEDOUT)
360             printf("sem_timedwait() timed out\\n");
361         else
362             perror("sem_timedwait");
363     } else
364         printf("sem_timedwait() succeeded\\n");
365
366     exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
367 }
368 .fi
369 .\"O .SH "SEE ALSO"
370 .SH ´ØÏ¢¹àÌÜ
371 .BR clock_gettime (2),
372 .BR sem_getvalue (3),
373 .BR sem_post (3),
374 .BR sem_overview (7),
375 .BR time (7)