1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is copyright (C) 1992 Drew Eckhardt,
4 .\" copyright (C) 1995 Michael Shields.
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.
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.
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" 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 2006-03-11 "Linux 2.6.16" "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 \fBint select(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
57 fd_set *\fIexceptfds\fB, struct timeval *\fItimeout\fB);
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 #define _XOPEN_SOURCE 600
68 .B #include <sys/select.h>
70 \fBint pselect(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
71 fd_set *\fIexceptfds\fB, const struct timespec *\fItimeout\fB,
72 const sigset_t *\fIsigmask\fB);
78 allow a program to monitor multiple file descriptors,
79 waiting until one or more of the file descriptors become "ready"
80 for some class of I/O operation (e.g., input possible).
81 A file descriptor is considered ready if it is possible to
82 perform the corresponding I/O operation (e.g.,
90 is identical, with three differences:
94 uses a timeout that is a
96 (with seconds and microseconds), while
100 (with seconds and nanoseconds).
106 argument to indicate how much time was left.
108 does not change this argument.
114 argument, and behaves as
119 Three independent sets of file descriptors are watched.
122 will be watched to see if characters become
123 available for reading (more precisely, to see if a read will not
124 block; in particular, a file descriptor is also ready on end-of-file),
127 will be watched to see if a write will not block, and
130 will be watched for exceptions.
131 On exit, the sets are modified in place
132 to indicate which file descriptors actually changed status.
133 Each of the three file descriptor sets may be specified as NULL
134 if no file descriptors are to be watched for the corresponding class
137 Four macros are provided to manipulate the sets.
143 respectively add and remove a given file descriptor from a set.
145 tests to see if a file descriptor is part of the set;
151 is the highest-numbered file descriptor in any of the three sets, plus 1.
154 is an upper bound on the amount of time elapsed before
157 It may be zero, causing
159 to return immediately.
160 (This is useful for polling.)
163 is NULL (no timeout),
165 can block indefinitely.
168 is a pointer to a signal mask (see
169 .BR sigprocmask (2));
170 if it is not NULL, then
172 first replaces the current signal mask by the one pointed to by
174 then does the `select' function, and then restores the original
177 Other than the difference in the precision of the
179 argument, the following
184 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
190 executing the following calls:
195 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
196 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
197 sigprocmask(SIG_SETMASK, &origmask, NULL);
202 is needed is that if one wants to wait for either a signal
203 or for a file descriptor to become ready, then
204 an atomic test is needed to prevent race conditions.
205 (Suppose the signal handler sets a global flag and
207 Then a test of this global flag followed by a call of
209 could hang indefinitely if the signal arrived just after the test
210 but just before the call.
213 allows one to first block signals, handle the signals that have come in,
220 The time structures involved are defined in
227 long tv_sec; /* seconds */
228 long tv_usec; /* microseconds */
238 long tv_sec; /* seconds */
239 long tv_nsec; /* nanoseconds */
244 (However, see below on the POSIX.1-2001 versions.)
248 with all three sets empty,
252 as a fairly portable way to sleep with subsecond precision.
258 to reflect the amount of time not slept; most other implementations
260 (POSIX.1-2001 permits either behaviour.)
261 This causes problems both when Linux code which reads
263 is ported to other operating systems, and when code is ported to Linux
264 that reuses a struct timeval for multiple
266 in a loop without reinitializing it.
269 to be undefined after
272 .\" .PP - it is rumoured that:
273 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
274 .\" - it is certainly true that:
275 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
281 return the number of file descriptors contained in the three returned
282 descriptor sets (that is, the total number of bits that are set in
286 which may be zero if the timeout expires before anything interesting happens.
287 On error, \-1 is returned, and
289 is set appropriately; the sets and
291 become undefined, so do not
292 rely on their contents after an error.
296 An invalid file descriptor was given in one of the sets.
297 (Perhaps a file descriptor that was already closed,
298 or one on which an error has occurred.)
305 is negative or the value contained within
310 unable to allocate memory for internal tables.
314 #include <sys/time.h>
315 #include <sys/types.h>
325 /* Watch stdin (fd 0) to see when it has input. */
329 /* Wait up to five seconds. */
333 retval = select(1, &rfds, NULL, NULL, &tv);
334 /* Don't rely on the value of tv now! */
339 printf("Data is available now.\\n");
340 /* FD_ISSET(0, &rfds) will be true. */
342 printf("No data within five seconds.\\n");
349 conforms to POSIX.1-2001 and
352 first appeared in 4.2BSD).
353 Generally portable to/from
354 non-BSD systems supporting clones of the BSD socket layer (including
356 However, note that the System V variant typically
357 sets the timeout variable before exit, but the BSD variant does not.
360 is defined in POSIX.1g, and in
365 is a fixed size buffer.
372 that is negative or is equal to or larger than FD_SETSIZE will result
373 in undefined behavior.
374 Moreover, POSIX requires
376 to be a valid file descriptor.
378 Concerning the types involved, the classical situation is that
381 structure are longs (as shown above),
382 and the structure is defined in
384 The POSIX.1-2001 situation is
389 time_t tv_sec; /* seconds */
390 suseconds_t tv_usec; /* microseconds */
395 where the structure is defined in
404 Concerning prototypes, the classical situation is that one should
409 The POSIX.1-2001 situation is that one should include
415 Libc4 and libc5 do not have a
417 header; under glibc 2.0 and later this header exists.
418 Under glibc 2.0 it unconditionally gives the wrong prototype for
420 under glibc 2.1-2.2.1 it gives
424 is defined, under glibc 2.2.2-2.2.4 it gives it when
426 is defined and has a value of 600 or larger.
427 No doubt, since POSIX.1-2001, it should give the prototype by default.
430 was added to Linux in kernel 2.6.16.
433 was emulated in glibc (but see BUGS).
437 system call modifies its
440 However, the glibc wrapper function hides this behaviour
441 by using a local variable for the timeout argument that
442 is passed to the system call.
445 function does not modify its timeout argument;
446 this is the behaviour required by POSIX.1-2001.
448 Glibc 2.0 provided a version of
454 Since version 2.1, glibc has provided an emulation of
456 that is implemented using
460 This implementation remains vulnerable to the very race condition that
462 was designed to prevent.
465 reliable (and more portable) signal trapping can be achieved
466 using the self-pipe trick
467 (where a signal handler writes a byte to a pipe whose other end
470 in the main program.)
474 may report a socket file descriptor as "ready for reading", while
475 nevertheless a subsequent read blocks.
476 This could for example
477 happen when data has arrived but upon examination has wrong
478 checksum and is discarded.
479 There may be other circumstances
480 in which a file descriptor is spuriously reported as ready.
481 .\" Stevens discusses a case where accept can block after select
482 .\" returns successfully because of an intervening RST from the client.
483 Thus it may be safer to use O_NONBLOCK on sockets that should not block.
484 .\" Maybe the kernel should have returned EIO in such a situation?
486 .\" FIXME select() (and pselect()?) also modify the timeout
487 .\" on an EINTR error return; POSIX.1-2001 doesn't permit this.
489 For a tutorial with discussion and examples, see
492 For vaguely related stuff, see
502 .BR feature_test_macros (7)