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