-.\" Hey Emacs! This file is -*- nroff -*- source.
-.\"
.\" This manpage is copyright (C) 1992 Drew Eckhardt,
.\" copyright (C) 1995 Michael Shields.
.\"
+.\" %%%LICENSE_START(VERBATIM)
.\" 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.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
+.\" %%%LICENSE_END
.\"
.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\" 2005-03-11, mtk, modified pselect() text (it is now a system
.\" call in 2.6.16.
.\"
-.TH SELECT 2 2012-05-02 "Linux" "Linux Programmer's Manual"
+.TH SELECT 2 2015-01-22 "Linux" "Linux Programmer's Manual"
.SH NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
synchronous I/O multiplexing
waiting until one or more of the file descriptors become "ready"
for some class of I/O operation (e.g., input possible).
A file descriptor is considered ready if it is possible to
-perform the corresponding I/O operation (e.g.,
-.BR read (2))
-without blocking.
+perform a corresponding I/O operation (e.g.,
+.BR read (2)
+without blocking, or a sufficiently small
+.BR write (2)).
.PP
The operation of
.BR select ()
and
.BR pselect ()
-is identical, with three differences:
+is identical, other than these three differences:
.TP
(i)
.BR select ()
block; in particular, a file descriptor is also ready on end-of-file),
those in
.I writefds
-will be watched to see if a write will not block, and
-those in
+will be watched to see if space is available for write (though a large
+write may still block), and those in
.I exceptfds
will be watched for exceptions.
On exit, the sets are modified in place
.PP
The
.I timeout
-argument specifies the minimum interval that
+argument specifies the interval that
.BR select ()
should block waiting for a file descriptor to become ready.
-(This interval will be rounded up to the system clock granularity,
+The call will block until either:
+.IP * 3
+a file descriptor becomes ready;
+.IP *
+the call is interrupted by a signal handler; or
+.IP *
+the timeout expires.
+.PP
+Note that the
+.I timeout
+interval will be rounded up to the system clock granularity,
and kernel scheduling delays mean that the blocking interval
-may overrun by a small amount.)
+may overrun by a small amount.
If both fields of the
.I timeval
structure are zero, then
sigset_t origmask;
- sigprocmask(SIG_SETMASK, &sigmask, &origmask);
+ pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
- sigprocmask(SIG_SETMASK, &origmask, NULL);
+ pthread_sigmask(SIG_SETMASK, &origmask, NULL);
.fi
.PP
The reason that
with the desired
.IR sigmask ,
avoiding the race.)
-.SS "The timeout"
+.SS The timeout
The time structures involved are defined in
.I <sys/time.h>
and look like
.\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
.\" - it is certainly true that:
.\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
-.SH "RETURN VALUE"
+.SH RETURN VALUE
On success,
.BR select ()
and
which may be zero if the timeout expires before anything interesting happens.
On error, \-1 is returned, and
.I errno
-is set appropriately; the sets and
+is set to indicate the error;
+the file descriptor sets are unmodified,
+and
.I timeout
-become undefined, so do not
-rely on their contents after an error.
+becomes undefined.
.SH ERRORS
.TP
.B EBADF
.TP
.B EINVAL
.I nfds
-is negative or the value contained within
+is negative or exceeds the
+.BR RLIMIT_NOFILE
+resource limit (see
+.BR getrlimit (2)).
+.TP
+.B EINVAL
+the value contained within
.I timeout
is invalid.
.TP
Prior to this,
.BR pselect ()
was emulated in glibc (but see BUGS).
-.SH "CONFORMING TO"
+.SH CONFORMING TO
.BR select ()
conforms to POSIX.1-2001 and
4.4BSD
first appeared in 4.2BSD).
Generally portable to/from
non-BSD systems supporting clones of the BSD socket layer (including
-System V variants).
-However, note that the System V variant typically
+System\ V variants).
+However, note that the System\ V variant typically
sets the timeout variable before exit, but the BSD variant does not.
.PP
.BR pselect ()
and
.BR pselect ().
-Libc4 and libc5 do not have a
+Under glibc 2.0,
.I <sys/select.h>
-header; under glibc 2.0 and later this header exists.
-Under glibc 2.0 it unconditionally gives the wrong prototype for
+gives the wrong prototype for
.BR pselect ().
-Under glibc 2.1 to 2.2.1 it gives
+Under glibc 2.1 to 2.2.1, it gives
.BR pselect ()
when
.B _GNU_SOURCE
is defined.
-Since glibc 2.2.2 the requirements are as shown in the SYNOPSIS.
-.SS "Linux Notes"
+Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS.
+.SS Multithreaded applications
+If a file descriptor being monitored by
+.BR select ()
+is closed in another thread, the result is unspecified.
+On some UNIX systems,
+.BR select ()
+unblocks and returns, with an indication that the file descriptor is ready
+(a subsequent I/O operation will likely fail with an error,
+unless another the file descriptor reopened between the time
+.BR select ()
+returned and the I/O operations was performed).
+On Linux (and some other systems),
+closing the file descriptor in another thread has no effect on
+.BR select ().
+In summary, any application that relies on a particular behavior
+in this scenario must be considered buggy.
+.\"
+.SS C library/kernel ABI differences
The
.BR pselect ()
interface described in this page is implemented by glibc.
The underlying Linux system call is named
.BR pselect6 ().
-This system call has somewhat different behavior from the gibc
+This system call has somewhat different behavior from the glibc
wrapper function.
The Linux
this is the behavior required by POSIX.1-2001.
The final argument of the
-.BR pselect6()
+.BR pselect6 ()
system call is not a
.I "sigset_t\ *"
pointer, but is instead a structure of the form:
exit(EXIT_SUCCESS);
}
.fi
-.SH "SEE ALSO"
-For a tutorial with discussion and examples, see
-.BR select_tut (2).
-.LP
-For vaguely related stuff, see
+.SH SEE ALSO
.BR accept (2),
.BR connect (2),
.BR poll (2),
.BR read (2),
.BR recv (2),
+.BR restart_syscall (2),
.BR send (2),
.BR sigprocmask (2),
.BR write (2),
.BR epoll (7),
.BR time (7)
+
+For a tutorial with discussion and examples, see
+.BR select_tut (2).
+.SH COLOPHON
+This page is part of release 3.79 of the Linux
+.I man-pages
+project.
+A description of the project,
+information about reporting bugs,
+and the latest version of this page,
+can be found at
+\%http://www.kernel.org/doc/man\-pages/.