man-pages/man2/select.2

503 lines
13 KiB
Groff
Raw Normal View History

2004-11-03 13:51:07 +00:00
.\" Hey Emacs! This file is -*- nroff -*- source.
.\"
.\" This manpage is copyright (C) 1992 Drew Eckhardt,
.\" copyright (C) 1995 Michael Shields.
.\"
.\" 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.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
2004-11-03 13:51:07 +00:00
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
2004-11-03 13:51:07 +00:00
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
.\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
.\" Sun Feb 11 14:07:00 MET 1996 Martin Schulze <joey@linux.de>
.\" * layout slightly modified
.\"
.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
.\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb
.\" Modified Thu Feb 9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb
.\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net>
2006-03-12 03:09:50 +00:00
.\" 2005-03-11, mtk, modified pselect() text (it is now a system
.\" call in 2.6.16.
2004-11-03 13:51:07 +00:00
.\"
2007-05-30 05:36:26 +00:00
.TH SELECT 2 2006-03-11 "Linux" "Linux Programmer's Manual"
2004-11-03 13:51:07 +00:00
.SH NAME
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
2006-12-27 03:54:41 +00:00
synchronous I/O multiplexing
2004-11-03 13:51:07 +00:00
.SH SYNOPSIS
.nf
2006-08-03 13:57:17 +00:00
/* According to POSIX.1-2001 */
2004-11-03 13:51:07 +00:00
.br
.B #include <sys/select.h>
.sp
/* According to earlier standards */
.br
.B #include <sys/time.h>
.br
.B #include <sys/types.h>
.br
.B #include <unistd.h>
.sp
\fBint select(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
fd_set *\fIexceptfds\fB, struct timeval *\fItimeout\fB);
2004-11-03 13:51:07 +00:00
.sp
.BI "void FD_CLR(int " fd ", fd_set *" set );
2004-11-03 13:51:07 +00:00
.br
2007-04-03 14:04:54 +00:00
.BI "int FD_ISSET(int " fd ", fd_set *" set );
2004-11-03 13:51:07 +00:00
.br
.BI "void FD_SET(int " fd ", fd_set *" set );
2004-11-03 13:51:07 +00:00
.br
.BI "void FD_ZERO(fd_set *" set );
.sp
.B #define _XOPEN_SOURCE 600
.B #include <sys/select.h>
.sp
\fBint pselect(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
fd_set *\fIexceptfds\fB, const struct timespec *\fItimeout\fB,
2006-05-13 04:57:40 +00:00
const sigset_t *\fIsigmask\fB);
2004-11-03 13:51:07 +00:00
.fi
.SH DESCRIPTION
.BR select ()
2004-11-03 13:51:07 +00:00
and
.BR pselect ()
2006-04-21 18:17:12 +00:00
allow a program to monitor multiple file descriptors,
waiting until one or more of the file descriptors become "ready"
for some class of I/O operation (e.g., input possible).
2006-04-21 18:17:12 +00:00
A file descriptor is considered ready if it is possible to
perform the corresponding I/O operation (e.g.,
.BR read (2))
without blocking.
2004-11-03 13:51:07 +00:00
.PP
The operation of
.BR select ()
and
.BR pselect ()
is identical, with three differences:
2004-11-03 13:51:07 +00:00
.TP
(i)
.BR select ()
uses a timeout that is a
2004-11-03 13:51:07 +00:00
.I struct timeval
(with seconds and microseconds), while
.BR pselect ()
2004-11-03 13:51:07 +00:00
uses a
.I struct timespec
(with seconds and nanoseconds).
.TP
(ii)
.BR select ()
may update the
2004-11-03 13:51:07 +00:00
.I timeout
argument to indicate how much time was left.
.BR pselect ()
does not change this argument.
2004-11-03 13:51:07 +00:00
.TP
(iii)
.BR select ()
has no
2004-11-03 13:51:07 +00:00
.I sigmask
argument, and behaves as
.BR pselect ()
2004-11-03 13:51:07 +00:00
called with NULL
.IR sigmask .
.PP
Three independent sets of file descriptors are watched.
Those listed in
2004-11-03 13:51:07 +00:00
.I readfds
will be watched to see if characters become
available for reading (more precisely, to see if a read will not
2005-07-06 06:54:27 +00:00
block; in particular, a file descriptor is also ready on end-of-file),
2004-11-03 13:51:07 +00:00
those in
.I writefds
will be watched to see if a write will not block, and
those in
.I exceptfds
will be watched for exceptions.
On exit, the sets are modified in place
to indicate which file descriptors actually changed status.
Each of the three file descriptor sets may be specified as NULL
if no file descriptors are to be watched for the corresponding class
of events.
2004-11-03 13:51:07 +00:00
.PP
Four macros are provided to manipulate the sets.
.BR FD_ZERO ()
2006-02-02 18:18:55 +00:00
clears a set.
.BR FD_SET ()
2004-11-03 13:51:07 +00:00
and
.BR FD_CLR ()
respectively add and remove a given file descriptor from a set.
.BR FD_ISSET ()
tests to see if a file descriptor is part of the set;
this is useful after
.BR select ()
2004-11-03 13:51:07 +00:00
returns.
.PP
2006-05-13 06:08:41 +00:00
.I nfds
is the highest-numbered file descriptor in any of the three sets, plus 1.
2004-11-03 13:51:07 +00:00
.PP
.I timeout
is an upper bound on the amount of time elapsed before
.BR select ()
returns.
It may be zero, causing
.BR select ()
to return immediately.
(This is useful for polling.)
If
2004-11-03 13:51:07 +00:00
.I timeout
is NULL (no timeout),
.BR select ()
2004-11-03 13:51:07 +00:00
can block indefinitely.
.PP
.I sigmask
is a pointer to a signal mask (see
.BR sigprocmask (2));
if it is not NULL, then
.BR pselect ()
2004-11-03 13:51:07 +00:00
first replaces the current signal mask by the one pointed to by
.IR sigmask ,
then does the `select' function, and then restores the original
signal mask.
2004-11-03 13:51:07 +00:00
.PP
2006-03-12 03:09:50 +00:00
Other than the difference in the precision of the
.I timeout
argument, the following
2006-03-12 03:09:50 +00:00
.BR pselect ()
call:
.nf
ready = pselect(nfds, &readfds, &writefds, &exceptfds,
2006-03-12 03:09:50 +00:00
timeout, &sigmask);
.fi
is equivalent to
.I atomically
executing the following calls:
.nf
sigset_t origmask;
sigprocmask(SIG_SETMASK, &sigmask, &origmask);
ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
sigprocmask(SIG_SETMASK, &origmask, NULL);
.fi
.PP
The reason that
.BR pselect ()
2006-03-12 03:09:50 +00:00
is needed is that if one wants to wait for either a signal
or for a file descriptor to become ready, then
an atomic test is needed to prevent race conditions.
2006-03-12 03:09:50 +00:00
(Suppose the signal handler sets a global flag and
returns.
Then a test of this global flag followed by a call of
2004-11-03 13:51:07 +00:00
.BR select ()
could hang indefinitely if the signal arrived just after the test
but just before the call.
2006-03-12 03:09:50 +00:00
By contrast,
.BR pselect ()
2004-11-03 13:51:07 +00:00
allows one to first block signals, handle the signals that have come in,
then call
.BR pselect ()
with the desired
.IR sigmask ,
avoiding the race.)
.SS "The timeout"
The time structures involved are defined in
.I <sys/time.h>
and look like
2006-03-15 10:27:09 +00:00
.in +0.25i
2004-11-03 13:51:07 +00:00
.nf
struct timeval {
2004-11-03 13:51:07 +00:00
long tv_sec; /* seconds */
long tv_usec; /* microseconds */
};
.fi
2006-03-15 10:27:09 +00:00
.in -0.25i
2004-11-03 13:51:07 +00:00
and
2006-03-15 10:27:09 +00:00
.in +0.25i
2004-11-03 13:51:07 +00:00
.nf
struct timespec {
long tv_sec; /* seconds */
long tv_nsec; /* nanoseconds */
};
.fi
2006-03-15 10:27:09 +00:00
.in -0.25i
2004-11-03 13:51:07 +00:00
2006-08-03 13:57:17 +00:00
(However, see below on the POSIX.1-2001 versions.)
2004-11-03 13:51:07 +00:00
.PP
Some code calls
.BR select ()
2004-11-03 13:51:07 +00:00
with all three sets empty,
2006-12-27 02:48:57 +00:00
.I nfds
zero, and a non-NULL
2004-11-03 13:51:07 +00:00
.I timeout
as a fairly portable way to sleep with subsecond precision.
.PP
On Linux,
.BR select ()
2004-11-03 13:51:07 +00:00
modifies
.I timeout
to reflect the amount of time not slept; most other implementations
2006-03-07 22:38:11 +00:00
do not do this.
(POSIX.1-2001 permits either behavior.)
2006-03-07 22:38:11 +00:00
This causes problems both when Linux code which reads
2004-11-03 13:51:07 +00:00
.I timeout
is ported to other operating systems, and when code is ported to Linux
2007-06-13 22:13:46 +00:00
that reuses a \fIstruct timeval\fP for multiple
.BR select ()s
in a loop without reinitializing it.
Consider
2004-11-03 13:51:07 +00:00
.I timeout
to be undefined after
.BR select ()
2004-11-03 13:51:07 +00:00
returns.
.\" .PP - it is rumored that:
2004-11-03 13:51:07 +00:00
.\" 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"
On success,
.BR select ()
2004-11-03 13:51:07 +00:00
and
.BR pselect ()
return the number of file descriptors contained in the three returned
descriptor sets (that is, the total number of bits that are set in
2004-11-03 13:51:07 +00:00
.IR readfds ,
.IR writefds ,
.IR exceptfds )
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
.I timeout
become undefined, so do not
rely on their contents after an error.
.SH ERRORS
.TP
.B EBADF
An invalid file descriptor was given in one of the sets.
(Perhaps a file descriptor that was already closed,
2006-05-13 06:08:41 +00:00
or one on which an error has occurred.)
2004-11-03 13:51:07 +00:00
.TP
.B EINTR
2006-05-13 06:08:41 +00:00
A signal was caught.
2004-11-03 13:51:07 +00:00
.TP
.B EINVAL
2006-05-13 06:08:41 +00:00
.I nfds
2004-11-03 13:51:07 +00:00
is negative or the value contained within
.I timeout
is invalid.
.TP
.B ENOMEM
2006-05-13 06:08:41 +00:00
unable to allocate memory for internal tables.
.SH VERSIONS
.BR pselect ()
was added to Linux in kernel 2.6.16.
Prior to this,
.BR pselect ()
was emulated in glibc (but see BUGS).
2004-11-03 13:51:07 +00:00
.SH "CONFORMING TO"
.BR select ()
conforms to POSIX.1-2001 and
4.4BSD
.RB ( select ()
first appeared in 4.2BSD).
Generally portable to/from
2004-11-03 13:51:07 +00:00
non-BSD systems supporting clones of the BSD socket layer (including
System V variants).
However, note that the System V variant typically
2004-11-03 13:51:07 +00:00
sets the timeout variable before exit, but the BSD variant does not.
.PP
.BR pselect ()
2006-08-03 13:57:17 +00:00
is defined in POSIX.1g, and in
POSIX.1-2001.
2004-11-03 13:51:07 +00:00
.SH NOTES
An
.I fd_set
is a fixed size buffer.
Executing
.BR FD_CLR ()
or
.BR FD_SET ()
with a value of
2004-11-03 13:51:07 +00:00
.I fd
that is negative or is equal to or larger than FD_SETSIZE will result
in undefined behavior.
Moreover, POSIX requires
2004-11-03 13:51:07 +00:00
.I fd
to be a valid file descriptor.
Concerning the types involved, the classical situation is that
the two fields of a
.I timeval
structure are longs (as shown above),
and the structure is defined in
2004-11-03 13:51:07 +00:00
.IR <sys/time.h> .
2006-08-03 13:57:17 +00:00
The POSIX.1-2001 situation is
2004-11-03 13:51:07 +00:00
.RS
.nf
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
.fi
.RE
where the structure is defined in
2004-11-03 13:51:07 +00:00
.I <sys/select.h>
and the data types
.I time_t
and
.I suseconds_t
are defined in
2004-11-03 13:51:07 +00:00
.IR <sys/types.h> .
.LP
Concerning prototypes, the classical situation is that one should
include
.I <time.h>
for
.BR select ().
2006-08-03 13:57:17 +00:00
The POSIX.1-2001 situation is that one should include
2004-11-03 13:51:07 +00:00
.I <sys/select.h>
for
.BR select ()
2004-11-03 13:51:07 +00:00
and
.BR pselect ().
2004-11-03 13:51:07 +00:00
Libc4 and libc5 do not have a
.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
.BR pselect (),
2004-11-03 13:51:07 +00:00
under glibc 2.1-2.2.1 it gives
.BR pselect ()
2004-11-03 13:51:07 +00:00
when
.B _GNU_SOURCE
is defined, under glibc 2.2.2-2.2.4 it gives it when
.B _XOPEN_SOURCE
is defined and has a value of 600 or larger.
2006-08-03 13:57:17 +00:00
No doubt, since POSIX.1-2001, it should give the prototype by default.
.SS "Linux Notes"
2006-03-07 22:38:11 +00:00
The Linux
.BR pselect ()
system call modifies its
.I timeout
2006-03-15 10:27:09 +00:00
argument.
However, the glibc wrapper function hides this behavior
2006-03-07 22:38:11 +00:00
by using a local variable for the timeout argument that
is passed to the system call.
Thus, the glibc
2006-03-07 22:38:11 +00:00
.BR pselect ()
function does not modify its timeout argument;
this is the behavior required by POSIX.1-2001.
2004-11-03 13:51:07 +00:00
.SH BUGS
Glibc 2.0 provided a version of
.BR pselect ()
that did not take a
.I sigmask
argument.
Since version 2.1, glibc has provided an emulation of
.BR pselect ()
that is implemented using
.BR sigprocmask (2)
and
.BR select ().
This implementation remains vulnerable to the very race condition that
.BR pselect ()
was designed to prevent.
On systems that lack
.BR pselect ()
reliable (and more portable) signal trapping can be achieved
using the self-pipe trick
(where a signal handler writes a byte to a pipe whose other end
is monitored by
.BR select ()
in the main program.)
2004-11-03 13:51:07 +00:00
Under Linux,
.BR select ()
2004-11-03 13:51:07 +00:00
may report a socket file descriptor as "ready for reading", while
nevertheless a subsequent read blocks.
This could for example
2004-11-03 13:51:07 +00:00
happen when data has arrived but upon examination has wrong
checksum and is discarded.
There may be other circumstances
2006-03-15 10:27:09 +00:00
in which a file descriptor is spuriously reported as ready.
2004-11-03 13:51:07 +00:00
.\" Stevens discusses a case where accept can block after select
.\" returns successfully because of an intervening RST from the client.
Thus it may be safer to use O_NONBLOCK on sockets that should not block.
.\" Maybe the kernel should have returned EIO in such a situation?
2006-03-12 03:09:50 +00:00
.\"
.\" FIXME select() (and pselect()?) also modify the timeout
2006-03-20 04:46:28 +00:00
.\" on an EINTR error return; POSIX.1-2001 doesn't permit this.
.SH EXAMPLE
.nf
#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
int
main(void)
{
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(0, &rfds);
/* Wait up to five seconds. */
tv.tv_sec = 5;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == \-1)
perror("select()");
else if (retval)
printf("Data is available now.\\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within five seconds.\\n");
exit(EXIT_SUCCESS);
}
.fi
2004-11-03 13:51:07 +00:00
.SH "SEE ALSO"
For a tutorial with discussion and examples, see
.BR select_tut (2).
.LP
For vaguely related stuff, see
.BR accept (2),
.BR connect (2),
.BR poll (2),
.BR read (2),
.BR recv (2),
.BR send (2),
.BR sigprocmask (2),
2006-04-21 06:49:34 +00:00
.BR write (2),
2006-05-13 06:08:41 +00:00
.BR epoll (7),
2006-05-15 09:13:10 +00:00
.BR feature_test_macros (7)