File: sem_wait.3

package info (click to toggle)
manpages-ja 0.5.0.0.20100315-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 21,156 kB
  • ctags: 1
  • sloc: sh: 13,935; perl: 157; makefile: 114
file content (270 lines) | stat: -rw-r--r-- 7,132 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
'\" t
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" Copyright (C) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein.  The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Japanese Version Copyright (c) 2006 Akihiro MOTOKI all rights reserved.
.\" Translated 2006-04-18, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\"
.TH SEM_WAIT 3 2009-02-09 "Linux" "Linux Programmer's Manual"
.SH ̾
sem_wait, sem_timedwait, sem_trywait \- ޥեå
.SH 
.nf
.B #include <semaphore.h>
.sp
.BI "int sem_wait(sem_t *" sem );
.sp
.BI "int sem_trywait(sem_t *" sem );
.sp
.BI "int sem_timedwait(sem_t *" sem ", const struct timespec *" abs_timeout );
.fi
.sp
.sp
\fI\-lrt\fP ޤ \fI\-pthread\fP ǥ󥯤롣
.in -4n
glibc εǽޥ׷
.RB ( feature_test_macros (7)
):
.in
.sp
.BR sem_timedwait ():
_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
.SH 
.BR sem_wait ()

.I sem
ؤޥեͤ 1 餹 (å)
ޥեͤ 0 礭硢¹Ԥ졢ؿľ롣
ޥեθͤ 0 ξˤϡ¹ԤǤ褦ˤʤ
(Ĥޤꡢޥեͤ 0 礭ͤˤʤ) ޤǡ⤷
ʥϥɥˤäƸƤӽФǤޤǡ
ؿƤӽФ (block) 롣

.BR sem_trywait ()

.BR sem_wait ()
Ʊޥեͤθ򤹤˼¹ԤǤʤäˡ
 (block) ΤǤϤʤ顼
.RI ( errno

.B EAGAIN
åȤ) ۤʤ롣

.BR sem_timedwait ()

.BR sem_wait ()
Ʊޥեͤθ򤹤˼¹ԤǤʤä
ؿƤӽФߤ֤ξ¤
.I abs_timeout
ǻꤹۤʤ롣
.I abs_timeout
ϡॢȻꤹ빽¤ΤؤΥݥ󥿤Ǥ롣
ι¤ΤˤϡॢȻﵪ (Epoch; 1970 ǯ 1  1 
00:00:00) ηв (áܥʥ) ǻꤹ롣
¤ΤϰʲΤ褦Ƥ:

.nf
.in +4n
struct timespec {
    time_t tv_sec;      /* Seconds */
    long   tv_nsec;     /* Nanoseconds [0 .. 999999999] */
};
.in
.fi
.PP
ؿƤӽФǤǤ˥ॢȤ˻ꤷ郎᤮Ƥꡢ
ĥޥե򤹤˥åǤʤäϡ
.BR sem_timedwait ()
ϥॢȥ顼
.RI ( errno

.B ETIMEDOUT
åȤ) ǼԤ롣

ޥե˼¹ԤǤȤϡ
.I abs_timeout
ɤͤǤäƤ
.BR sem_timedwait ()
Ԥ뤳ȤϷ褷Ƥʤˤȡξˤ
.I abs_timeout
θϹԤʤ
.SH ֤
ȡδؿ 0 ֤
顼ξ硢ޥեͤѹˡ\-1 ֤
.I errno
˥顼򼨤ͤ򥻥åȤ롣
.SH 顼
.TP
.B EINTR
ƤӽФϥʥϥɥˤǤ줿
.BR signal (7)
ȡ
.TP
.B EINVAL
.I sem
ͭʥޥեǤʤ
.PP
.BR sem_trywait ()
ξˤϡ嵭˲äưʲΥ顼ⵯ롣
.TP
.B EAGAIN
 (block) ˥åλǤʤä (Ĥޤꡢ
ޥեθߤͤ 0 Ǥä)
.PP
.BR sem_timedwait ()
ξ硢ʲΥ顼ⵯ롣
.TP
.B EINVAL
.I abs_timeout.tv_nsecs
ͤ 0 ̤⤷ 1,000,000,000 ʾǤ롣
.TP
.B ETIMEDOUT
ޥեΥå˻ڤȤʤä
.\" POSIX.1-2001 Ǥϥ顼Ȥ EDEADLK ѰդƤ롣
.\" ϥǥåɥå򸡽ФȤ򼨤Τ
.\" Linux ȯ뤳ȤϤʤ(?)
POSIX.1-2001.
.SH 
ʥϥɥϡ
.BR sigaction (2)

.B SA_RESTART
ե饰ѤƤ뤫ɤ˴ؤ餺δؿθƤӽФ
ߤƤ硢ʥϥɥˤǤ롣
.\" sem_wait() ¾ΤۤȤɤμǤϾǤ뤬
.\" FreeBSD 5.4 Ǥ SA_RESTART ꤷƤȼ¹ԤƳ롣
.SH 
.PP
ʲ˼ (äȤ) ץ̾ʤޥեԤ
ץϥޥɥ饤 2 ļ롣
ǽΰˤϡ
.B SIGALRM
ʥ뤿Υ顼ॿޡ˻Ȥͤ
ñ̤ǻꤹ롣Υʥϥɥϡ
.I main()

.BR sem_timedwait ()
ȤäԤäƤ륻ޥե
.BR sem_post (3)
ȤäƲû롣
2ܤΰˤϡ
.BR sem_timedwait ()
ϤॢȤޤǤλ֤ñ̤ǻꤹ롣

.in +4n
.nf
.RB "$" " ./a.out 2 3"
About to call sem_timedwait()
sem_post() from handler
sem_getvalue() from handler; value = 1
sem_timedwait() succeeded
.RB "$" " ./a.out 2 1"
About to call sem_timedwait()
sem_timedwait() timed out
.fi
.in
.SS ץΥ
\&
.nf
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <signal.h>

sem_t sem;

#define handle_error(msg) \\
    do { perror(msg); exit(EXIT_FAILURE); } while (0)

static void
handler(int sig)
{
    write(STDOUT_FILENO, "sem_post() from handler\\n", 24);
    if (sem_post(&sem) == \-1) {
        write(STDERR_FILENO, "sem_post() failed\\n", 18);
        _exit(EXIT_FAILURE);
    }
}

int
main(int argc, char *argv[])
{
    struct sigaction sa;
    struct timespec ts;
    int s;

    if (argc != 3) {
        fprintf(stderr, "Usage: %s <alarm\-secs> <wait\-secs>\\n",
                argv[0]);
        exit(EXIT_FAILURE);
    }

    if (sem_init(&sem, 0, 0) == \-1)
        handle_error("sem_init");

    /* Establish SIGALRM handler; set alarm timer using argv[1] */

    sa.sa_handler = handler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = 0;
    if (sigaction(SIGALRM, &sa, NULL) == \-1)
        handle_error("sigaction");

    alarm(atoi(argv[1]));

    /* Calculate relative interval as current time plus
       number of seconds given argv[2] */

    if (clock_gettime(CLOCK_REALTIME, &ts) == \-1)
        handle_error("clock_gettime");

    ts.tv_sec += atoi(argv[2]);

    printf("main() about to call sem_timedwait()\\n");
    while ((s = sem_timedwait(&sem, &ts)) == \-1 && errno == EINTR)
        continue;       /* Restart if interrupted by handler */

    /* Check what happened */

    if (s == \-1) {
        if (errno == ETIMEDOUT)
            printf("sem_timedwait() timed out\\n");
        else
            perror("sem_timedwait");
    } else
        printf("sem_timedwait() succeeded\\n");

    exit((s == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
}
.fi
.SH Ϣ
.BR clock_gettime (2),
.BR sem_getvalue (3),
.BR sem_post (3),
.BR sem_overview (7),
.BR time (7)