]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/select.2
sched_setattr.2: tfix
[thirdparty/man-pages.git] / man2 / select.2
CommitLineData
fea681da 1.\" This manpage is copyright (C) 1992 Drew Eckhardt,
095d8388
MK
2.\" copyright (C) 1995 Michael Shields,
3.\" copyright (C) 2001 Paul Sheer,
b397824f 4.\" copyright (C) 2006, 2019 Michael Kerrisk <mtk.manpages@gmail.com>
fea681da 5.\"
93015253 6.\" %%%LICENSE_START(VERBATIM)
fea681da
MK
7.\" Permission is granted to make and distribute verbatim copies of this
8.\" manual provided the copyright notice and this permission notice are
9.\" preserved on all copies.
10.\"
11.\" Permission is granted to copy and distribute modified versions of this
12.\" manual under the conditions for verbatim copying, provided that the
13.\" entire resulting derived work is distributed under the terms of a
14.\" permission notice identical to this one.
c13182ef 15.\"
fea681da
MK
16.\" Since the Linux kernel and libraries are constantly changing, this
17.\" manual page may be incorrect or out-of-date. The author(s) assume no
18.\" responsibility for errors or omissions, or for damages resulting from
19.\" the use of the information contained herein. The author(s) may not
20.\" have taken the same level of care in the production of this manual,
21.\" which is licensed free of charge, as they might when working
22.\" professionally.
c13182ef 23.\"
fea681da
MK
24.\" Formatted or processed versions of this manual, if unaccompanied by
25.\" the source, must acknowledge the copyright and authors of this work.
4b72fb64 26.\" %%%LICENSE_END
fea681da
MK
27.\"
28.\" Modified 1993-07-24 by Rik Faith <faith@cs.unc.edu>
29.\" Modified 1995-05-18 by Jim Van Zandt <jrv@vanzandt.mv.com>
30.\" Sun Feb 11 14:07:00 MET 1996 Martin Schulze <joey@linux.de>
31.\" * layout slightly modified
32.\"
33.\" Modified Mon Oct 21 23:05:29 EDT 1996 by Eric S. Raymond <esr@thyrsus.com>
34.\" Modified Thu Feb 24 01:41:09 CET 2000 by aeb
35.\" Modified Thu Feb 9 22:32:09 CET 2001 by bert hubert <ahu@ds9a.nl>, aeb
36.\" Modified Mon Nov 11 14:35:00 PST 2002 by Ben Woodard <ben@zork.net>
d02aa9bc
MK
37.\" 2005-03-11, mtk, modified pselect() text (it is now a system
38.\" call in 2.6.16.
fea681da 39.\"
e8426ca2 40.TH SELECT 2 2020-04-11 "Linux" "Linux Programmer's Manual"
fea681da 41.SH NAME
c13182ef 42select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \-
35478399 43synchronous I/O multiplexing
fea681da 44.SH SYNOPSIS
cc9befa9 45.nf
fea681da 46.B #include <sys/select.h>
dbfe9c70 47.PP
cc4615cc
MK
48.BI "int select(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
49.BI " fd_set *" exceptfds ", struct timeval *" timeout );
68e4db0a 50.PP
071dbad9 51.BI "void FD_CLR(int " fd ", fd_set *" set );
521bf584 52.BI "int FD_ISSET(int " fd ", fd_set *" set );
071dbad9 53.BI "void FD_SET(int " fd ", fd_set *" set );
071dbad9 54.BI "void FD_ZERO(fd_set *" set );
68e4db0a 55.PP
cc4615cc
MK
56.BI "int pselect(int " nfds ", fd_set *" readfds ", fd_set *" writefds ,
57.BI " fd_set *" exceptfds ", const struct timespec *" timeout ,
58.BI " const sigset_t *" sigmask );
fea681da 59.fi
68e4db0a 60.PP
cc4615cc
MK
61.in -4n
62Feature Test Macro Requirements for glibc (see
63.BR feature_test_macros (7)):
64.in
68e4db0a 65.PP
cc4615cc 66.BR pselect ():
a446ac0c 67_POSIX_C_SOURCE\ >=\ 200112L
fea681da 68.SH DESCRIPTION
e511ffb6 69.BR select ()
01901530 70allows a program to monitor multiple file descriptors,
5e01a1de
MK
71waiting until one or more of the file descriptors become "ready"
72for some class of I/O operation (e.g., input possible).
39179b3e 73A file descriptor is considered ready if it is possible to
d2e7d1bb 74perform a corresponding I/O operation (e.g.,
43e3c551
MK
75.BR read (2),
76or a sufficiently small
77.BR write (2))
78without blocking.
fea681da 79.PP
6b6e9185 80.BR select ()
6c345305
MK
81can monitor only file descriptors numbers that are less than
82.BR FD_SETSIZE ;
83.BR poll (2)
095d8388
MK
84and
85.BR epoll (7)
86do not have this limitation.
6c345305 87See BUGS.
095d8388
MK
88.\"
89.SS File descriptor sets
90The principal arguments of
91.BR select ()
92are three "sets" of file descriptors (declared with the type
93.IR fd_set ),
94which allow the caller to wait for three classes of events
95on the specified set of file descriptors.
96Each of the
97.I fd_set
98arguments may be specified as NULL if no file descriptors are
99to be watched for the corresponding class of events.
6b6e9185 100.PP
095d8388 101.BR "Note well" :
1eda1a3a 102Upon return, each of the file descriptor sets is modified in place
095d8388
MK
103to indicate which file descriptors are currently "ready".
104Thus, if using
cd2ea4b4 105.BR select ()
095d8388 106within a loop, the sets \fImust be reinitialized\fP before each call.
095d8388
MK
107The implementation of the
108.I fd_set
109arguments as value-result arguments is a design error that is avoided in
110.BR poll (2)
111and
112.BR epoll (7).
fea681da 113.PP
095d8388
MK
114The contents of a file descriptor set can be manipulated
115using the following macros:
116.TP
e511ffb6 117.BR FD_ZERO ()
095d8388
MK
118This macro clears (removes all file descriptors from)
119.IR set .
120It should be employed as the first step in initializing a file descriptor set.
121.TP
e511ffb6 122.BR FD_SET ()
095d8388
MK
123This macro adds the file descriptor
124.I fd
125to
126.IR set .
95a85f14
MK
127Adding a file descriptor that is already present in the set is a no-op,
128and does not produce an error.
095d8388 129.TP
e511ffb6 130.BR FD_CLR ()
095d8388
MK
131This macro removes the file descriptor
132.I fd
133from
134.IR set .
95a85f14
MK
135Removing a file descriptor that is not present in the set is a no-op,
136and does not produce an error.
095d8388 137.TP
e511ffb6 138.BR FD_ISSET ()
e511ffb6 139.BR select ()
095d8388
MK
140modifies the contents of the sets according to the rules
141described below.
142After calling
143.BR select (),
144the
145.BR FD_ISSET ()
146macro
147can be used to test if a file descriptor is still present in a set.
148.BR FD_ISSET ()
149returns nonzero if the file descriptor
150.I fd
151is present in
152.IR set ,
153and zero if it is not.
154.\"
155.SS Arguments
156The arguments of
157.BR select ()
158are as follows:
159.TP
160.I readfds
161The file descriptors in this set are watched to see if they are
162ready for reading.
163A file descriptor is ready for reading if a read operation will not
164block; in particular, a file descriptor is also ready on end-of-file.
165.IP
166After
167.BR select ()
168has returned, \fIreadfds\fP will be
169cleared of all file descriptors except for those that are ready for reading.
170.TP
171.I writefds
172The file descriptors in this set are watched to see if they are
173ready for writing.
174A file descriptor is ready for writing if a write operation will not block.
175However, even if a file descriptor indicates as writable,
176a large write may still block.
177.IP
178After
179.BR select ()
180has returned, \fIwritefds\fP will be
181cleared of all file descriptors except for those that are ready for writing.
182.TP
183.I exceptfds
184The file descriptors in this set are watched for "exceptional conditions".
185For examples of some exceptional conditions, see the discussion of
186.B POLLPRI
187in
188.BR poll (2).
189.IP
190After
191.BR select ()
192has returned,
193\fIexceptfds\fP will be cleared of all file descriptors except for those
194for which an exceptional condition has occurred.
195.TP
6efed4df 196.I nfds
095d8388 197This argument should be set to the highest-numbered file descriptor in any
8b58a9d4
MK
198of the three sets, plus 1.
199The indicated file descriptors in each set are checked, up to this limit
200(but see BUGS).
095d8388
MK
201.TP
202.I timeout
8c121f40 203The
fea681da 204.I timeout
8678102a 205argument is a
b8f8864d
MK
206.I timeval
207structure (shown below) that specifies the interval that
e511ffb6 208.BR select ()
8c121f40 209should block waiting for a file descriptor to become ready.
40df3d00 210The call will block until either:
095d8388 211.RS
4628e3ec 212.IP \(bu 2
40df3d00 213a file descriptor becomes ready;
4628e3ec 214.IP \(bu
40df3d00 215the call is interrupted by a signal handler; or
4628e3ec 216.IP \(bu
71e7d7f1 217the timeout expires.
095d8388
MK
218.RE
219.IP
40df3d00
MK
220Note that the
221.I timeout
222interval will be rounded up to the system clock granularity,
8c121f40 223and kernel scheduling delays mean that the blocking interval
073f0240 224may overrun by a small amount.
095d8388 225.IP
485eb4ad
MK
226If both fields of the
227.I timeval
c808bb16 228structure are zero, then
e511ffb6 229.BR select ()
485eb4ad 230returns immediately.
c13182ef 231(This is useful for polling.)
095d8388 232.IP
c13182ef 233If
fea681da 234.I timeout
095d8388 235is specified as NULL,
e511ffb6 236.BR select ()
095d8388 237blocks indefinitely waiting for a file descriptor to become ready.
01901530
MK
238.\"
239.SS pselect()
240.PP
241The
242.BR pselect ()
243system call allows an application to safely wait until either
244a file descriptor becomes ready or until a signal is caught.
245.PP
246The operation of
247.BR select ()
248and
249.BR pselect ()
250is identical, other than these three differences:
4628e3ec 251.IP \(bu 2
01901530
MK
252.BR select ()
253uses a timeout that is a
254.I struct timeval
255(with seconds and microseconds), while
256.BR pselect ()
257uses a
258.I struct timespec
259(with seconds and nanoseconds).
4628e3ec 260.IP \(bu
01901530
MK
261.BR select ()
262may update the
263.I timeout
264argument to indicate how much time was left.
265.BR pselect ()
266does not change this argument.
4628e3ec 267.IP \(bu
01901530
MK
268.BR select ()
269has no
270.I sigmask
271argument, and behaves as
272.BR pselect ()
273called with NULL
274.IR sigmask .
fea681da
MK
275.PP
276.I sigmask
277is a pointer to a signal mask (see
278.BR sigprocmask (2));
279if it is not NULL, then
e511ffb6 280.BR pselect ()
fea681da
MK
281first replaces the current signal mask by the one pointed to by
282.IR sigmask ,
2d986c92 283then does the "select" function, and then restores the original
cc9befa9 284signal mask.
095d8388
MK
285(If
286.I sigmask
287is NULL,
288the signal mask is not modified during the
289.BR pselect ()
290call.)
fea681da 291.PP
d02aa9bc
MK
292Other than the difference in the precision of the
293.I timeout
c13182ef 294argument, the following
d02aa9bc
MK
295.BR pselect ()
296call:
408731d4
MK
297.PP
298.in +4n
299.EX
300ready = pselect(nfds, &readfds, &writefds, &exceptfds,
301 timeout, &sigmask);
302.EE
303.in
304.PP
d02aa9bc
MK
305is equivalent to
306.I atomically
307executing the following calls:
408731d4
MK
308.PP
309.in +4n
310.EX
311sigset_t origmask;
d02aa9bc 312
408731d4
MK
313pthread_sigmask(SIG_SETMASK, &sigmask, &origmask);
314ready = select(nfds, &readfds, &writefds, &exceptfds, timeout);
315pthread_sigmask(SIG_SETMASK, &origmask, NULL);
316.EE
317.in
318.PP
d02aa9bc 319.PP
c13182ef 320The reason that
e511ffb6 321.BR pselect ()
d02aa9bc
MK
322is needed is that if one wants to wait for either a signal
323or for a file descriptor to become ready, then
c13182ef 324an atomic test is needed to prevent race conditions.
d02aa9bc 325(Suppose the signal handler sets a global flag and
c13182ef
MK
326returns.
327Then a test of this global flag followed by a call of
fea681da
MK
328.BR select ()
329could hang indefinitely if the signal arrived just after the test
c13182ef 330but just before the call.
d02aa9bc 331By contrast,
e511ffb6 332.BR pselect ()
fea681da
MK
333allows one to first block signals, handle the signals that have come in,
334then call
335.BR pselect ()
336with the desired
337.IR sigmask ,
338avoiding the race.)
73d8cece 339.SS The timeout
f0e902c3
MK
340The
341.I timeout
342argument for
343.BR select ()
344is a structure of the following type:
efeece04 345.PP
088a639b 346.in +4n
b8302363 347.EX
c13182ef 348struct timeval {
f0e902c3
MK
349 time_t tv_sec; /* seconds */
350 suseconds_t tv_usec; /* microseconds */
fea681da 351};
b8302363 352.EE
a08ea57c 353.in
efeece04 354.PP
f0e902c3
MK
355The corresponding argument for
356.BR pselect ()
357has the following type:
efeece04 358.PP
088a639b 359.in +4n
b8302363 360.EX
fea681da 361struct timespec {
f0e902c3
MK
362 time_t tv_sec; /* seconds */
363 long tv_nsec; /* nanoseconds */
fea681da 364};
b8302363 365.EE
a08ea57c 366.in
efeece04 367.PP
c13182ef 368On Linux,
e511ffb6 369.BR select ()
fea681da
MK
370modifies
371.I timeout
372to reflect the amount of time not slept; most other implementations
77f00d75 373do not do this.
e9419385 374(POSIX.1 permits either behavior.)
77f00d75 375This causes problems both when Linux code which reads
fea681da
MK
376.I timeout
377is ported to other operating systems, and when code is ported to Linux
0c2ec4f1 378that reuses a \fIstruct timeval\fP for multiple
e511ffb6 379.BR select ()s
c13182ef
MK
380in a loop without reinitializing it.
381Consider
fea681da
MK
382.I timeout
383to be undefined after
e511ffb6 384.BR select ()
fea681da 385returns.
d9bfdb9c 386.\" .PP - it is rumored that:
fea681da
MK
387.\" On BSD, when a timeout occurs, the file descriptor bits are not changed.
388.\" - it is certainly true that:
389.\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout.
47297adb 390.SH RETURN VALUE
fea681da 391On success,
e511ffb6 392.BR select ()
fea681da 393and
e511ffb6 394.BR pselect ()
22f348ca
MK
395return the number of file descriptors contained in the three returned
396descriptor sets (that is, the total number of bits that are set in
fea681da
MK
397.IR readfds ,
398.IR writefds ,
f7cd2865
MK
399.IR exceptfds ).
400The return value may be zero if the timeout expired before any
401file descriptors became ready.
e5704b1a 402.PP
fea681da
MK
403On error, \-1 is returned, and
404.I errno
8dc33675
MK
405is set to indicate the error;
406the file descriptor sets are unmodified,
407and
fea681da 408.I timeout
8dc33675 409becomes undefined.
fea681da
MK
410.SH ERRORS
411.TP
412.B EBADF
413An invalid file descriptor was given in one of the sets.
c13182ef 414(Perhaps a file descriptor that was already closed,
6efed4df 415or one on which an error has occurred.)
8b58a9d4 416However, see BUGS.
fea681da
MK
417.TP
418.B EINTR
01538d0d
MK
419A signal was caught; see
420.BR signal (7).
fea681da
MK
421.TP
422.B EINVAL
6efed4df 423.I nfds
b9ebc9b7
MK
424is negative or exceeds the
425.BR RLIMIT_NOFILE
426resource limit (see
427.BR getrlimit (2)).
428.TP
429.B EINVAL
02959ce2 430The value contained within
fea681da
MK
431.I timeout
432is invalid.
433.TP
434.B ENOMEM
02959ce2 435Unable to allocate memory for internal tables.
a1d5f77c
MK
436.SH VERSIONS
437.BR pselect ()
438was added to Linux in kernel 2.6.16.
439Prior to this,
440.BR pselect ()
441was emulated in glibc (but see BUGS).
47297adb 442.SH CONFORMING TO
c13182ef 443.BR select ()
e9419385 444conforms to POSIX.1-2001, POSIX.1-2008, and
c13182ef 4454.4BSD
cc9befa9 446.RB ( select ()
c13182ef
MK
447first appeared in 4.2BSD).
448Generally portable to/from
fea681da 449non-BSD systems supporting clones of the BSD socket layer (including
efbfd7ec
MK
450System\ V variants).
451However, note that the System\ V variant typically
1eda1a3a 452sets the timeout variable before returning, but the BSD variant does not.
fea681da 453.PP
e511ffb6 454.BR pselect ()
97c1eac8 455is defined in POSIX.1g, and in
e9419385 456POSIX.1-2001 and POSIX.1-2008.
fea681da 457.SH NOTES
c13182ef
MK
458An
459.I fd_set
460is a fixed size buffer.
461Executing
22f348ca 462.BR FD_CLR ()
c13182ef 463or
22f348ca
MK
464.BR FD_SET ()
465with a value of
fea681da 466.I fd
682edefb
MK
467that is negative or is equal to or larger than
468.B FD_SETSIZE
469will result
c13182ef
MK
470in undefined behavior.
471Moreover, POSIX requires
fea681da
MK
472.I fd
473to be a valid file descriptor.
efeece04 474.PP
e795580f
MK
475The operation of
476.BR select ()
477and
478.BR pselect ()
479is not affected by the
480.BR O_NONBLOCK
481flag.
482.PP
20cc8fa8
MK
483On some other UNIX systems,
484.\" Darwin, according to a report by Jeremy Sequoia, relayed by Josh Triplett
485.BR select ()
486can fail with the error
487.B EAGAIN
488if the system fails to allocate kernel-internal resources, rather than
489.B ENOMEM
490as Linux does.
491POSIX specifies this error for
492.BR poll (2),
493but not for
494.BR select ().
495Portable programs may wish to check for
496.B EAGAIN
497and loop, just as with
498.BR EINTR .
d221e421
MK
499.\"
500.SS The self-pipe trick
3116bbe0
MK
501On systems that lack
502.BR pselect (),
503reliable (and more portable) signal trapping can be achieved
504using the self-pipe trick.
505In this technique,
506a signal handler writes a byte to a pipe whose other end
507is monitored by
508.BR select ()
509in the main program.
510(To avoid possibly blocking when writing to a pipe that may be full
511or reading from a pipe that may be empty,
512nonblocking I/O is used when reading from and writing to the pipe.)
ab264bff 513.\"
bd89babb
MK
514.SS Emulating usleep(3)
515.PP
516Before the advent of
517.BR usleep (3),
518some code employed a call to
519.BR select ()
520with all three sets empty,
521.I nfds
522zero, and a non-NULL
523.I timeout
524as a fairly portable way to sleep with subsecond precision.
525.\"
ab264bff
MK
526.SS Correspondence between select() and poll() notifications
527Within the Linux kernel source,
528.\" fs/select.c
529we find the following definitions which show the correspondence
530between the readable, writable, and exceptional condition notifications of
531.BR select ()
532and the event notifications provided by
533.BR poll (2)
23c167c6
MK
534and
535.BR epoll (7):
efeece04 536.PP
ab264bff 537.in +4n
b8302363 538.EX
23c167c6
MK
539#define POLLIN_SET (EPOLLRDNORM | EPOLLRDBAND | EPOLLIN |
540 EPOLLHUP | EPOLLERR)
ab264bff 541 /* Ready for reading */
23c167c6
MK
542#define POLLOUT_SET (EPOLLWRBAND | EPOLLWRNORM | EPOLLOUT |
543 EPOLLERR)
ab264bff 544 /* Ready for writing */
23c167c6 545#define POLLEX_SET (EPOLLPRI)
ab264bff 546 /* Exceptional condition */
b8302363 547.EE
e646a1ba 548.in
ab264bff 549.\"
10ed041e
MK
550.SS Multithreaded applications
551If a file descriptor being monitored by
552.BR select ()
553is closed in another thread, the result is unspecified.
554On some UNIX systems,
555.BR select ()
556unblocks and returns, with an indication that the file descriptor is ready
557(a subsequent I/O operation will likely fail with an error,
be914947 558unless another process reopens file descriptor between the time
10ed041e 559.BR select ()
be914947 560returned and the I/O operation is performed).
10ed041e
MK
561On Linux (and some other systems),
562closing the file descriptor in another thread has no effect on
563.BR select ().
564In summary, any application that relies on a particular behavior
565in this scenario must be considered buggy.
17ec2d27 566.\"
0722a578 567.SS C library/kernel differences
6c345305
MK
568The Linux kernel allows file descriptor sets of arbitrary size,
569determining the length of the sets to be checked from the value of
570.IR nfds .
571However, in the glibc implementation, the
572.IR fd_set
573type is fixed in size.
574See also BUGS.
efeece04 575.PP
a16eec1e 576The
77f00d75 577.BR pselect ()
a16eec1e
MK
578interface described in this page is implemented by glibc.
579The underlying Linux system call is named
580.BR pselect6 ().
a59e64be 581This system call has somewhat different behavior from the glibc
a16eec1e 582wrapper function.
efeece04 583.PP
a16eec1e
MK
584The Linux
585.BR pselect6 ()
c13182ef
MK
586system call modifies its
587.I timeout
2f11acf5 588argument.
d9bfdb9c 589However, the glibc wrapper function hides this behavior
77f00d75
MK
590by using a local variable for the timeout argument that
591is passed to the system call.
c13182ef 592Thus, the glibc
77f00d75 593.BR pselect ()
d53de2a7
MK
594function does not modify its
595.I timeout
596argument;
d9bfdb9c 597this is the behavior required by POSIX.1-2001.
efeece04 598.PP
a16eec1e 599The final argument of the
02ace852 600.BR pselect6 ()
0ab8aeec 601system call is not a
a16eec1e
MK
602.I "sigset_t\ *"
603pointer, but is instead a structure of the form:
408731d4 604.PP
a16eec1e 605.in +4
408731d4 606.EX
a16eec1e 607struct {
1a116ea0
MK
608 const kernel_sigset_t *ss; /* Pointer to signal set */
609 size_t ss_len; /* Size (in bytes) of object
610 pointed to by 'ss' */
a16eec1e 611};
e646a1ba 612.EE
a16eec1e 613.in
e646a1ba 614.PP
a16eec1e
MK
615This allows the system call to obtain both
616a pointer to the signal set and its size,
617while allowing for the fact that most architectures
f8dcca84
MK
618support a maximum of 6 arguments to a system call.
619See
620.BR sigprocmask (2)
f25ea51b
N
621for a discussion of the difference between the kernel and libc
622notion of the signal set.
0a4d109d
MK
623.\"
624.SS Historical glibc details
625.PP
626Glibc 2.0 provided an incorrect version of
627.BR pselect ()
628that did not take a
629.I sigmask
630argument.
631.PP
632In glibc versions 2.1 to 2.2.1,
633one must define
634.B _GNU_SOURCE
635in order to obtain the declaration of
636.BR pselect ()
637from
638.IR <sys/select.h> .
fea681da 639.SH BUGS
6c345305
MK
640POSIX allows an implementation to define an upper limit,
641advertised via the constant
642.BR FD_SETSIZE ,
643on the range of file descriptors that can be specified
644in a file descriptor set.
645The Linux kernel imposes no fixed limit, but the glibc implementation makes
646.IR fd_set
647a fixed-size type, with
648.BR FD_SETSIZE
649defined as 1024, and the
650.BR FD_* ()
651macros operating according to that limit.
652To monitor file descriptors greater than 1023, use
653.BR poll (2)
095d8388
MK
654or
655.BR epoll (7)
6c345305 656instead.
efeece04 657.PP
8b58a9d4
MK
658According to POSIX,
659.BR select ()
660should check all specified file descriptors in the three file descriptor sets,
661up to the limit
662.IR nfds\-1 .
663However, the current implementation ignores any file descriptor in
664these sets that is greater than the maximum file descriptor number
665that the process currently has open.
666According to POSIX, any such file descriptor that is specified in one
667of the sets should result in the error
668.BR EBADF .
efeece04 669.PP
3fa2e4b9 670Starting with version 2.1, glibc provided an emulation of
c13182ef 671.BR pselect ()
3fa2e4b9 672that was implemented using
cc9befa9
MK
673.BR sigprocmask (2)
674and
675.BR select ().
3fa2e4b9 676This implementation remained vulnerable to the very race condition that
cc9befa9
MK
677.BR pselect ()
678was designed to prevent.
3fa2e4b9
MK
679Modern versions of glibc use the (race-free)
680.BR pselect ()
681system call on kernels where it is provided.
efeece04 682.PP
a84ed700 683On Linux,
e511ffb6 684.BR select ()
fea681da 685may report a socket file descriptor as "ready for reading", while
c13182ef
MK
686nevertheless a subsequent read blocks.
687This could for example
a84ed700 688happen when data has arrived but upon examination has the wrong
c13182ef
MK
689checksum and is discarded.
690There may be other circumstances
2f11acf5 691in which a file descriptor is spuriously reported as ready.
fea681da
MK
692.\" Stevens discusses a case where accept can block after select
693.\" returns successfully because of an intervening RST from the client.
682edefb
MK
694Thus it may be safer to use
695.B O_NONBLOCK
696on sockets that should not block.
fea681da 697.\" Maybe the kernel should have returned EIO in such a situation?
efeece04 698.PP
5766b196
MK
699On Linux,
700.BR select ()
701also modifies
702.I timeout
703if the call is interrupted by a signal handler (i.e., the
704.B EINTR
705error return).
e9419385 706This is not permitted by POSIX.1.
5766b196 707The Linux
2777b1ca 708.BR pselect ()
5766b196
MK
709system call has the same behavior,
710but the glibc wrapper hides this behavior by internally copying the
711.I timeout
712to a local variable and passing that variable to the system call.
2b2581ee 713.SH EXAMPLE
408731d4 714.EX
2b2581ee 715#include <stdio.h>
af9c7ff2 716#include <stdlib.h>
a63fef43 717#include <sys/select.h>
2b2581ee
MK
718
719int
720main(void)
721{
722 fd_set rfds;
723 struct timeval tv;
724 int retval;
725
726 /* Watch stdin (fd 0) to see when it has input. */
887f19e8 727
2b2581ee
MK
728 FD_ZERO(&rfds);
729 FD_SET(0, &rfds);
730
731 /* Wait up to five seconds. */
887f19e8 732
2b2581ee
MK
733 tv.tv_sec = 5;
734 tv.tv_usec = 0;
735
736 retval = select(1, &rfds, NULL, NULL, &tv);
737 /* Don't rely on the value of tv now! */
738
739 if (retval == \-1)
740 perror("select()");
741 else if (retval)
d1a71985 742 printf("Data is available now.\en");
2b2581ee
MK
743 /* FD_ISSET(0, &rfds) will be true. */
744 else
d1a71985 745 printf("No data within five seconds.\en");
2b2581ee
MK
746
747 exit(EXIT_SUCCESS);
748}
408731d4 749.EE
47297adb 750.SH SEE ALSO
fea681da
MK
751.BR accept (2),
752.BR connect (2),
753.BR poll (2),
754.BR read (2),
755.BR recv (2),
25a7bfe6 756.BR restart_syscall (2),
fea681da
MK
757.BR send (2),
758.BR sigprocmask (2),
50e5322c 759.BR write (2),
1d7c4d16
MK
760.BR epoll (7),
761.BR time (7)
efeece04 762.PP
173fe7e7
DP
763For a tutorial with discussion and examples, see
764.BR select_tut (2).