]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/select.2
ffix
[thirdparty/man-pages.git] / man2 / select.2
CommitLineData
fea681da
MK
1.\" Hey Emacs! This file is -*- nroff -*- source.
2.\"
3.\" This manpage is copyright (C) 1992 Drew Eckhardt,
4.\" copyright (C) 1995 Michael Shields.
5.\"
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.
9.\"
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.
14.\"
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
21.\" professionally.
22.\"
23.\" Formatted or processed versions of this manual, if unaccompanied by
24.\" the source, must acknowledge the copyright and authors of this work.
25.\"
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
30.\"
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>
d02aa9bc
MK
35.\" 2005-03-11, mtk, modified pselect() text (it is now a system
36.\" call in 2.6.16.
fea681da 37.\"
d02aa9bc 38.TH SELECT 2 2006-03-11 "Linux 2.6.16" "Linux Programmer's Manual"
fea681da 39.SH NAME
35478399
MK
40select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
41synchronous I/O multiplexing
fea681da 42.SH SYNOPSIS
cc9befa9 43.nf
97c1eac8 44/* According to POSIX.1-2001 */
fea681da
MK
45.br
46.B #include <sys/select.h>
47.sp
48/* According to earlier standards */
49.br
50.B #include <sys/time.h>
51.br
52.B #include <sys/types.h>
53.br
54.B #include <unistd.h>
55.sp
6efed4df 56\fBint select(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
cc9befa9 57 fd_set *\fIexceptfds\fB, struct timeval *\fItimeout\fB);
fea681da 58.sp
071dbad9 59.BI "void FD_CLR(int " fd ", fd_set *" set );
fea681da 60.br
071dbad9 61.BI "int FD_ISSET(int " fd ", fd_set *" set );
fea681da 62.br
071dbad9 63.BI "void FD_SET(int " fd ", fd_set *" set );
fea681da 64.br
071dbad9 65.BI "void FD_ZERO(fd_set *" set );
9b813741
MK
66.sp
67.B #define _XOPEN_SOURCE 600
68.B #include <sys/select.h>
69.sp
6efed4df 70\fBint pselect(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB,
616c0fd3
MK
71 fd_set *\fIexceptfds\fB, const struct timespec *\fItimeout\fB,
72 const sigset_t *\fIsigmask\fB);
fea681da
MK
73.fi
74.SH DESCRIPTION
e511ffb6 75.BR select ()
fea681da 76and
e511ffb6 77.BR pselect ()
39179b3e 78allow a program to monitor multiple file descriptors,
5e01a1de
MK
79waiting until one or more of the file descriptors become "ready"
80for some class of I/O operation (e.g., input possible).
39179b3e 81A file descriptor is considered ready if it is possible to
5e01a1de
MK
82perform the corresponding I/O operation (e.g.,
83.BR read (2))
84without blocking.
fea681da 85.PP
5e01a1de
MK
86The operation of
87.BR select ()
88and
89.BR pselect ()
90is identical, with three differences:
fea681da
MK
91.TP
92(i)
e511ffb6 93.BR select ()
cc9befa9 94uses a timeout that is a
fea681da
MK
95.I struct timeval
96(with seconds and microseconds), while
e511ffb6 97.BR pselect ()
fea681da
MK
98uses a
99.I struct timespec
100(with seconds and nanoseconds).
101.TP
102(ii)
e511ffb6 103.BR select ()
cc9befa9 104may update the
fea681da 105.I timeout
cc9befa9 106argument to indicate how much time was left.
e511ffb6 107.BR pselect ()
cc9befa9 108does not change this argument.
fea681da
MK
109.TP
110(iii)
e511ffb6 111.BR select ()
cc9befa9 112has no
fea681da 113.I sigmask
cc9befa9 114argument, and behaves as
e511ffb6 115.BR pselect ()
fea681da
MK
116called with NULL
117.IR sigmask .
118.PP
22f348ca
MK
119Three independent sets of file descriptors are watched.
120Those listed in
fea681da
MK
121.I readfds
122will be watched to see if characters become
123available for reading (more precisely, to see if a read will not
e9496f74 124block; in particular, a file descriptor is also ready on end-of-file),
fea681da
MK
125those in
126.I writefds
127will be watched to see if a write will not block, and
128those in
129.I exceptfds
130will be watched for exceptions. On exit, the sets are modified in place
22f348ca
MK
131to indicate which file descriptors actually changed status.
132Each of the three file descriptor sets may be specified as NULL
133if no file descriptors are to be watched for the corresponding class
134of events.
fea681da
MK
135.PP
136Four macros are provided to manipulate the sets.
e511ffb6 137.BR FD_ZERO ()
21045df8 138clears a set.
e511ffb6 139.BR FD_SET ()
fea681da 140and
e511ffb6 141.BR FD_CLR ()
22f348ca 142respectively add and remove a given file descriptor from a set.
e511ffb6 143.BR FD_ISSET ()
22f348ca
MK
144tests to see if a file descriptor is part of the set;
145this is useful after
e511ffb6 146.BR select ()
fea681da
MK
147returns.
148.PP
6efed4df 149.I nfds
22f348ca 150is the highest-numbered file descriptor in any of the three sets, plus 1.
fea681da
MK
151.PP
152.I timeout
153is an upper bound on the amount of time elapsed before
e511ffb6 154.BR select ()
fea681da 155returns. It may be zero, causing
e511ffb6 156.BR select ()
fea681da
MK
157to return immediately. (This is useful for polling.) If
158.I timeout
159is NULL (no timeout),
e511ffb6 160.BR select ()
fea681da
MK
161can block indefinitely.
162.PP
163.I sigmask
164is a pointer to a signal mask (see
165.BR sigprocmask (2));
166if it is not NULL, then
e511ffb6 167.BR pselect ()
fea681da
MK
168first replaces the current signal mask by the one pointed to by
169.IR sigmask ,
170then does the `select' function, and then restores the original
cc9befa9 171signal mask.
fea681da 172.PP
d02aa9bc
MK
173Other than the difference in the precision of the
174.I timeout
175argument, the following
176.BR pselect ()
177call:
178.nf
179
6efed4df 180 ready = pselect(nfds, &readfds, &writefds, &exceptfds,
d02aa9bc
MK
181 timeout, &sigmask);
182
183.fi
184is equivalent to
185.I atomically
186executing the following calls:
187.nf
188
189 sigset_t origmask;
190
191 sigprocmask(SIG_SETMASK, &sigmask, &origmask);
192 ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
193 sigprocmask(SIG_SETMASK, &origmask, NULL);
194.fi
195.PP
196The reason that
e511ffb6 197.BR pselect ()
d02aa9bc
MK
198is needed is that if one wants to wait for either a signal
199or for a file descriptor to become ready, then
200an atomic test is needed to prevent race conditions.
201(Suppose the signal handler sets a global flag and
fea681da
MK
202returns. Then a test of this global flag followed by a call of
203.BR select ()
204could hang indefinitely if the signal arrived just after the test
d02aa9bc
MK
205but just before the call.
206By contrast,
e511ffb6 207.BR pselect ()
fea681da
MK
208allows one to first block signals, handle the signals that have come in,
209then call
210.BR pselect ()
211with the desired
212.IR sigmask ,
213avoiding the race.)
fea681da
MK
214.SS "The timeout"
215The time structures involved are defined in
216.I <sys/time.h>
217and look like
218
2f11acf5 219.in +0.25i
fea681da
MK
220.nf
221struct timeval {
222 long tv_sec; /* seconds */
223 long tv_usec; /* microseconds */
224};
225.fi
2f11acf5 226.in -0.25i
fea681da
MK
227
228and
229
2f11acf5 230.in +0.25i
fea681da
MK
231.nf
232struct timespec {
233 long tv_sec; /* seconds */
234 long tv_nsec; /* nanoseconds */
235};
236.fi
2f11acf5 237.in -0.25i
fea681da 238
97c1eac8 239(However, see below on the POSIX.1-2001 versions.)
fea681da
MK
240.PP
241Some code calls
e511ffb6 242.BR select ()
fea681da 243with all three sets empty,
79b8032e 244.I nfds
22f348ca 245zero, and a non-NULL
fea681da
MK
246.I timeout
247as a fairly portable way to sleep with subsecond precision.
248.PP
cc9befa9 249On Linux,
e511ffb6 250.BR select ()
fea681da
MK
251modifies
252.I timeout
253to reflect the amount of time not slept; most other implementations
77f00d75
MK
254do not do this.
255(POSIX.1-2001 permits either behaviour.)
256This causes problems both when Linux code which reads
fea681da
MK
257.I timeout
258is ported to other operating systems, and when code is ported to Linux
259that reuses a struct timeval for multiple
e511ffb6 260.BR select ()s
fea681da
MK
261in a loop without reinitializing it. Consider
262.I timeout
263to be undefined after
e511ffb6 264.BR select ()
fea681da
MK
265returns.
266.\" .PP - it is rumoured that:
267.\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
268.\" - it is certainly true that:
269.\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
270.SH "RETURN VALUE"
271On success,
e511ffb6 272.BR select ()
fea681da 273and
e511ffb6 274.BR pselect ()
22f348ca
MK
275return the number of file descriptors contained in the three returned
276descriptor sets (that is, the total number of bits that are set in
fea681da
MK
277.IR readfds ,
278.IR writefds ,
279.IR exceptfds )
280which may be zero if the timeout expires before anything interesting happens.
281On error, \-1 is returned, and
282.I errno
283is set appropriately; the sets and
284.I timeout
285become undefined, so do not
286rely on their contents after an error.
287.SH ERRORS
288.TP
289.B EBADF
290An invalid file descriptor was given in one of the sets.
6efed4df
MK
291(Perhaps a file descriptor that was already closed,
292or one on which an error has occurred.)
fea681da
MK
293.TP
294.B EINTR
6efed4df 295A signal was caught.
fea681da
MK
296.TP
297.B EINVAL
6efed4df 298.I nfds
fea681da
MK
299is negative or the value contained within
300.I timeout
301is invalid.
302.TP
303.B ENOMEM
6efed4df 304unable to allocate memory for internal tables.
fea681da
MK
305.SH EXAMPLE
306.nf
307#include <stdio.h>
308#include <sys/time.h>
309#include <sys/types.h>
310#include <unistd.h>
311
312int
313main(void) {
314 fd_set rfds;
315 struct timeval tv;
316 int retval;
317
318 /* Watch stdin (fd 0) to see when it has input. */
319 FD_ZERO(&rfds);
320 FD_SET(0, &rfds);
6efed4df 321
fea681da
MK
322 /* Wait up to five seconds. */
323 tv.tv_sec = 5;
324 tv.tv_usec = 0;
325
326 retval = select(1, &rfds, NULL, NULL, &tv);
327 /* Don't rely on the value of tv now! */
328
2bc2f479 329 if (retval == \-1)
fea681da
MK
330 perror("select()");
331 else if (retval)
332 printf("Data is available now.\\n");
333 /* FD_ISSET(0, &rfds) will be true. */
334 else
335 printf("No data within five seconds.\\n");
336
337 return 0;
338}
339.fi
340.SH "CONFORMING TO"
97c1eac8
MK
341.BR select ()
342conforms to POSIX.1-2001 and
cc9befa9
MK
3434.4BSD
344.RB ( select ()
345first appeared in 4.2BSD). Generally portable to/from
fea681da
MK
346non-BSD systems supporting clones of the BSD socket layer (including
347System V variants). However, note that the System V variant typically
348sets the timeout variable before exit, but the BSD variant does not.
349.PP
e511ffb6 350.BR pselect ()
97c1eac8
MK
351is defined in POSIX.1g, and in
352POSIX.1-2001.
fea681da 353.SH NOTES
22f348ca
MK
354An
355.I fd_set
356is a fixed size buffer.
357Executing
358.BR FD_CLR ()
359or
360.BR FD_SET ()
361with a value of
fea681da
MK
362.I fd
363that is negative or is equal to or larger than FD_SETSIZE will result
364in undefined behavior. Moreover, POSIX requires
365.I fd
366to be a valid file descriptor.
367
368Concerning the types involved, the classical situation is that
22f348ca
MK
369the two fields of a
370.I timeval
371structure are longs (as shown above),
372and the structure is defined in
fea681da 373.IR <sys/time.h> .
97c1eac8 374The POSIX.1-2001 situation is
fea681da
MK
375
376.RS
377.nf
378struct timeval {
379 time_t tv_sec; /* seconds */
380 suseconds_t tv_usec; /* microseconds */
381};
382.fi
383.RE
384
22f348ca 385where the structure is defined in
fea681da 386.I <sys/select.h>
d883fde0 387and the data types
22f348ca
MK
388.I time_t
389and
390.I suseconds_t
391are defined in
fea681da
MK
392.IR <sys/types.h> .
393.LP
394Concerning prototypes, the classical situation is that one should
395include
396.I <time.h>
397for
e511ffb6 398.BR select ().
97c1eac8 399The POSIX.1-2001 situation is that one should include
fea681da
MK
400.I <sys/select.h>
401for
e511ffb6 402.BR select ()
fea681da 403and
e511ffb6 404.BR pselect ().
fea681da
MK
405Libc4 and libc5 do not have a
406.I <sys/select.h>
407header; under glibc 2.0 and later this header exists.
408Under glibc 2.0 it unconditionally gives the wrong prototype for
e511ffb6 409.BR pselect (),
fea681da 410under glibc 2.1-2.2.1 it gives
e511ffb6 411.BR pselect ()
fea681da
MK
412when
413.B _GNU_SOURCE
414is defined, under glibc 2.2.2-2.2.4 it gives it when
415.B _XOPEN_SOURCE
416is defined and has a value of 600 or larger.
97c1eac8 417No doubt, since POSIX.1-2001, it should give the prototype by default.
cc9befa9
MK
418.SH VERSIONS
419.BR pselect ()
420was added to Linux in kernel 2.6.16.
cc9befa9
MK
421Prior to this,
422.BR pselect ()
423was emulated in glibc (but see BUGS).
77f00d75
MK
424.SH "LINUX NOTES"
425The Linux
426.BR pselect ()
2f11acf5
MK
427system call modifies its
428.I timeout
429argument.
430However, the glibc wrapper function hides this behaviour
77f00d75
MK
431by using a local variable for the timeout argument that
432is passed to the system call.
433Thus, the glibc
434.BR pselect ()
435function does not modify its timeout argument;
436this is the behaviour required by POSIX.1-2001.
fea681da 437.SH BUGS
cc9befa9 438Glibc 2.0 provided a version of
e511ffb6 439.BR pselect ()
cc9befa9
MK
440that did not take a
441.I sigmask
442argument.
443
444Since version 2.1, glibc has provided an emulation of
445.BR pselect ()
446that is implemented using
447.BR sigprocmask (2)
448and
449.BR select ().
450This implementation remains vulnerable to the very race condition that
451.BR pselect ()
452was designed to prevent.
453On systems that lack
454.BR pselect ()
455reliable (and more portable) signal trapping can be achieved
456using the self-pipe trick
457(where a signal handler writes a byte to a pipe whose other end
458is monitored by
459.BR select ()
460in the main program.)
fea681da
MK
461
462Under Linux,
e511ffb6 463.BR select ()
fea681da
MK
464may report a socket file descriptor as "ready for reading", while
465nevertheless a subsequent read blocks. This could for example
466happen when data has arrived but upon examination has wrong
2f11acf5
MK
467checksum and is discarded. There may be other circumstances
468in which a file descriptor is spuriously reported as ready.
fea681da
MK
469.\" Stevens discusses a case where accept can block after select
470.\" returns successfully because of an intervening RST from the client.
471Thus it may be safer to use O_NONBLOCK on sockets that should not block.
472.\" Maybe the kernel should have returned EIO in such a situation?
d02aa9bc
MK
473.\"
474.\" FIXME select() (and pselect()?) also modify the timeout
777f5a9e 475.\" on an EINTR error return; POSIX.1-2001 doesn't permit this.
fea681da
MK
476.SH "SEE ALSO"
477For a tutorial with discussion and examples, see
478.BR select_tut (2).
479.LP
480For vaguely related stuff, see
481.BR accept (2),
482.BR connect (2),
483.BR poll (2),
484.BR read (2),
485.BR recv (2),
486.BR send (2),
487.BR sigprocmask (2),
50e5322c 488.BR write (2),
6efed4df 489.BR epoll (7),
a8e7c990 490.BR feature_test_macros (7)