]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Hey Emacs! This file is -*- nroff -*- source. |
2 | .\" | |
3 | .\" This manpage is copyright (C) 1992 Drew Eckhardt, | |
4 | .\" copyright (C) 1995 Michael Shields. | |
5 | .\" | |
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. | |
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 | .\" |
cc4615cc | 38 | .TH SELECT 2 2007-07-26 "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 () | |
97 | is identical, with 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 MK |
159 | .PP |
160 | .I timeout | |
161 | is an upper bound on the amount of time elapsed before | |
e511ffb6 | 162 | .BR select () |
c13182ef MK |
163 | returns. |
164 | It may be zero, causing | |
e511ffb6 | 165 | .BR select () |
c13182ef MK |
166 | to return immediately. |
167 | (This is useful for polling.) | |
168 | If | |
fea681da MK |
169 | .I timeout |
170 | is NULL (no timeout), | |
e511ffb6 | 171 | .BR select () |
fea681da MK |
172 | can block indefinitely. |
173 | .PP | |
174 | .I sigmask | |
175 | is a pointer to a signal mask (see | |
176 | .BR sigprocmask (2)); | |
177 | if it is not NULL, then | |
e511ffb6 | 178 | .BR pselect () |
fea681da MK |
179 | first replaces the current signal mask by the one pointed to by |
180 | .IR sigmask , | |
2d986c92 | 181 | then does the "select" function, and then restores the original |
cc9befa9 | 182 | signal mask. |
fea681da | 183 | .PP |
d02aa9bc MK |
184 | Other than the difference in the precision of the |
185 | .I timeout | |
c13182ef | 186 | argument, the following |
d02aa9bc MK |
187 | .BR pselect () |
188 | call: | |
189 | .nf | |
190 | ||
c13182ef | 191 | ready = pselect(nfds, &readfds, &writefds, &exceptfds, |
d02aa9bc MK |
192 | timeout, &sigmask); |
193 | ||
194 | .fi | |
195 | is equivalent to | |
196 | .I atomically | |
197 | executing the following calls: | |
198 | .nf | |
199 | ||
200 | sigset_t origmask; | |
201 | ||
202 | sigprocmask(SIG_SETMASK, &sigmask, &origmask); | |
203 | ready = select(nfds, &readfds, &writefds, &exceptfds, timeout); | |
204 | sigprocmask(SIG_SETMASK, &origmask, NULL); | |
205 | .fi | |
206 | .PP | |
c13182ef | 207 | The reason that |
e511ffb6 | 208 | .BR pselect () |
d02aa9bc MK |
209 | is needed is that if one wants to wait for either a signal |
210 | or for a file descriptor to become ready, then | |
c13182ef | 211 | an atomic test is needed to prevent race conditions. |
d02aa9bc | 212 | (Suppose the signal handler sets a global flag and |
c13182ef MK |
213 | returns. |
214 | Then a test of this global flag followed by a call of | |
fea681da MK |
215 | .BR select () |
216 | could hang indefinitely if the signal arrived just after the test | |
c13182ef | 217 | but just before the call. |
d02aa9bc | 218 | By contrast, |
e511ffb6 | 219 | .BR pselect () |
fea681da MK |
220 | allows one to first block signals, handle the signals that have come in, |
221 | then call | |
222 | .BR pselect () | |
223 | with the desired | |
224 | .IR sigmask , | |
225 | avoiding the race.) | |
fea681da MK |
226 | .SS "The timeout" |
227 | The time structures involved are defined in | |
228 | .I <sys/time.h> | |
229 | and look like | |
230 | ||
2f11acf5 | 231 | .in +0.25i |
fea681da | 232 | .nf |
c13182ef | 233 | struct timeval { |
fea681da MK |
234 | long tv_sec; /* seconds */ |
235 | long tv_usec; /* microseconds */ | |
236 | }; | |
237 | .fi | |
2f11acf5 | 238 | .in -0.25i |
fea681da MK |
239 | |
240 | and | |
241 | ||
2f11acf5 | 242 | .in +0.25i |
fea681da MK |
243 | .nf |
244 | struct timespec { | |
245 | long tv_sec; /* seconds */ | |
246 | long tv_nsec; /* nanoseconds */ | |
247 | }; | |
248 | .fi | |
2f11acf5 | 249 | .in -0.25i |
fea681da | 250 | |
97c1eac8 | 251 | (However, see below on the POSIX.1-2001 versions.) |
fea681da MK |
252 | .PP |
253 | Some code calls | |
e511ffb6 | 254 | .BR select () |
fea681da | 255 | with all three sets empty, |
79b8032e | 256 | .I nfds |
22f348ca | 257 | zero, and a non-NULL |
fea681da MK |
258 | .I timeout |
259 | as a fairly portable way to sleep with subsecond precision. | |
260 | .PP | |
c13182ef | 261 | On Linux, |
e511ffb6 | 262 | .BR select () |
fea681da MK |
263 | modifies |
264 | .I timeout | |
265 | to reflect the amount of time not slept; most other implementations | |
77f00d75 | 266 | do not do this. |
d9bfdb9c | 267 | (POSIX.1-2001 permits either behavior.) |
77f00d75 | 268 | This causes problems both when Linux code which reads |
fea681da MK |
269 | .I timeout |
270 | is ported to other operating systems, and when code is ported to Linux | |
0c2ec4f1 | 271 | that reuses a \fIstruct timeval\fP for multiple |
e511ffb6 | 272 | .BR select ()s |
c13182ef MK |
273 | in a loop without reinitializing it. |
274 | Consider | |
fea681da MK |
275 | .I timeout |
276 | to be undefined after | |
e511ffb6 | 277 | .BR select () |
fea681da | 278 | returns. |
d9bfdb9c | 279 | .\" .PP - it is rumored that: |
fea681da MK |
280 | .\" On BSD, when a timeout occurs, the file descriptor bits are not changed. |
281 | .\" - it is certainly true that: | |
282 | .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout. | |
283 | .SH "RETURN VALUE" | |
284 | On success, | |
e511ffb6 | 285 | .BR select () |
fea681da | 286 | and |
e511ffb6 | 287 | .BR pselect () |
22f348ca MK |
288 | return the number of file descriptors contained in the three returned |
289 | descriptor sets (that is, the total number of bits that are set in | |
fea681da MK |
290 | .IR readfds , |
291 | .IR writefds , | |
292 | .IR exceptfds ) | |
293 | which may be zero if the timeout expires before anything interesting happens. | |
294 | On error, \-1 is returned, and | |
295 | .I errno | |
296 | is set appropriately; the sets and | |
297 | .I timeout | |
298 | become undefined, so do not | |
299 | rely on their contents after an error. | |
300 | .SH ERRORS | |
301 | .TP | |
302 | .B EBADF | |
303 | An invalid file descriptor was given in one of the sets. | |
c13182ef | 304 | (Perhaps a file descriptor that was already closed, |
6efed4df | 305 | or one on which an error has occurred.) |
fea681da MK |
306 | .TP |
307 | .B EINTR | |
6efed4df | 308 | A signal was caught. |
fea681da MK |
309 | .TP |
310 | .B EINVAL | |
6efed4df | 311 | .I nfds |
fea681da MK |
312 | is negative or the value contained within |
313 | .I timeout | |
314 | is invalid. | |
315 | .TP | |
316 | .B ENOMEM | |
6efed4df | 317 | unable to allocate memory for internal tables. |
a1d5f77c MK |
318 | .SH VERSIONS |
319 | .BR pselect () | |
320 | was added to Linux in kernel 2.6.16. | |
321 | Prior to this, | |
322 | .BR pselect () | |
323 | was emulated in glibc (but see BUGS). | |
fea681da | 324 | .SH "CONFORMING TO" |
c13182ef MK |
325 | .BR select () |
326 | conforms to POSIX.1-2001 and | |
327 | 4.4BSD | |
cc9befa9 | 328 | .RB ( select () |
c13182ef MK |
329 | first appeared in 4.2BSD). |
330 | Generally portable to/from | |
fea681da | 331 | non-BSD systems supporting clones of the BSD socket layer (including |
c13182ef MK |
332 | System V variants). |
333 | However, note that the System V variant typically | |
fea681da MK |
334 | sets the timeout variable before exit, but the BSD variant does not. |
335 | .PP | |
e511ffb6 | 336 | .BR pselect () |
97c1eac8 MK |
337 | is defined in POSIX.1g, and in |
338 | POSIX.1-2001. | |
fea681da | 339 | .SH NOTES |
c13182ef MK |
340 | An |
341 | .I fd_set | |
342 | is a fixed size buffer. | |
343 | Executing | |
22f348ca | 344 | .BR FD_CLR () |
c13182ef | 345 | or |
22f348ca MK |
346 | .BR FD_SET () |
347 | with a value of | |
fea681da | 348 | .I fd |
682edefb MK |
349 | that is negative or is equal to or larger than |
350 | .B FD_SETSIZE | |
351 | will result | |
c13182ef MK |
352 | in undefined behavior. |
353 | Moreover, POSIX requires | |
fea681da MK |
354 | .I fd |
355 | to be a valid file descriptor. | |
356 | ||
357 | Concerning the types involved, the classical situation is that | |
22f348ca | 358 | the two fields of a |
c13182ef | 359 | .I timeval |
e3e25559 MK |
360 | structure are typed as |
361 | .I long | |
362 | (as shown above), and the structure is defined in | |
fea681da | 363 | .IR <sys/time.h> . |
97c1eac8 | 364 | The POSIX.1-2001 situation is |
fea681da MK |
365 | |
366 | .RS | |
367 | .nf | |
368 | struct timeval { | |
369 | time_t tv_sec; /* seconds */ | |
370 | suseconds_t tv_usec; /* microseconds */ | |
371 | }; | |
372 | .fi | |
373 | .RE | |
374 | ||
22f348ca | 375 | where the structure is defined in |
fea681da | 376 | .I <sys/select.h> |
c13182ef MK |
377 | and the data types |
378 | .I time_t | |
379 | and | |
380 | .I suseconds_t | |
22f348ca | 381 | are defined in |
fea681da MK |
382 | .IR <sys/types.h> . |
383 | .LP | |
384 | Concerning prototypes, the classical situation is that one should | |
385 | include | |
386 | .I <time.h> | |
387 | for | |
e511ffb6 | 388 | .BR select (). |
97c1eac8 | 389 | The POSIX.1-2001 situation is that one should include |
fea681da MK |
390 | .I <sys/select.h> |
391 | for | |
e511ffb6 | 392 | .BR select () |
fea681da | 393 | and |
e511ffb6 | 394 | .BR pselect (). |
fea681da MK |
395 | Libc4 and libc5 do not have a |
396 | .I <sys/select.h> | |
397 | header; under glibc 2.0 and later this header exists. | |
398 | Under glibc 2.0 it unconditionally gives the wrong prototype for | |
e511ffb6 | 399 | .BR pselect (), |
fea681da | 400 | under glibc 2.1-2.2.1 it gives |
e511ffb6 | 401 | .BR pselect () |
fea681da MK |
402 | when |
403 | .B _GNU_SOURCE | |
404 | is defined, under glibc 2.2.2-2.2.4 it gives it when | |
405 | .B _XOPEN_SOURCE | |
406 | is defined and has a value of 600 or larger. | |
97c1eac8 | 407 | No doubt, since POSIX.1-2001, it should give the prototype by default. |
4fb31341 | 408 | .SS "Linux Notes" |
77f00d75 MK |
409 | The Linux |
410 | .BR pselect () | |
c13182ef MK |
411 | system call modifies its |
412 | .I timeout | |
2f11acf5 | 413 | argument. |
d9bfdb9c | 414 | However, the glibc wrapper function hides this behavior |
77f00d75 MK |
415 | by using a local variable for the timeout argument that |
416 | is passed to the system call. | |
c13182ef | 417 | Thus, the glibc |
77f00d75 MK |
418 | .BR pselect () |
419 | function does not modify its timeout argument; | |
d9bfdb9c | 420 | this is the behavior required by POSIX.1-2001. |
fea681da | 421 | .SH BUGS |
cc9befa9 | 422 | Glibc 2.0 provided a version of |
e511ffb6 | 423 | .BR pselect () |
c13182ef MK |
424 | that did not take a |
425 | .I sigmask | |
cc9befa9 MK |
426 | argument. |
427 | ||
428 | Since version 2.1, glibc has provided an emulation of | |
c13182ef MK |
429 | .BR pselect () |
430 | that is implemented using | |
cc9befa9 MK |
431 | .BR sigprocmask (2) |
432 | and | |
433 | .BR select (). | |
434 | This implementation remains vulnerable to the very race condition that | |
435 | .BR pselect () | |
436 | was designed to prevent. | |
437 | On systems that lack | |
438 | .BR pselect () | |
c13182ef MK |
439 | reliable (and more portable) signal trapping can be achieved |
440 | using the self-pipe trick | |
cc9befa9 | 441 | (where a signal handler writes a byte to a pipe whose other end |
c13182ef | 442 | is monitored by |
cc9befa9 MK |
443 | .BR select () |
444 | in the main program.) | |
fea681da MK |
445 | |
446 | Under Linux, | |
e511ffb6 | 447 | .BR select () |
fea681da | 448 | may report a socket file descriptor as "ready for reading", while |
c13182ef MK |
449 | nevertheless a subsequent read blocks. |
450 | This could for example | |
fea681da | 451 | happen when data has arrived but upon examination has wrong |
c13182ef MK |
452 | checksum and is discarded. |
453 | There may be other circumstances | |
2f11acf5 | 454 | in which a file descriptor is spuriously reported as ready. |
fea681da MK |
455 | .\" Stevens discusses a case where accept can block after select |
456 | .\" returns successfully because of an intervening RST from the client. | |
682edefb MK |
457 | Thus it may be safer to use |
458 | .B O_NONBLOCK | |
459 | on sockets that should not block. | |
fea681da | 460 | .\" Maybe the kernel should have returned EIO in such a situation? |
5766b196 MK |
461 | |
462 | On Linux, | |
463 | .BR select () | |
464 | also modifies | |
465 | .I timeout | |
466 | if the call is interrupted by a signal handler (i.e., the | |
467 | .B EINTR | |
468 | error return). | |
469 | This is not permitted by POSIX.1-2001. | |
470 | The Linux | |
2777b1ca | 471 | .BR pselect () |
5766b196 MK |
472 | system call has the same behavior, |
473 | but the glibc wrapper hides this behavior by internally copying the | |
474 | .I timeout | |
475 | to a local variable and passing that variable to the system call. | |
2b2581ee MK |
476 | .SH EXAMPLE |
477 | .nf | |
478 | #include <stdio.h> | |
af9c7ff2 | 479 | #include <stdlib.h> |
2b2581ee MK |
480 | #include <sys/time.h> |
481 | #include <sys/types.h> | |
482 | #include <unistd.h> | |
483 | ||
484 | int | |
485 | main(void) | |
486 | { | |
487 | fd_set rfds; | |
488 | struct timeval tv; | |
489 | int retval; | |
490 | ||
491 | /* Watch stdin (fd 0) to see when it has input. */ | |
492 | FD_ZERO(&rfds); | |
493 | FD_SET(0, &rfds); | |
494 | ||
495 | /* Wait up to five seconds. */ | |
496 | tv.tv_sec = 5; | |
497 | tv.tv_usec = 0; | |
498 | ||
499 | retval = select(1, &rfds, NULL, NULL, &tv); | |
500 | /* Don't rely on the value of tv now! */ | |
501 | ||
502 | if (retval == \-1) | |
503 | perror("select()"); | |
504 | else if (retval) | |
505 | printf("Data is available now.\\n"); | |
506 | /* FD_ISSET(0, &rfds) will be true. */ | |
507 | else | |
508 | printf("No data within five seconds.\\n"); | |
509 | ||
510 | exit(EXIT_SUCCESS); | |
511 | } | |
512 | .fi | |
fea681da MK |
513 | .SH "SEE ALSO" |
514 | For a tutorial with discussion and examples, see | |
515 | .BR select_tut (2). | |
516 | .LP | |
517 | For vaguely related stuff, see | |
518 | .BR accept (2), | |
519 | .BR connect (2), | |
520 | .BR poll (2), | |
521 | .BR read (2), | |
522 | .BR recv (2), | |
523 | .BR send (2), | |
524 | .BR sigprocmask (2), | |
50e5322c | 525 | .BR write (2), |
cc4615cc | 526 | .BR epoll (7) |