1 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
2 .\" copyright (C) 1995 Michael Shields.
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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
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
38 .TH SELECT 2 2012-08-17 "Linux" "Linux Programmer's Manual"
40 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
41 synchronous I/O multiplexing
44 /* According to POSIX.1-2001 */
46 .B #include <sys/select.h>
48 /* According to earlier standards */
50 .B #include <sys/time.h>
52 .B #include <sys/types.h>
54 .B #include <unistd.h>
56 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
57 .BI " fd_set *" exceptfds ", struct timeval *" timeout );
59 .BI "void FD_CLR(int " fd ", fd_set *" set );
61 .BI "int FD_ISSET(int " fd ", fd_set *" set );
63 .BI "void FD_SET(int " fd ", fd_set *" set );
65 .BI "void FD_ZERO(fd_set *" set );
67 .B #include <sys/select.h>
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 );
75 Feature Test Macro Requirements for glibc (see
76 .BR feature_test_macros (7)):
80 _POSIX_C_SOURCE\ >=\ 200112L || _XOPEN_SOURCE\ >=\ 600
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.,
97 is identical, other than these three differences:
101 uses a timeout that is a
103 (with seconds and microseconds), while
107 (with seconds and nanoseconds).
113 argument to indicate how much time was left.
115 does not change this argument.
121 argument, and behaves as
126 Three independent sets of file descriptors are watched.
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),
134 will be watched to see if a write will not block, and
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
144 Four macros are provided to manipulate the sets.
150 respectively add and remove a given file descriptor from a set.
152 tests to see if a file descriptor is part of the set;
158 is the highest-numbered file descriptor in any of the three sets, plus 1.
162 argument specifies the minimum interval that
164 should block waiting for a file descriptor to become ready.
165 (This interval will be rounded up to the system clock granularity,
166 and kernel scheduling delays mean that the blocking interval
167 may overrun by a small amount.)
168 If both fields of the
170 structure are zero, then
173 (This is useful for polling.)
176 is NULL (no timeout),
178 can block indefinitely.
181 is a pointer to a signal mask (see
182 .BR sigprocmask (2));
183 if it is not NULL, then
185 first replaces the current signal mask by the one pointed to by
187 then does the "select" function, and then restores the original
190 Other than the difference in the precision of the
192 argument, the following
197 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
203 executing the following calls:
208 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
209 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
210 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
215 is needed is that if one wants to wait for either a signal
216 or for a file descriptor to become ready, then
217 an atomic test is needed to prevent race conditions.
218 (Suppose the signal handler sets a global flag and
220 Then a test of this global flag followed by a call of
222 could hang indefinitely if the signal arrived just after the test
223 but just before the call.
226 allows one to first block signals, handle the signals that have come in,
233 The time structures involved are defined in
240 long tv_sec; /* seconds */
241 long tv_usec; /* microseconds */
251 long tv_sec; /* seconds */
252 long tv_nsec; /* nanoseconds */
257 (However, see below on the POSIX.1-2001 versions.)
261 with all three sets empty,
265 as a fairly portable way to sleep with subsecond precision.
271 to reflect the amount of time not slept; most other implementations
273 (POSIX.1-2001 permits either behavior.)
274 This causes problems both when Linux code which reads
276 is ported to other operating systems, and when code is ported to Linux
277 that reuses a \fIstruct timeval\fP for multiple
279 in a loop without reinitializing it.
282 to be undefined after
285 .\" .PP - it is rumored that:
286 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
287 .\" - it is certainly true that:
288 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
294 return the number of file descriptors contained in the three returned
295 descriptor sets (that is, the total number of bits that are set in
299 which may be zero if the timeout expires before anything interesting happens.
300 On error, \-1 is returned, and
302 is set appropriately; the sets and
304 become undefined, so do not
305 rely on their contents after an error.
309 An invalid file descriptor was given in one of the sets.
310 (Perhaps a file descriptor that was already closed,
311 or one on which an error has occurred.)
314 A signal was caught; see
319 is negative or the value contained within
324 unable to allocate memory for internal tables.
327 was added to Linux in kernel 2.6.16.
330 was emulated in glibc (but see BUGS).
333 conforms to POSIX.1-2001 and
336 first appeared in 4.2BSD).
337 Generally portable to/from
338 non-BSD systems supporting clones of the BSD socket layer (including
340 However, note that the System V variant typically
341 sets the timeout variable before exit, but the BSD variant does not.
344 is defined in POSIX.1g, and in
349 is a fixed size buffer.
356 that is negative or is equal to or larger than
359 in undefined behavior.
360 Moreover, POSIX requires
362 to be a valid file descriptor.
364 Concerning the types involved, the classical situation is that
367 structure are typed as
369 (as shown above), and the structure is defined in
371 The POSIX.1-2001 situation is
376 time_t tv_sec; /* seconds */
377 suseconds_t tv_usec; /* microseconds */
382 where the structure is defined in
391 Concerning prototypes, the classical situation is that one should
396 The POSIX.1-2001 situation is that one should include
403 Libc4 and libc5 do not have a
405 header; under glibc 2.0 and later this header exists.
406 Under glibc 2.0 it unconditionally gives the wrong prototype for
408 Under glibc 2.1 to 2.2.1 it gives
413 Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
414 .SS Multithreaded applications
415 If a file descriptor being monitored by
417 is closed in another thread, the result is unspecified.
418 On some UNIX systems,
420 unblocks and returns, with an indication that the file descriptor is ready
421 (a subsequent I/O operation will likely fail with an error,
422 unless another the file descriptor reopened between the time
424 returned and the I/O operations was performed).
425 On Linux (and some other systems),
426 closing the file descriptor in another thread has no effect on
428 In summary, any application that relies on a particular behavior
429 in this scenario must be considered buggy.
433 interface described in this page is implemented by glibc.
434 The underlying Linux system call is named
436 This system call has somewhat different behavior from the glibc
441 system call modifies its
444 However, the glibc wrapper function hides this behavior
445 by using a local variable for the timeout argument that
446 is passed to the system call.
449 function does not modify its
452 this is the behavior required by POSIX.1-2001.
454 The final argument of the
458 pointer, but is instead a structure of the form:
463 const sigset_t *ss; /* Pointer to signal set */
464 size_t ss_len; /* Size (in bytes) of object pointed
470 This allows the system call to obtain both
471 a pointer to the signal set and its size,
472 while allowing for the fact that most architectures
473 support a maximum of 6 arguments to a system call.
475 Glibc 2.0 provided a version of
481 Starting with version 2.1, glibc provided an emulation of
483 that was implemented using
487 This implementation remained vulnerable to the very race condition that
489 was designed to prevent.
490 Modern versions of glibc use the (race-free)
492 system call on kernels where it is provided.
496 reliable (and more portable) signal trapping can be achieved
497 using the self-pipe trick.
499 a signal handler writes a byte to a pipe whose other end
503 (To avoid possibly blocking when writing to a pipe that may be full
504 or reading from a pipe that may be empty,
505 nonblocking I/O is used when reading from and writing to the pipe.)
509 may report a socket file descriptor as "ready for reading", while
510 nevertheless a subsequent read blocks.
511 This could for example
512 happen when data has arrived but upon examination has wrong
513 checksum and is discarded.
514 There may be other circumstances
515 in which a file descriptor is spuriously reported as ready.
516 .\" Stevens discusses a case where accept can block after select
517 .\" returns successfully because of an intervening RST from the client.
518 Thus it may be safer to use
520 on sockets that should not block.
521 .\" Maybe the kernel should have returned EIO in such a situation?
527 if the call is interrupted by a signal handler (i.e., the
530 This is not permitted by POSIX.1-2001.
533 system call has the same behavior,
534 but the glibc wrapper hides this behavior by internally copying the
536 to a local variable and passing that variable to the system call.
541 #include <sys/time.h>
542 #include <sys/types.h>
552 /* Watch stdin (fd 0) to see when it has input. */
556 /* Wait up to five seconds. */
560 retval = select(1, &rfds, NULL, NULL, &tv);
561 /* Don't rely on the value of tv now! */
566 printf("Data is available now.\\n");
567 /* FD_ISSET(0, &rfds) will be true. */
569 printf("No data within five seconds.\\n");
586 For a tutorial with discussion and examples, see