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 2017-09-15 "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, POSIX.1-2008 */
45 .B #include <sys/select.h>
47 /* According to earlier standards */
48 .B #include <sys/time.h>
49 .B #include <sys/types.h>
50 .B #include <unistd.h>
52 .BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
53 .BI " fd_set *" exceptfds ", struct timeval *" timeout );
55 .BI "void FD_CLR(int " fd ", fd_set *" set );
56 .BI "int FD_ISSET(int " fd ", fd_set *" set );
57 .BI "void FD_SET(int " fd ", fd_set *" set );
58 .BI "void FD_ZERO(fd_set *" set );
60 .B #include <sys/select.h>
62 .BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
63 .BI " fd_set *" exceptfds ", const struct timespec *" timeout ,
64 .BI " const sigset_t *" sigmask );
68 Feature Test Macro Requirements for glibc (see
69 .BR feature_test_macros (7)):
73 _POSIX_C_SOURCE\ >=\ 200112L
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 a corresponding I/O operation (e.g.,
84 without blocking, or a sufficiently small
88 can monitor only file descriptors numbers that are less than
91 does not have this limitation.
98 is identical, other than these three differences:
102 uses a timeout that is a
104 (with seconds and microseconds), while
108 (with seconds and nanoseconds).
114 argument to indicate how much time was left.
116 does not change this argument.
122 argument, and behaves as
127 Three independent sets of file descriptors are watched.
128 The file descriptors listed in
130 will be watched to see if characters become
131 available for reading (more precisely, to see if a read will not
132 block; in particular, a file descriptor is also ready on end-of-file).
133 The file descriptors in
135 will be watched to see if space is available for write (though a large
136 write may still block).
137 The file descriptors in
139 will be watched for exceptional conditions.
140 (For examples of some exceptional conditions, see the discussion of
145 On exit, each of the file descriptor sets is modified in place
146 to indicate which file descriptors actually changed status.
149 within a loop, the sets must be reinitialized before each call.)
151 Each of the three file descriptor sets may be specified as NULL
152 if no file descriptors are to be watched for the corresponding class
155 Four macros are provided to manipulate the sets.
161 respectively add and remove a given file descriptor from a set.
163 tests to see if a file descriptor is part of the set;
169 should be set to the highest-numbered file descriptor in any
170 of the three sets, plus 1.
171 The indicated file descriptors in each set are checked, up to this limit
176 argument specifies the interval that
178 should block waiting for a file descriptor to become ready.
179 The call will block until either:
181 a file descriptor becomes ready;
183 the call is interrupted by a signal handler; or
189 interval will be rounded up to the system clock granularity,
190 and kernel scheduling delays mean that the blocking interval
191 may overrun by a small amount.
192 If both fields of the
194 structure are zero, then
197 (This is useful for polling.)
200 is NULL (no timeout),
202 can block indefinitely.
205 is a pointer to a signal mask (see
206 .BR sigprocmask (2));
207 if it is not NULL, then
209 first replaces the current signal mask by the one pointed to by
211 then does the "select" function, and then restores the original
214 Other than the difference in the precision of the
216 argument, the following
222 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
229 executing the following calls:
235 pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
236 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
237 pthread_sigmask(SIG_SETMASK, &origmask, NULL);
244 is needed is that if one wants to wait for either a signal
245 or for a file descriptor to become ready, then
246 an atomic test is needed to prevent race conditions.
247 (Suppose the signal handler sets a global flag and
249 Then a test of this global flag followed by a call of
251 could hang indefinitely if the signal arrived just after the test
252 but just before the call.
255 allows one to first block signals, handle the signals that have come in,
262 The time structures involved are defined in
269 long tv_sec; /* seconds */
270 long tv_usec; /* microseconds */
280 long tv_sec; /* seconds */
281 long tv_nsec; /* nanoseconds */
286 (However, see below on the POSIX.1 versions.)
290 with all three sets empty,
294 as a fairly portable way to sleep with subsecond precision.
300 to reflect the amount of time not slept; most other implementations
302 (POSIX.1 permits either behavior.)
303 This causes problems both when Linux code which reads
305 is ported to other operating systems, and when code is ported to Linux
306 that reuses a \fIstruct timeval\fP for multiple
308 in a loop without reinitializing it.
311 to be undefined after
314 .\" .PP - it is rumored that:
315 .\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
316 .\" - it is certainly true that:
317 .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
323 return the number of file descriptors contained in the three returned
324 descriptor sets (that is, the total number of bits that are set in
328 which may be zero if the timeout expires before anything interesting happens.
329 On error, \-1 is returned, and
331 is set to indicate the error;
332 the file descriptor sets are unmodified,
339 An invalid file descriptor was given in one of the sets.
340 (Perhaps a file descriptor that was already closed,
341 or one on which an error has occurred.)
345 A signal was caught; see
350 is negative or exceeds the
356 The value contained within
361 Unable to allocate memory for internal tables.
364 was added to Linux in kernel 2.6.16.
367 was emulated in glibc (but see BUGS).
370 conforms to POSIX.1-2001, POSIX.1-2008, and
373 first appeared in 4.2BSD).
374 Generally portable to/from
375 non-BSD systems supporting clones of the BSD socket layer (including
377 However, note that the System\ V variant typically
378 sets the timeout variable before exit, but the BSD variant does not.
381 is defined in POSIX.1g, and in
382 POSIX.1-2001 and POSIX.1-2008.
386 is a fixed size buffer.
393 that is negative or is equal to or larger than
396 in undefined behavior.
397 Moreover, POSIX requires
399 to be a valid file descriptor.
405 is not affected by the
409 On some other UNIX systems,
410 .\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
412 can fail with the error
414 if the system fails to allocate kernel-internal resources, rather than
417 POSIX specifies this error for
421 Portable programs may wish to check for
423 and loop, just as with
428 reliable (and more portable) signal trapping can be achieved
429 using the self-pipe trick.
431 a signal handler writes a byte to a pipe whose other end
435 (To avoid possibly blocking when writing to a pipe that may be full
436 or reading from a pipe that may be empty,
437 nonblocking I/O is used when reading from and writing to the pipe.)
439 Concerning the types involved, the classical situation is that
442 structure are typed as
444 (as shown above), and the structure is defined in
446 The POSIX.1 situation is
451 time_t tv_sec; /* seconds */
452 suseconds_t tv_usec; /* microseconds */
457 where the structure is defined in
466 Concerning prototypes, the classical situation is that one should
471 The POSIX.1 situation is that one should include
480 gives the wrong prototype for
482 Under glibc 2.1 to 2.2.1, it gives
487 Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS.
489 .SS Correspondence between select() and poll() notifications
490 Within the Linux kernel source,
492 we find the following definitions which show the correspondence
493 between the readable, writable, and exceptional condition notifications of
495 and the event notifications provided by
502 #define POLLIN_SET (POLLRDNORM | POLLRDBAND | POLLIN | POLLHUP |
504 /* Ready for reading */
505 #define POLLOUT_SET (POLLWRBAND | POLLWRNORM | POLLOUT | POLLERR)
506 /* Ready for writing */
507 #define POLLEX_SET (POLLPRI)
508 /* Exceptional condition */
512 .SS Multithreaded applications
513 If a file descriptor being monitored by
515 is closed in another thread, the result is unspecified.
516 On some UNIX systems,
518 unblocks and returns, with an indication that the file descriptor is ready
519 (a subsequent I/O operation will likely fail with an error,
520 unless another process reopens file descriptor between the time
522 returned and the I/O operation is performed).
523 On Linux (and some other systems),
524 closing the file descriptor in another thread has no effect on
526 In summary, any application that relies on a particular behavior
527 in this scenario must be considered buggy.
529 .SS C library/kernel differences
530 The Linux kernel allows file descriptor sets of arbitrary size,
531 determining the length of the sets to be checked from the value of
533 However, in the glibc implementation, the
535 type is fixed in size.
540 interface described in this page is implemented by glibc.
541 The underlying Linux system call is named
543 This system call has somewhat different behavior from the glibc
548 system call modifies its
551 However, the glibc wrapper function hides this behavior
552 by using a local variable for the timeout argument that
553 is passed to the system call.
556 function does not modify its
559 this is the behavior required by POSIX.1-2001.
561 The final argument of the
565 pointer, but is instead a structure of the form:
570 const kernel_sigset_t *ss; /* Pointer to signal set */
571 size_t ss_len; /* Size (in bytes) of object
572 pointed to by 'ss' */
577 This allows the system call to obtain both
578 a pointer to the signal set and its size,
579 while allowing for the fact that most architectures
580 support a maximum of 6 arguments to a system call.
583 for a discussion of the difference between the kernel and libc
584 notion of the signal set.
586 POSIX allows an implementation to define an upper limit,
587 advertised via the constant
589 on the range of file descriptors that can be specified
590 in a file descriptor set.
591 The Linux kernel imposes no fixed limit, but the glibc implementation makes
593 a fixed-size type, with
595 defined as 1024, and the
597 macros operating according to that limit.
598 To monitor file descriptors greater than 1023, use
602 The implementation of the
604 arguments as value-result arguments means that they must be
605 reinitialized on each call to
607 This design error is avoided by
609 which uses separate structure fields for the input and output of the call.
613 should check all specified file descriptors in the three file descriptor sets,
616 However, the current implementation ignores any file descriptor in
617 these sets that is greater than the maximum file descriptor number
618 that the process currently has open.
619 According to POSIX, any such file descriptor that is specified in one
620 of the sets should result in the error
623 Glibc 2.0 provided a version of
629 Starting with version 2.1, glibc provided an emulation of
631 that was implemented using
635 This implementation remained vulnerable to the very race condition that
637 was designed to prevent.
638 Modern versions of glibc use the (race-free)
640 system call on kernels where it is provided.
644 may report a socket file descriptor as "ready for reading", while
645 nevertheless a subsequent read blocks.
646 This could for example
647 happen when data has arrived but upon examination has wrong
648 checksum and is discarded.
649 There may be other circumstances
650 in which a file descriptor is spuriously reported as ready.
651 .\" Stevens discusses a case where accept can block after select
652 .\" returns successfully because of an intervening RST from the client.
653 Thus it may be safer to use
655 on sockets that should not block.
656 .\" Maybe the kernel should have returned EIO in such a situation?
662 if the call is interrupted by a signal handler (i.e., the
665 This is not permitted by POSIX.1.
668 system call has the same behavior,
669 but the glibc wrapper hides this behavior by internally copying the
671 to a local variable and passing that variable to the system call.
676 #include <sys/time.h>
677 #include <sys/types.h>
687 /* Watch stdin (fd 0) to see when it has input. */
692 /* Wait up to five seconds. */
697 retval = select(1, &rfds, NULL, NULL, &tv);
698 /* Don't rely on the value of tv now! */
703 printf("Data is available now.\\n");
704 /* FD_ISSET(0, &rfds) will be true. */
706 printf("No data within five seconds.\\n");
717 .BR restart_syscall (2),
724 For a tutorial with discussion and examples, see