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