OSDN Git Service

(split) Fixed #25191 (typo in fenv.3).
[linuxjm/LDP_man-pages.git] / release / 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 .SH Ì¾Á°
31 sem_wait, sem_timedwait, sem_trywait \- ¥»¥Þ¥Õ¥©¤ò¥í¥Ã¥¯¤¹¤ë
32 .SH ½ñ¼°
33 .nf
34 .B #include <semaphore.h>
35 .sp
36 .BI "int sem_wait(sem_t *" sem );
37 .sp
38 .BI "int sem_trywait(sem_t *" sem );
39 .sp
40 .BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
41 .fi
42 .sp
43 .sp
44 \fI\-lrt\fP ¤Þ¤¿¤Ï \fI\-pthread\fP ¤Ç¥ê¥ó¥¯¤¹¤ë¡£
45 .in -4n
46 glibc ¸þ¤±¤Îµ¡Ç½¸¡ºº¥Þ¥¯¥í¤ÎÍ×·ï
47 .RB ( feature_test_macros (7)
48 »²¾È):
49 .in
50 .sp
51 .BR sem_timedwait ():
52 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
53 .SH ÀâÌÀ
54 .BR sem_wait ()
55 ¤Ï
56 .I sem
57 ¤¬»Ø¤¹¥»¥Þ¥Õ¥©¤ÎÃͤò 1 ¸º¤é¤¹ (¥í¥Ã¥¯¤¹¤ë)¡£
58 ¥»¥Þ¥Õ¥©¤ÎÃͤ¬ 0 ¤è¤êÂ礭¤¤¾ì¹ç¡¢¸º»»¤¬¼Â¹Ô¤µ¤ì¡¢´Ø¿ô¤Ïľ¤Á¤ËÉüµ¢¤¹¤ë¡£
59 ¥»¥Þ¥Õ¥©¤Î¸½ºßÃͤ¬ 0 ¤Î¾ì¹ç¤Ë¤Ï¡¢¸º»»¤ò¼Â¹Ô¤Ç¤­¤ë¤è¤¦¤Ë¤Ê¤ë
60 (¤Ä¤Þ¤ê¡¢¥»¥Þ¥Õ¥©¤ÎÃͤ¬ 0 ¤è¤êÂ礭¤ÊÃͤˤʤë) ¤Þ¤Ç¡¢¤â¤·¤¯¤Ï
61 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤Ã¤Æ¸Æ¤Ó½Ð¤·¤¬ÃæÃǤµ¤ì¤ë¤Þ¤Ç¡¢
62 ´Ø¿ô¸Æ¤Ó½Ð¤·¤ÏÄä»ß (block) ¤¹¤ë¡£
63
64 .BR sem_trywait ()
65 ¤Ï
66 .BR sem_wait ()
67 ¤ÈƱ¤¸¤À¤¬¡¢¥»¥Þ¥Õ¥©Ãͤθº»»¤ò¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë¡¢
68 Ää»ß (block) ¤¹¤ë¤Î¤Ç¤Ï¤Ê¤¯¥¨¥é¡¼¤ÇÉüµ¢¤¹¤ë
69 .RI ( errno
70 ¤Ë
71 .B EAGAIN
72 ¤¬¥»¥Ã¥È¤µ¤ì¤ë) ÅÀ¤¬°Û¤Ê¤ë¡£
73
74 .BR sem_timedwait ()
75 ¤Ï
76 .BR sem_wait ()
77 ¤ÈƱ¤¸¤À¤¬¡¢¥»¥Þ¥Õ¥©Ãͤθº»»¤ò¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ë
78 ´Ø¿ô¸Æ¤Ó½Ð¤·¤¬Ää»ß¤¹¤ë»þ´Ö¤Î¾å¸Â¤ò
79 .I abs_timeout
80 ¤Ç»ØÄꤹ¤ëÅÀ¤¬°Û¤Ê¤ë¡£
81 .I abs_timeout
82 °ú¤­¿ô¤Ï¡¢¥¿¥¤¥à¥¢¥¦¥È»þ¹ï¤ò»ØÄꤹ¤ë¹½Â¤ÂΤؤΥݥ¤¥ó¥¿¤Ç¤¢¤ë¡£
83 ¤³¤Î¹½Â¤ÂΤˤϡ¢¥¿¥¤¥à¥¢¥¦¥È»þ¹ï¤ò»þ¹ïµª¸µ (Epoch;
84 1970-01-01 00:00:00 +0000 (UTC)) ¤«¤é¤Î
85 ·Ð²á»þ´Ö (ÉáܥʥÎÉÃ) ¤Ç»ØÄꤹ¤ë¡£
86 ¹½Â¤ÂΤϰʲ¼¤Î¤è¤¦¤ËÄêµÁ¤µ¤ì¤Æ¤¤¤ë:
87
88 .nf
89 .in +4n
90 struct timespec {
91     time_t tv_sec;      /* Seconds */
92     long   tv_nsec;     /* Nanoseconds [0 .. 999999999] */
93 };
94 .in
95 .fi
96 .PP
97 ´Ø¿ô¸Æ¤Ó½Ð¤·»þÅÀ¤Ç¤¹¤Ç¤Ë¥¿¥¤¥à¥¢¥¦¥È¤Ë»ØÄꤷ¤¿»þ¹ï¤¬²á¤®¤Æ¤ª¤ê¡¢
98 ¤«¤Ä¥»¥Þ¥Õ¥©¤ò¤¹¤°¤Ë¥í¥Ã¥¯¤Ç¤­¤Ê¤«¤Ã¤¿¾ì¹ç¤Ï¡¢
99 .BR sem_timedwait ()
100 ¤Ï¥¿¥¤¥à¥¢¥¦¥È¥¨¥é¡¼
101 .RI ( errno
102 ¤Ë
103 .B ETIMEDOUT
104 ¤¬¥»¥Ã¥È¤µ¤ì¤ë) ¤Ç¼ºÇÔ¤¹¤ë¡£
105
106 ¥»¥Þ¥Õ¥©Áàºî¤¬¤¹¤°¤Ë¼Â¹Ô¤Ç¤­¤ë¤È¤­¤Ï¡¢
107 .I abs_timeout
108 ¤¬¤É¤ó¤ÊÃͤǤ¢¤Ã¤Æ¤â
109 .BR sem_timedwait ()
110 ¤¬¼ºÇÔ¤¹¤ë¤³¤È¤Ï·è¤·¤Æ¤Ê¤¤¡£¤µ¤é¤Ë¤¤¤¦¤È¡¢¤³¤Î¾ì¹ç¤Ë¤Ï
111 .I abs_timeout
112 ¤ÎÀµÅöÀ­¤Î¸¡ºº¤Ï¹Ô¤ï¤ì¤Ê¤¤¡£
113 .SH ÊÖ¤êÃÍ
114 À®¸ù¤¹¤ë¤È¡¢¤³¤ì¤é¤Î´Ø¿ô¤Ï 0 ¤òÊÖ¤¹¡£
115 ¥¨¥é¡¼¤Î¾ì¹ç¡¢¥»¥Þ¥Õ¥©¤ÎÃͤòÊѹ¹¤»¤º¤Ë¡¢\-1 ¤òÊÖ¤·¡¢
116 .I errno
117 ¤Ë¥¨¥é¡¼¤ò¼¨¤¹Ãͤò¥»¥Ã¥È¤¹¤ë¡£
118 .SH ¥¨¥é¡¼
119 .TP
120 .B EINTR
121 ¸Æ¤Ó½Ð¤·¤Ï¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤êÃæÃǤµ¤ì¤¿¡£
122 .BR signal (7)
123 »²¾È¡£
124 .TP
125 .B EINVAL
126 .I sem
127 ¤ÏÍ­¸ú¤Ê¥»¥Þ¥Õ¥©¤Ç¤Ê¤¤¡£
128 .PP
129 .BR sem_trywait ()
130 ¤Î¾ì¹ç¤Ë¤Ï¡¢¾åµ­¤Ë²Ã¤¨¤Æ°Ê²¼¤Î¥¨¥é¡¼¤âµ¯¤³¤ë¡£
131 .TP
132 .B EAGAIN
133 Ää»ß (block) ¤»¤º¤Ë¥í¥Ã¥¯Áàºî¤ò´°Î»¤Ç¤­¤Ê¤«¤Ã¤¿ (¤Ä¤Þ¤ê¡¢
134 ¥»¥Þ¥Õ¥©¤Î¸½ºß¤ÎÃͤ¬ 0 ¤Ç¤¢¤Ã¤¿)¡£
135 .PP
136 .BR sem_timedwait ()
137 ¤Î¾ì¹ç¡¢°Ê²¼¤Î¥¨¥é¡¼¤âµ¯¤³¤ë¡£
138 .TP
139 .B EINVAL
140 .I abs_timeout.tv_nsecs
141 ¤ÎÃͤ¬ 0 Ì¤Ëþ¡¢¤â¤·¤¯¤Ï 1,000,000,000 °Ê¾å¤Ç¤¢¤ë¡£
142 .TP
143 .B ETIMEDOUT
144 ¥»¥Þ¥Õ¥©¤Î¥í¥Ã¥¯¤ËÀ®¸ù¤¹¤ëÁ°¤Ë»þ´ÖÀÚ¤ì¤È¤Ê¤Ã¤¿¡£
145 .\" POSIX.1-2001 ¤Ç¤Ï¥¨¥é¡¼¤È¤·¤Æ EDEADLK ¤âÍÑ°Õ¤·¤Æ¤¤¤ë¡£
146 .\" ¤³¤ì¤Ï¥Ç¥Ã¥É¥í¥Ã¥¯¾ò·ï¤ò¸¡½Ð¤·¤¿¤³¤È¤ò¼¨¤¹¤â¤Î¤À¤¬¡¢
147 .\" Linux ¤ÇȯÀ¸¤¹¤ë¤³¤È¤Ï¤Ê¤¤(?)
148 POSIX.1-2001.
149 .SH Ãí°Õ
150 ¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¡¢
151 .BR sigaction (2)
152 ¤Î
153 .B SA_RESTART
154 ¥Õ¥é¥°¤ò»ÈÍѤ·¤Æ¤¤¤ë¤«¤É¤¦¤«¤Ë´Ø¤ï¤é¤º¡¢¤³¤ì¤é¤Î´Ø¿ô¤Î¸Æ¤Ó½Ð¤·¤¬
155 Ää»ß¤·¤Æ¤¤¤ë¾ì¹ç¡¢¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ë¤è¤ê¾ï¤ËÃæÃǤµ¤ì¤ë¡£
156 .\" sem_wait() ¤Ï¾¤Î¤Û¤È¤ó¤É¤Î¼ÂÁõ¤Ç¤Ï¾ï¤ËÃæÃǤµ¤ì¤ë¤¬¡¢
157 .\" FreeBSD 5.4 ¤Ç¤Ï SA_RESTART ¤ò»ØÄꤷ¤Æ¤ª¤¯¤È¼Â¹Ô¤¬ºÆ³«¤µ¤ì¤ë¡£
158 .SH Îã
159 .PP
160 °Ê²¼¤Ë¼¨¤¹ (¤Á¤ç¤Ã¤È¤·¤¿) ¥×¥í¥°¥é¥à¤Ï̾Á°¤Ê¤·¥»¥Þ¥Õ¥©¤ÎÁàºî¤ò¹Ô¤¦¡£
161 ¥×¥í¥°¥é¥à¤Ï¥³¥Þ¥ó¥É¥é¥¤¥ó°ú¤­¿ô¤ò 2 ¤Ä¼è¤ë¡£
162 ºÇ½é¤Î°ú¤­¿ô¤Ë¤Ï¡¢
163 .B SIGALRM
164 ¥·¥°¥Ê¥ë¤òÀ¸À®¤¹¤ë¤¿¤á¤Î¥¢¥é¡¼¥à¥¿¥¤¥Þ¡¼¤ÎÀßÄê¤Ë»È¤ï¤ì¤ëÃͤò
165 ÉÃñ°Ì¤Ç»ØÄꤹ¤ë¡£¤³¤Î¥·¥°¥Ê¥ë¥Ï¥ó¥É¥é¤Ï¡¢
166 .I main()
167 Æâ¤Ç
168 .BR sem_timedwait ()
169 ¤ò»È¤Ã¤ÆÂԤäƤ¤¤ë¥»¥Þ¥Õ¥©¤ò¡¢
170 .BR sem_post (3)
171 ¤ò»È¤Ã¤Æ²Ã»»¤¹¤ë¡£
172 2ÈÖÌܤΰú¤­¿ô¤Ë¤Ï¡¢
173 .BR sem_timedwait ()
174 ¤ËÅϤ¹¥¿¥¤¥à¥¢¥¦¥È¤Þ¤Ç¤Î»þ´Ö¤òÉÃñ°Ì¤Ç»ØÄꤹ¤ë¡£
175
176 .in +4n
177 .nf
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
186 .fi
187 .in
188 .SS ¥×¥í¥°¥é¥à¤Î¥½¡¼¥¹
189 \&
190 .nf
191 #include <unistd.h>
192 #include <stdio.h>
193 #include <stdlib.h>
194 #include <semaphore.h>
195 #include <time.h>
196 #include <assert.h>
197 #include <errno.h>
198 #include <signal.h>
199
200 sem_t sem;
201
202 #define handle_error(msg) \\
203     do { perror(msg); exit(EXIT_FAILURE); } while (0)
204
205 static void
206 handler(int sig)
207 {
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);
211         _exit(EXIT_FAILURE);
212     }
213 }
214
215 int
216 main(int argc, char *argv[])
217 {
218     struct sigaction sa;
219     struct timespec ts;
220     int s;
221
222     if (argc != 3) {
223         fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
224                 argv[0]);
225         exit(EXIT_FAILURE);
226     }
227
228     if (sem_init(&sem, 0, 0) == \-1)
229         handle_error("sem_init");
230
231     /* Establish SIGALRM handler; set alarm timer using argv[1] */
232
233     sa.sa_handler = handler;
234     sigemptyset(&sa.sa_mask);
235     sa.sa_flags = 0;
236     if (sigaction(SIGALRM, &sa, NULL) == \-1)
237         handle_error("sigaction");
238
239     alarm(atoi(argv[1]));
240
241     /* Calculate relative interval as current time plus
242        number of seconds given argv[2] */
243
244     if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
245         handle_error("clock_gettime");
246
247     ts.tv_sec += atoi(argv[2]);
248
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 */
252
253     /* Check what happened */
254
255     if (s == \-1) {
256         if (errno == ETIMEDOUT)
257             printf("sem_timedwait() timed out\\n");
258         else
259             perror("sem_timedwait");
260     } else
261         printf("sem_timedwait() succeeded\\n");
262
263     exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
264 }
265 .fi
266 .SH ´ØÏ¢¹àÌÜ
267 .BR clock_gettime (2),
268 .BR sem_getvalue (3),
269 .BR sem_post (3),
270 .BR sem_overview (7),
271 .BR time (7)