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