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