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