File: select.2

package info (click to toggle)
manpages-ja 0.5.0.0.20080615-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 20,508 kB
  • ctags: 1
  • sloc: sh: 13,690; perl: 157; makefile: 114
file content (559 lines) | stat: -rw-r--r-- 14,759 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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is copyright (C) 1992 Drew Eckhardt,
.\"                 copyright (C) 1995 Michael Shields.
.\"
.\" 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.
.\"
.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\" Sun Feb 11 14:07:00 MET 1996  Martin Schulze  <joey@linux.de>
.\"	* layout slightly modified
.\"
.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb
.\" Modified Thu Feb  9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb
.\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net>
.\" 2005-03-11, mtk, modified pselect() text (it is now a system
.\"     call in 2.6.16.
.\"
.\" Japanese Version Copyright (c) 1997 HANATAKA Shinya all rights reserved.
.\" Translated 1997-02-23, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Modified 2000-03-12, HANATAKA Shinya <hanataka@abyss.rim.or.jp>
.\" Updated & Modified 2001-06-04, Yuichi SATO <ysato@h4.dion.ne.jp>
.\" Updated & Modified 2001-07-01, Yuichi SATO
.\" Updated & Modifedd 2002-01-14, Yuichi SATO
.\" Updated 2002-09-24, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
.\" Updated 2003-01-19, Akihiro MOTOKI
.\" Updated 2005-03-17, Akihiro MOTOKI
.\" Updated 2006-04-16, Akihiro MOTOKI, LDP v2.28
.\" Updated 2006-07-23, Akihiro MOTOKI, LDP v2.36
.\" Updated 2007-09-08, Akihiro MOTOKI, LDP v2.64
.\"
.\"WORD:	synchronous		Ʊ
.\"WORD:	multiplexing		¿Ų
.\"WORD:	file descriptor		եǥץ
.\"WORD:	descriptor		һ
.\"WORD:	macro			ޥ
.\"WORD:	block			(block)
.\"WORD:	block			ػ(block)
.\"WORD:	signal			ʥ
.\"WORD:	timeout			ڤ(timeout)
.\"WORD:	portable		ܿΤ
.\"
.TH SELECT 2 2007-07-26 "Linux" "Linux Programmer's Manual"
.SH ̾
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- ¿Ų줿 I/O ƱȤ
.SH 
.nf
/* POSIX.1-2001 ˽ */
.br
.B #include <sys/select.h>
.sp
/* εʤ˽ */
.br
.B #include <sys/time.h>
.br
.B #include <sys/types.h>
.br
.B #include <unistd.h>
.sp
.BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
.BI "           fd_set *" exceptfds ", struct timeval *" timeout );
.sp
.BI "void FD_CLR(int " fd ", fd_set *" set );
.br
.BI "int  FD_ISSET(int " fd ", fd_set *" set );
.br
.BI "void FD_SET(int " fd ", fd_set *" set );
.br
.BI "void FD_ZERO(fd_set *" set );
.sp
.B #include <sys/select.h>
.sp
.BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
.BI "            fd_set *" exceptfds ", const struct timespec *" timeout ,
.BI "            const sigset_t *" sigmask );
.fi
.sp
.in -4n
glibc εǽޥ׷
.RB ( feature_test_macros (7)
):
.in
.sp
.BR pselect ():
_POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
.SH 
.BR select ()

.BR pselect ()
ȤȡץʣΥեǥץƻ뤷
İʾΥեǥץ I/O 
ready (Ǥ)׾ (㤨Сɤ߹߲ǽˤʤä)
ˤʤޤԤĤȤǤ롣
եǥץ ready (Ǥ) Ȥϡ
б I/O  (㤨
.BR read (2)
ʤ)  (block) ʤ˼¹Բǽʾ֤ˤ뤳Ȥ̣롣
.PP
.BR select ()

.BR pselect ()
ưƱǤ뤬ʲ 3 ۤʤ:
.TP
(i)
.BR select ()
ǤϡॢȻ֤λ˹¤
.I struct timeval
(áޥñ) Ѥ롣

.BR pselect ()
ؿǤϡ¤
.I struct timespec
(áʥñ) Ѥ롣
.TP
(ii)
.BR select ()
ϻĤ֤򼨤
.I timeout
򹹿뤳Ȥ롣
.BR pselect ()
Ϥΰѹʤ
.TP
(iii)
.BR select ()

.I sigmask
ʤư
.I sigmask
 NULL ꤷ
.BR pselect ()
ƱǤ롣
.PP
3 ĤΩեǥץδƻԤ
.I readfds
줿ǥץˤĤƤϡɤ߹ߤǽɤ
ƻ뤹 (Τˤȡ (block) ʤɤळȤǤ뤫
Ĵ٤롣եνü (end-of-file) ξ⡢
եǥץɤ߹߲ǽȤư)
.I writefds
줿ǥץˤĤƤϡߤ˽񤭹ߤ
ǽɤƻ뤹롣
.I exceptfds
ˤΤˤĤƤϡ㳰δƻԤʤƥॳ뽪λˡ
ɤΥեǥץξ֤ºݤѲˡ
Ƥѹ롣
̤Υ٥Ȥƻ뤷եǥץĤʤˤϡ
бեǥץ NULL ꤹ뤳ȤǤ롣
.PP
뤿 4 ĤΥޥ󶡤Ƥ롣
.BR FD_ZERO ()
Ͻõ롣
.BR FD_SET ()

.BR FD_CLR ()
Ϥ줾ꤷեǥץνؤɲáԤ
.BR FD_ISSET ()
Ͻ˥եǥץ뤫ɤĴ٤; 
Υޥ
.BR select ()
λ˻ȤǤ롣
.PP
.I nfds
 3 Ĥν˴ޤޤեǥץκͤ 1 ­ΤǤ롣
.PP
.I timeout

.BR select ()
ޤǤηв֤ξ¤Ǥ롣 0 ˤ뤳ȤǤ
ξ
.BR select ()
Ϥ
(εǽϥݡ (polling) ԤΤǤ)
.I timeout
 NULL (ॢȤʤ)
ꤵȡ
.BR select ()
̵¤ (block) 롣
.PP
.I sigmask
ϡʥޥ
.RB ( sigprocmask (2)
򻲾) ؤΥݥ󥿤Ǥ롣
.I sigmask
 NULL Ǥʤ硢
.BR pselect ()

.I sigmask
ؤƤ륷ʥޥǸߤΥʥޥ֤Ƥ顢
"select" ؿ¹Ԥ
λ˥ʥޥ򸵤Υʥޥ᤹
.PP
.I timeout
٤ΰ㤤ȡʲ
.BR pselect ()
θƤӽФϡ
.nf

    ready = pselect(nfds, &readfds, &writefds, &exceptfds,
                    timeout, &sigmask);

.fi
Υ
.I atomic
˼¹ԤΤǤ롣
.nf

    sigset_t origmask;

    sigprocmask(SIG_SETMASK, &sigmask, &origmask);
    ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
    sigprocmask(SIG_SETMASK, &origmask, NULL);
.fi
.PP
.BR pselect ()
ɬפˤʤͳϡʥեǥץξѲ
ԤȤˤϡ֤򤱤뤿 atomic ʥƥȤɬפˤʤ
Ǥ롣
(ʥϥɥ餬ե饰ꤷͤƤߤ褦
ե饰ΥƥȤ³
.BR select ()
ƤӽФȡ
ʥ뤬ƥȤľ夫ĸƤӽФľϤˤ
.BR select ()
ϱʵפ˥ϥ󥰤Ƥޤ⤷ʤ

.BR pselect ()
Ȥȡޤʥػ (block) ơäƤ륷ʥ
˾ߤ
.I sigmask

.BR pselect ()
ƤӽФȤǡζ򤱤뤳ȤǤ롣)
.SS ॢ
δؿǻѤִϢι¤Τϡ
.I <sys/time.h>
ǰʲΤ褦Ƥ롣

.in +4n
.nf
struct timeval {
    long    tv_sec;         /*  */
    long    tv_usec;        /* ޥ */
};
.fi
.in

.in +4n
.nf
struct timespec {
    long    tv_sec;         /*  */
    long    tv_nsec;        /* ʥ */
};
.fi
.in

(POSIX.1-2001 ǤˤĤƤϲΡաפ򻲾)
.PP
ñ̰ʲ٤ǥ꡼פ¸
ܿι⤤ˡȤơ
3 ĤνƤ
.I nfds
 0 
.I timeout
 NULL Ǥʤͤꤷ
.BR select ()
ƤӽФȤˡȤäƤ륳ɤ⤢롣
.PP
Linux Ǥϡ
.BR select ()

.I timeout
ѹĤ߻֤ȿǤ褦ˤʤäƤ뤬
¾ΤۤȤɤμǤϤΤ褦ˤʤäƤʤ
(POSIX.1-2001 ϤɤưǧƤ)
Τᡢ
.I timeout
򻲾ȤƤ Linux Υɤ¾Υڥ졼ƥ󥰡ƥ
ܿ硢꤬롣
ޤ롼פ \fItimeval\fP ¤ΤˤΤޤ޺Ѥ
.BR select ()
ʣԤʤäƤ륳ɤ Linux ذܿˤ⡢꤬롣
.BR select ()

.I timeout
̤Ǥȹͤ٤Ǥ롣
.\" .PP
.\" BSD ǤϥॢȤäƤ⡢
.\" եǥץӥåȤѤʤפʹȤ롣
.\" Linux  SUSv2 ˽äƤꡢ
.\" ॢȤκݤˤϥӥåȥޥ 0 ˤפȤΤϳΤ˻¤Ǥ롣
.SH ֤
.BR select ()

.BR pselect ()
Ϲ줿 3 ĤΥǥץ˴ޤޤƤ
եǥץο (Ĥޤꡢ
.IR readfds ,
.IR writefds ,
.I exceptfds
 1 ˤʤäƤӥåȤ) ֤
ⵯ餺˻ڤˤʤä硢
ǥץο 0 ˤʤ뤳Ȥ⤢롣
顼ʤ \-1 ֤
.I errno
Ŭڤͤꤵ; 
.I timeout
̤ȤʤΤǡ顼äϤƤꤷƤϤʤʤ
.SH 顼
.TP
.B EBADF
줫ν̵ʥեǥץꤵ줿
(餯ϡǤ˥줿եǥץ
顼ȯեǥץꤵ줿)
.TP
.B EINTR
ʥ
.TP
.B EINVAL
.I n
顢ޤ
.I timeout
äƤͤǤ롣
.TP
.B ENOMEM
ơ֥˥Ƥ뤳ȤǤʤä
.SH С
.BR pselect ()
ϥͥ 2.6.16  Linux ɲä줿
ϡ
.BR pselect ()
 glibc ǥߥ졼ȤƤ (֥ХפξϤ򻲾)
.SH 
.BR select ()
 POSIX.1-2001  4.4BSD
.RB ( select ()
 4.2BSD Ǻǽо줷) ˽򤹤롣
BSD åؤΥ򥵥ݡȤƤ BSD ƥ
(System V Ϥޤ) Ȥδ֤Ǥܿ롣 System V ϤǤ
 timeout ѿ exit ˥åȤ뤬
BSD ϤǤϤǤʤΤդ뤳ȡ
.PP
.BR pselect ()
 POSIX.1g  POSIX.1-2001 Ƥ롣
.SH 
.I fd_set
ϸꥵΥХåեǤ롣

.B FD_SETSIZE
ʾͤ
.I fd
Ф
.BR FD_CLR ()

.BR FD_SET ()
¹Ԥ硢
ɤΤ褦ư򤹤뤫Ƥʤ
ޤ POSIX Ǥ
.I fd
ͭʥեǥץǤʤФʤʤȵꤵƤ롣

˴ؤƤϡΤʤξǤ
.I timeval
¤Τ 2 ĤΥեɤ
(嵭Τ褦) ξȤ
.I long
Ǥꡢ¤Τ
.I <sys/time.h>
Ƥ롣
POSIX.1-2001 βǤϡʲΤ褦ˤʤäƤ롣

.in +4n
.nf
struct timeval {
	 time_t         tv_sec;     /*  */
	 suseconds_t    tv_usec;    /* ޥ */
};
.fi
.in

ι¤Τ
.I <sys/select.h>
Ƥꡢǡ
.I time_t

.I suseconds_t

.I <sys/types.h>
Ƥ롣
.LP
ץȥפ˴ؤƤϡΤʤξ
.BR select ()
Ȥϡ
.I <time.h>
򥤥󥯥롼ɤФ褤
POSIX.1-2001 δĶ
.BR select ()

.BR pselect ()
Ȥϡ
.I <sys/select.h>
򥤥󥯥롼ɤФ褤
إåե
.I <sys/select.h>
 libc4  libc5 ˤϤʤglibc 2.0 ʹߤ¸ߤ롣
Ȥ glibc 2.0 Ǥ
.BR pselect ()
ΥץȥפְäƤ롣
glibc 2.1-2.2.1 Ǥ
.B _GNU_SOURCE
Ƥˡ
.BR pselect ()
󶡤롣
glibc 2.2.2-2.2.4 Ǥ
.B _XOPEN_SOURCE
Ƥơͤ 600 ʾǤ
.BR pselect ()
󶡤롣
 POSIX.1-2001 ʹߤǤϡǥեȤǥץȥפ󶡤롣
.SS "Linux Ǥ"
Linux 
.BR pselect ()
ƥॳ
.I timeout
ѹ롣
 glibc ΥåѡؿϡƥॳϤ timeout 
ȤƥѿȤȤǤưäƤ롣
Τᡢglibc 
.BR pselect ()
ؿ timeout ѹʤ
줬 POSIX.1-2001 ׵ᤷƤưǤ롣
.SH Х
glibc 2.0 Ǥϡ
.I sigmask
ʤС
.BR pselect ()
󶡤Ƥ

С 2.1 ʹߤ glibc Ǥϡ
.BR pselect ()

.BR sigprocmask (2)

.BR select ()
Ȥäƥߥ졼ȤƤ
μˤϤɤˤȼĤäƤ롣
ζˤɻߤ뤿
.BR pselect ()
߷פ줿ΤǤ롣
.BR pselect ()
ʤƥˤơ
ʥª (ܿ⤤) ˡǹԤˤϡ
ʥѥ (self-pipe) ȤȤȤ褤 (ʥϥɥϥѥפ
1 ХȤΥǡ񤭹ߡƱѥפΤ⤦üᥤץ
.BR select ()
Ǵƻ뤹ȤˡǤ)

Linux Ǥϡ
.BR select ()
åȥեǥץ "ɤ߹ߤνǤ" 𤷤Ǥ⡢
θ read Ԥ (block) 뤳Ȥ롣Τ褦ʾϡ
㤨Сǡ夷ǥå۾郎ĤѴ줿
ʤɤ˵ꤨ롣¾ˤեǥץǤȴְä
𤵤뤫⤷ʤ
.\" Stevens 󤲤Τϡselect Ƥ accept 
.\" ƤФޤǤδ֤ˡ饤Ȥ RST ȡ
.\" accept ߤǤ롣
äơߤ٤ǤϤʤåȤФƤ
.B O_NONBLOCK
ȤȤǤ
.\" ¿ʬΤ褦ʾǤϥͥ EIO ֤Ƥޤ?

Linux Ǥϡ
.BR select ()
ʥϥɥˤޤ줿 (Ĥޤ
.B EINTR
顼֤)
.I timeout
ѹ롣
 POSIX.1-2001 ǤǧƤʤưǤ롣
Linux 
.BR pselect ()
ƥॳƱư򤹤뤬
glibc ΥåѡؿεưäƤ롣
Ūˤϡglibc Υåѡؿǡ
.I timeout
ѿ˥ԡ
Υѿ򥷥ƥॳϤƤ롣
.SH 
.nf
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

int
main(void)
{
    fd_set rfds;
    struct timeval tv;
    int retval;

    /* stdin (fd 0) ƻ뤷Ϥäɽ롣*/
    FD_ZERO(&rfds);
    FD_SET(0, &rfds);

    /* 5 ôִƻ뤹롣*/
    tv.tv_sec = 5;
    tv.tv_usec = 0;

    retval = select(1, &rfds, NULL, NULL, &tv);
    /* λǤ tv ͤꤷƤϤʤʤ*/

    if (retval == \-1)
	perror("select()");
    else if (retval)
        printf("ǡǤޤ\\n");
        /* FD_ISSET(0, &rfds)  true ˤʤ롣*/
    else
        printf("5 ð˥ǡϤޤǤ\\n");

    exit(EXIT_SUCCESS);
}
.fi
.SH Ϣ
ͻȻν񤫤줿塼ȥꥢȤơ
.BR select_tut (2)
.LP
طꤽʤΤ󤲤Ƥ:
.BR accept (2),
.BR connect (2),
.BR poll (2),
.BR read (2),
.BR recv (2),
.BR send (2),
.BR sigprocmask (2),
.BR write (2),
.BR epoll (7)