OSDN Git Service

(split) LDP: Update original to LDP v3.39.
[linuxjm/LDP_man-pages.git] / original / man2 / select.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
4 .\"                 copyright (C) 1995 Michael Shields.
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 .\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
28 .\" Sun Feb 11 14:07:00 MET 1996  Martin Schulze  <joey@linux.de>
29 .\"     * layout slightly modified
30 .\"
31 .\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb
33 .\" Modified Thu Feb  9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb
34 .\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net>
35 .\" 2005-03-11, mtk, modified pselect() text (it is now a system
36 .\"     call in 2.6.16.
37 .\"
38 .TH SELECT 2 2010-08-31 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
41 synchronous I/O multiplexing
42 .SH SYNOPSIS
43 .nf
44 /* According to POSIX.1-2001 */
45 .br
46 .B #include <sys/select.h>
47 .sp
48 /* According to earlier standards */
49 .br
50 .B #include <sys/time.h>
51 .br
52 .B #include <sys/types.h>
53 .br
54 .B #include <unistd.h>
55 .sp
56 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
57 .BI "           fd_set *" exceptfds ", struct timeval *" timeout );
58 .sp
59 .BI "void FD_CLR(int " fd ", fd_set *" set );
60 .br
61 .BI "int  FD_ISSET(int " fd ", fd_set *" set );
62 .br
63 .BI "void FD_SET(int " fd ", fd_set *" set );
64 .br
65 .BI "void FD_ZERO(fd_set *" set );
66 .sp
67 .B #include <sys/select.h>
68 .sp
69 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
70 .BI "            fd_set *" exceptfds ", const struct timespec *" timeout ,
71 .BI "            const sigset_t *" sigmask );
72 .fi
73 .sp
74 .in -4n
75 Feature Test Macro Requirements for glibc (see
76 .BR feature_test_macros (7)):
77 .in
78 .sp
79 .BR pselect ():
80 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
81 .SH DESCRIPTION
82 .BR select ()
83 and
84 .BR pselect ()
85 allow a program to monitor multiple file descriptors,
86 waiting until one or more of the file descriptors become "ready"
87 for some class of I/O operation (e.g., input possible).
88 A file descriptor is considered ready if it is possible to
89 perform the corresponding I/O operation (e.g.,
90 .BR read (2))
91 without blocking.
92 .PP
93 The operation of
94 .BR select ()
95 and
96 .BR pselect ()
97 is identical, with three differences:
98 .TP
99 (i)
100 .BR select ()
101 uses a timeout that is a
102 .I struct timeval
103 (with seconds and microseconds), while
104 .BR pselect ()
105 uses a
106 .I struct timespec
107 (with seconds and nanoseconds).
108 .TP
109 (ii)
110 .BR select ()
111 may update the
112 .I timeout
113 argument to indicate how much time was left.
114 .BR pselect ()
115 does not change this argument.
116 .TP
117 (iii)
118 .BR select ()
119 has no
120 .I sigmask
121 argument, and behaves as
122 .BR pselect ()
123 called with NULL
124 .IR sigmask .
125 .PP
126 Three independent sets of file descriptors are watched.
127 Those listed in
128 .I readfds
129 will be watched to see if characters become
130 available for reading (more precisely, to see if a read will not
131 block; in particular, a file descriptor is also ready on end-of-file),
132 those in
133 .I writefds
134 will be watched to see if a write will not block, and
135 those in
136 .I exceptfds
137 will be watched for exceptions.
138 On exit, the sets are modified in place
139 to indicate which file descriptors actually changed status.
140 Each of the three file descriptor sets may be specified as NULL
141 if no file descriptors are to be watched for the corresponding class
142 of events.
143 .PP
144 Four macros are provided to manipulate the sets.
145 .BR FD_ZERO ()
146 clears a set.
147 .BR FD_SET ()
148 and
149 .BR FD_CLR ()
150 respectively add and remove a given file descriptor from a set.
151 .BR FD_ISSET ()
152 tests to see if a file descriptor is part of the set;
153 this is useful after
154 .BR select ()
155 returns.
156 .PP
157 .I nfds
158 is the highest-numbered file descriptor in any of the three sets, plus 1.
159 .PP
160 .I timeout
161 is an upper bound on the amount of time elapsed before
162 .BR select ()
163 returns.
164 If both fields of the
165 .I timeval
166 structure are zero, then
167 .BR select ()
168 returns immediately.
169 (This is useful for polling.)
170 If
171 .I timeout
172 is NULL (no timeout),
173 .BR select ()
174 can block indefinitely.
175 .PP
176 .I sigmask
177 is a pointer to a signal mask (see
178 .BR sigprocmask (2));
179 if it is not NULL, then
180 .BR pselect ()
181 first replaces the current signal mask by the one pointed to by
182 .IR sigmask ,
183 then does the "select" function, and then restores the original
184 signal mask.
185 .PP
186 Other than the difference in the precision of the
187 .I timeout
188 argument, the following
189 .BR pselect ()
190 call:
191 .nf
192
193     ready = pselect(nfds, &readfds, &writefds, &exceptfds,
194                     timeout, &sigmask);
195
196 .fi
197 is equivalent to
198 .I atomically
199 executing the following calls:
200 .nf
201
202     sigset_t origmask;
203
204     sigprocmask(SIG_SETMASK, &sigmask, &origmask);
205     ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
206     sigprocmask(SIG_SETMASK, &origmask, NULL);
207 .fi
208 .PP
209 The reason that
210 .BR pselect ()
211 is needed is that if one wants to wait for either a signal
212 or for a file descriptor to become ready, then
213 an atomic test is needed to prevent race conditions.
214 (Suppose the signal handler sets a global flag and
215 returns.
216 Then a test of this global flag followed by a call of
217 .BR select ()
218 could hang indefinitely if the signal arrived just after the test
219 but just before the call.
220 By contrast,
221 .BR pselect ()
222 allows one to first block signals, handle the signals that have come in,
223 then call
224 .BR pselect ()
225 with the desired
226 .IR sigmask ,
227 avoiding the race.)
228 .SS "The timeout"
229 The time structures involved are defined in
230 .I <sys/time.h>
231 and look like
232
233 .in +4n
234 .nf
235 struct timeval {
236     long    tv_sec;         /* seconds */
237     long    tv_usec;        /* microseconds */
238 };
239 .fi
240 .in
241
242 and
243
244 .in +4n
245 .nf
246 struct timespec {
247     long    tv_sec;         /* seconds */
248     long    tv_nsec;        /* nanoseconds */
249 };
250 .fi
251 .in
252
253 (However, see below on the POSIX.1-2001 versions.)
254 .PP
255 Some code calls
256 .BR select ()
257 with all three sets empty,
258 .I nfds
259 zero, and a non-NULL
260 .I timeout
261 as a fairly portable way to sleep with subsecond precision.
262 .PP
263 On Linux,
264 .BR select ()
265 modifies
266 .I timeout
267 to reflect the amount of time not slept; most other implementations
268 do not do this.
269 (POSIX.1-2001 permits either behavior.)
270 This causes problems both when Linux code which reads
271 .I timeout
272 is ported to other operating systems, and when code is ported to Linux
273 that reuses a \fIstruct timeval\fP for multiple
274 .BR select ()s
275 in a loop without reinitializing it.
276 Consider
277 .I timeout
278 to be undefined after
279 .BR select ()
280 returns.
281 .\" .PP - it is rumored that:
282 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
283 .\" - it is certainly true that:
284 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
285 .SH "RETURN VALUE"
286 On success,
287 .BR select ()
288 and
289 .BR pselect ()
290 return the number of file descriptors contained in the three returned
291 descriptor sets (that is, the total number of bits that are set in
292 .IR readfds ,
293 .IR writefds ,
294 .IR exceptfds )
295 which may be zero if the timeout expires before anything interesting happens.
296 On error, \-1 is returned, and
297 .I errno
298 is set appropriately; the sets and
299 .I timeout
300 become undefined, so do not
301 rely on their contents after an error.
302 .SH ERRORS
303 .TP
304 .B EBADF
305 An invalid file descriptor was given in one of the sets.
306 (Perhaps a file descriptor that was already closed,
307 or one on which an error has occurred.)
308 .TP
309 .B EINTR
310 A signal was caught; see
311 .BR signal (7).
312 .TP
313 .B EINVAL
314 .I nfds
315 is negative or the value contained within
316 .I timeout
317 is invalid.
318 .TP
319 .B ENOMEM
320 unable to allocate memory for internal tables.
321 .SH VERSIONS
322 .BR pselect ()
323 was added to Linux in kernel 2.6.16.
324 Prior to this,
325 .BR pselect ()
326 was emulated in glibc (but see BUGS).
327 .SH "CONFORMING TO"
328 .BR select ()
329 conforms to POSIX.1-2001 and
330 4.4BSD
331 .RB ( select ()
332 first appeared in 4.2BSD).
333 Generally portable to/from
334 non-BSD systems supporting clones of the BSD socket layer (including
335 System V variants).
336 However, note that the System V variant typically
337 sets the timeout variable before exit, but the BSD variant does not.
338 .PP
339 .BR pselect ()
340 is defined in POSIX.1g, and in
341 POSIX.1-2001.
342 .SH NOTES
343 An
344 .I fd_set
345 is a fixed size buffer.
346 Executing
347 .BR FD_CLR ()
348 or
349 .BR FD_SET ()
350 with a value of
351 .I fd
352 that is negative or is equal to or larger than
353 .B FD_SETSIZE
354 will result
355 in undefined behavior.
356 Moreover, POSIX requires
357 .I fd
358 to be a valid file descriptor.
359
360 Concerning the types involved, the classical situation is that
361 the two fields of a
362 .I timeval
363 structure are typed as
364 .I long
365 (as shown above), and the structure is defined in
366 .IR <sys/time.h> .
367 The POSIX.1-2001 situation is
368
369 .in +4n
370 .nf
371 struct timeval {
372     time_t         tv_sec;     /* seconds */
373     suseconds_t    tv_usec;    /* microseconds */
374 };
375 .fi
376 .in
377
378 where the structure is defined in
379 .I <sys/select.h>
380 and the data types
381 .I time_t
382 and
383 .I suseconds_t
384 are defined in
385 .IR <sys/types.h> .
386 .LP
387 Concerning prototypes, the classical situation is that one should
388 include
389 .I <time.h>
390 for
391 .BR select ().
392 The POSIX.1-2001 situation is that one should include
393 .I <sys/select.h>
394 for
395 .BR select ()
396 and
397 .BR pselect ().
398
399 Libc4 and libc5 do not have a
400 .I <sys/select.h>
401 header; under glibc 2.0 and later this header exists.
402 Under glibc 2.0 it unconditionally gives the wrong prototype for
403 .BR pselect ().
404 Under glibc 2.1 to 2.2.1 it gives
405 .BR pselect ()
406 when
407 .B _GNU_SOURCE
408 is defined.
409 Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
410 .SS "Linux Notes"
411 The Linux
412 .BR pselect ()
413 system call modifies its
414 .I timeout
415 argument.
416 However, the glibc wrapper function hides this behavior
417 by using a local variable for the timeout argument that
418 is passed to the system call.
419 Thus, the glibc
420 .BR pselect ()
421 function does not modify its
422 .I timeout
423 argument;
424 this is the behavior required by POSIX.1-2001.
425 .SH BUGS
426 Glibc 2.0 provided a version of
427 .BR pselect ()
428 that did not take a
429 .I sigmask
430 argument.
431
432 Starting with version 2.1, glibc provided an emulation of
433 .BR pselect ()
434 that was implemented using
435 .BR sigprocmask (2)
436 and
437 .BR select ().
438 This implementation remained vulnerable to the very race condition that
439 .BR pselect ()
440 was designed to prevent.
441 Modern versions of glibc use the (race-free)
442 .BR pselect ()
443 system call on kernels where it is provided.
444
445 On systems that lack
446 .BR pselect (),
447 reliable (and more portable) signal trapping can be achieved
448 using the self-pipe trick
449 (where a signal handler writes a byte to a pipe whose other end
450 is monitored by
451 .BR select ()
452 in the main program.)
453
454 Under Linux,
455 .BR select ()
456 may report a socket file descriptor as "ready for reading", while
457 nevertheless a subsequent read blocks.
458 This could for example
459 happen when data has arrived but upon examination has wrong
460 checksum and is discarded.
461 There may be other circumstances
462 in which a file descriptor is spuriously reported as ready.
463 .\" Stevens discusses a case where accept can block after select
464 .\" returns successfully because of an intervening RST from the client.
465 Thus it may be safer to use
466 .B O_NONBLOCK
467 on sockets that should not block.
468 .\" Maybe the kernel should have returned EIO in such a situation?
469
470 On Linux,
471 .BR select ()
472 also modifies
473 .I timeout
474 if the call is interrupted by a signal handler (i.e., the
475 .B EINTR
476 error return).
477 This is not permitted by POSIX.1-2001.
478 The Linux
479 .BR pselect ()
480 system call has the same behavior,
481 but the glibc wrapper hides this behavior by internally copying the
482 .I timeout
483 to a local variable and passing that variable to the system call.
484 .SH EXAMPLE
485 .nf
486 #include <stdio.h>
487 #include <stdlib.h>
488 #include <sys/time.h>
489 #include <sys/types.h>
490 #include <unistd.h>
491
492 int
493 main(void)
494 {
495     fd_set rfds;
496     struct timeval tv;
497     int retval;
498
499     /* Watch stdin (fd 0) to see when it has input. */
500     FD_ZERO(&rfds);
501     FD_SET(0, &rfds);
502
503     /* Wait up to five seconds. */
504     tv.tv_sec = 5;
505     tv.tv_usec = 0;
506
507     retval = select(1, &rfds, NULL, NULL, &tv);
508     /* Don't rely on the value of tv now! */
509
510     if (retval == \-1)
511         perror("select()");
512     else if (retval)
513         printf("Data is available now.\\n");
514         /* FD_ISSET(0, &rfds) will be true. */
515     else
516         printf("No data within five seconds.\\n");
517
518     exit(EXIT_SUCCESS);
519 }
520 .fi
521 .SH "SEE ALSO"
522 For a tutorial with discussion and examples, see
523 .BR select_tut (2).
524 .LP
525 For vaguely related stuff, see
526 .BR accept (2),
527 .BR connect (2),
528 .BR poll (2),
529 .BR read (2),
530 .BR recv (2),
531 .BR send (2),
532 .BR sigprocmask (2),
533 .BR write (2),
534 .BR epoll (7),
535 .BR time (7)