]>
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. | |
14 | .\" | |
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. | |
22 | .\" | |
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 | .\" |
d02aa9bc | 38 | .TH SELECT 2 2006-03-11 "Linux 2.6.16" "Linux Programmer's Manual" |
fea681da | 39 | .SH NAME |
35478399 MK |
40 | select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO \- |
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 | |
6efed4df | 56 | \fBint select(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB, |
cc9befa9 | 57 | fd_set *\fIexceptfds\fB, struct timeval *\fItimeout\fB); |
fea681da | 58 | .sp |
071dbad9 | 59 | .BI "void FD_CLR(int " fd ", fd_set *" set ); |
fea681da | 60 | .br |
071dbad9 | 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 MK |
66 | .sp |
67 | .B #define _XOPEN_SOURCE 600 | |
68 | .B #include <sys/select.h> | |
69 | .sp | |
6efed4df | 70 | \fBint pselect(int \fInfds\fB, fd_set *\fIreadfds\fB, fd_set *\fIwritefds\fB, |
616c0fd3 MK |
71 | fd_set *\fIexceptfds\fB, const struct timespec *\fItimeout\fB, |
72 | const sigset_t *\fIsigmask\fB); | |
fea681da MK |
73 | .fi |
74 | .SH DESCRIPTION | |
e511ffb6 | 75 | .BR select () |
fea681da | 76 | and |
e511ffb6 | 77 | .BR pselect () |
39179b3e | 78 | allow a program to monitor multiple file descriptors, |
5e01a1de MK |
79 | waiting until one or more of the file descriptors become "ready" |
80 | for some class of I/O operation (e.g., input possible). | |
39179b3e | 81 | A file descriptor is considered ready if it is possible to |
5e01a1de MK |
82 | perform the corresponding I/O operation (e.g., |
83 | .BR read (2)) | |
84 | without blocking. | |
fea681da | 85 | .PP |
5e01a1de MK |
86 | The operation of |
87 | .BR select () | |
88 | and | |
89 | .BR pselect () | |
90 | is identical, with three differences: | |
fea681da MK |
91 | .TP |
92 | (i) | |
e511ffb6 | 93 | .BR select () |
cc9befa9 | 94 | uses a timeout that is a |
fea681da MK |
95 | .I struct timeval |
96 | (with seconds and microseconds), while | |
e511ffb6 | 97 | .BR pselect () |
fea681da MK |
98 | uses a |
99 | .I struct timespec | |
100 | (with seconds and nanoseconds). | |
101 | .TP | |
102 | (ii) | |
e511ffb6 | 103 | .BR select () |
cc9befa9 | 104 | may update the |
fea681da | 105 | .I timeout |
cc9befa9 | 106 | argument to indicate how much time was left. |
e511ffb6 | 107 | .BR pselect () |
cc9befa9 | 108 | does not change this argument. |
fea681da MK |
109 | .TP |
110 | (iii) | |
e511ffb6 | 111 | .BR select () |
cc9befa9 | 112 | has no |
fea681da | 113 | .I sigmask |
cc9befa9 | 114 | argument, and behaves as |
e511ffb6 | 115 | .BR pselect () |
fea681da MK |
116 | called with NULL |
117 | .IR sigmask . | |
118 | .PP | |
22f348ca MK |
119 | Three independent sets of file descriptors are watched. |
120 | Those listed in | |
fea681da MK |
121 | .I readfds |
122 | will be watched to see if characters become | |
123 | available for reading (more precisely, to see if a read will not | |
e9496f74 | 124 | block; in particular, a file descriptor is also ready on end-of-file), |
fea681da MK |
125 | those in |
126 | .I writefds | |
127 | will be watched to see if a write will not block, and | |
128 | those in | |
129 | .I exceptfds | |
130 | will be watched for exceptions. On exit, the sets are modified in place | |
22f348ca MK |
131 | to indicate which file descriptors actually changed status. |
132 | Each of the three file descriptor sets may be specified as NULL | |
133 | if no file descriptors are to be watched for the corresponding class | |
134 | of events. | |
fea681da MK |
135 | .PP |
136 | Four macros are provided to manipulate the sets. | |
e511ffb6 | 137 | .BR FD_ZERO () |
21045df8 | 138 | clears a set. |
e511ffb6 | 139 | .BR FD_SET () |
fea681da | 140 | and |
e511ffb6 | 141 | .BR FD_CLR () |
22f348ca | 142 | respectively add and remove a given file descriptor from a set. |
e511ffb6 | 143 | .BR FD_ISSET () |
22f348ca MK |
144 | tests to see if a file descriptor is part of the set; |
145 | this is useful after | |
e511ffb6 | 146 | .BR select () |
fea681da MK |
147 | returns. |
148 | .PP | |
6efed4df | 149 | .I nfds |
22f348ca | 150 | is the highest-numbered file descriptor in any of the three sets, plus 1. |
fea681da MK |
151 | .PP |
152 | .I timeout | |
153 | is an upper bound on the amount of time elapsed before | |
e511ffb6 | 154 | .BR select () |
fea681da | 155 | returns. It may be zero, causing |
e511ffb6 | 156 | .BR select () |
fea681da MK |
157 | to return immediately. (This is useful for polling.) If |
158 | .I timeout | |
159 | is NULL (no timeout), | |
e511ffb6 | 160 | .BR select () |
fea681da MK |
161 | can block indefinitely. |
162 | .PP | |
163 | .I sigmask | |
164 | is a pointer to a signal mask (see | |
165 | .BR sigprocmask (2)); | |
166 | if it is not NULL, then | |
e511ffb6 | 167 | .BR pselect () |
fea681da MK |
168 | first replaces the current signal mask by the one pointed to by |
169 | .IR sigmask , | |
170 | then does the `select' function, and then restores the original | |
cc9befa9 | 171 | signal mask. |
fea681da | 172 | .PP |
d02aa9bc MK |
173 | Other than the difference in the precision of the |
174 | .I timeout | |
175 | argument, the following | |
176 | .BR pselect () | |
177 | call: | |
178 | .nf | |
179 | ||
6efed4df | 180 | ready = pselect(nfds, &readfds, &writefds, &exceptfds, |
d02aa9bc MK |
181 | timeout, &sigmask); |
182 | ||
183 | .fi | |
184 | is equivalent to | |
185 | .I atomically | |
186 | executing the following calls: | |
187 | .nf | |
188 | ||
189 | sigset_t origmask; | |
190 | ||
191 | sigprocmask(SIG_SETMASK, &sigmask, &origmask); | |
192 | ready = select(nfds, &readfds, &writefds, &exceptfds, timeout); | |
193 | sigprocmask(SIG_SETMASK, &origmask, NULL); | |
194 | .fi | |
195 | .PP | |
196 | The reason that | |
e511ffb6 | 197 | .BR pselect () |
d02aa9bc MK |
198 | is needed is that if one wants to wait for either a signal |
199 | or for a file descriptor to become ready, then | |
200 | an atomic test is needed to prevent race conditions. | |
201 | (Suppose the signal handler sets a global flag and | |
fea681da MK |
202 | returns. Then a test of this global flag followed by a call of |
203 | .BR select () | |
204 | could hang indefinitely if the signal arrived just after the test | |
d02aa9bc MK |
205 | but just before the call. |
206 | By contrast, | |
e511ffb6 | 207 | .BR pselect () |
fea681da MK |
208 | allows one to first block signals, handle the signals that have come in, |
209 | then call | |
210 | .BR pselect () | |
211 | with the desired | |
212 | .IR sigmask , | |
213 | avoiding the race.) | |
fea681da MK |
214 | .SS "The timeout" |
215 | The time structures involved are defined in | |
216 | .I <sys/time.h> | |
217 | and look like | |
218 | ||
2f11acf5 | 219 | .in +0.25i |
fea681da MK |
220 | .nf |
221 | struct timeval { | |
222 | long tv_sec; /* seconds */ | |
223 | long tv_usec; /* microseconds */ | |
224 | }; | |
225 | .fi | |
2f11acf5 | 226 | .in -0.25i |
fea681da MK |
227 | |
228 | and | |
229 | ||
2f11acf5 | 230 | .in +0.25i |
fea681da MK |
231 | .nf |
232 | struct timespec { | |
233 | long tv_sec; /* seconds */ | |
234 | long tv_nsec; /* nanoseconds */ | |
235 | }; | |
236 | .fi | |
2f11acf5 | 237 | .in -0.25i |
fea681da | 238 | |
97c1eac8 | 239 | (However, see below on the POSIX.1-2001 versions.) |
fea681da MK |
240 | .PP |
241 | Some code calls | |
e511ffb6 | 242 | .BR select () |
fea681da | 243 | with all three sets empty, |
79b8032e | 244 | .I nfds |
22f348ca | 245 | zero, and a non-NULL |
fea681da MK |
246 | .I timeout |
247 | as a fairly portable way to sleep with subsecond precision. | |
248 | .PP | |
cc9befa9 | 249 | On Linux, |
e511ffb6 | 250 | .BR select () |
fea681da MK |
251 | modifies |
252 | .I timeout | |
253 | to reflect the amount of time not slept; most other implementations | |
77f00d75 MK |
254 | do not do this. |
255 | (POSIX.1-2001 permits either behaviour.) | |
256 | This causes problems both when Linux code which reads | |
fea681da MK |
257 | .I timeout |
258 | is ported to other operating systems, and when code is ported to Linux | |
259 | that reuses a struct timeval for multiple | |
e511ffb6 | 260 | .BR select ()s |
fea681da MK |
261 | in a loop without reinitializing it. Consider |
262 | .I timeout | |
263 | to be undefined after | |
e511ffb6 | 264 | .BR select () |
fea681da MK |
265 | returns. |
266 | .\" .PP - it is rumoured that: | |
267 | .\" On BSD, when a timeout occurs, the file descriptor bits are not changed. | |
268 | .\" - it is certainly true that: | |
269 | .\" Linux follows SUSv2 and sets the bit masks to zero upon a timeout. | |
270 | .SH "RETURN VALUE" | |
271 | On success, | |
e511ffb6 | 272 | .BR select () |
fea681da | 273 | and |
e511ffb6 | 274 | .BR pselect () |
22f348ca MK |
275 | return the number of file descriptors contained in the three returned |
276 | descriptor sets (that is, the total number of bits that are set in | |
fea681da MK |
277 | .IR readfds , |
278 | .IR writefds , | |
279 | .IR exceptfds ) | |
280 | which may be zero if the timeout expires before anything interesting happens. | |
281 | On error, \-1 is returned, and | |
282 | .I errno | |
283 | is set appropriately; the sets and | |
284 | .I timeout | |
285 | become undefined, so do not | |
286 | rely on their contents after an error. | |
287 | .SH ERRORS | |
288 | .TP | |
289 | .B EBADF | |
290 | An invalid file descriptor was given in one of the sets. | |
6efed4df MK |
291 | (Perhaps a file descriptor that was already closed, |
292 | or one on which an error has occurred.) | |
fea681da MK |
293 | .TP |
294 | .B EINTR | |
6efed4df | 295 | A signal was caught. |
fea681da MK |
296 | .TP |
297 | .B EINVAL | |
6efed4df | 298 | .I nfds |
fea681da MK |
299 | is negative or the value contained within |
300 | .I timeout | |
301 | is invalid. | |
302 | .TP | |
303 | .B ENOMEM | |
6efed4df | 304 | unable to allocate memory for internal tables. |
fea681da MK |
305 | .SH EXAMPLE |
306 | .nf | |
307 | #include <stdio.h> | |
308 | #include <sys/time.h> | |
309 | #include <sys/types.h> | |
310 | #include <unistd.h> | |
311 | ||
312 | int | |
313 | main(void) { | |
314 | fd_set rfds; | |
315 | struct timeval tv; | |
316 | int retval; | |
317 | ||
318 | /* Watch stdin (fd 0) to see when it has input. */ | |
319 | FD_ZERO(&rfds); | |
320 | FD_SET(0, &rfds); | |
6efed4df | 321 | |
fea681da MK |
322 | /* Wait up to five seconds. */ |
323 | tv.tv_sec = 5; | |
324 | tv.tv_usec = 0; | |
325 | ||
326 | retval = select(1, &rfds, NULL, NULL, &tv); | |
327 | /* Don't rely on the value of tv now! */ | |
328 | ||
2bc2f479 | 329 | if (retval == \-1) |
fea681da MK |
330 | perror("select()"); |
331 | else if (retval) | |
332 | printf("Data is available now.\\n"); | |
333 | /* FD_ISSET(0, &rfds) will be true. */ | |
334 | else | |
335 | printf("No data within five seconds.\\n"); | |
336 | ||
337 | return 0; | |
338 | } | |
339 | .fi | |
340 | .SH "CONFORMING TO" | |
97c1eac8 MK |
341 | .BR select () |
342 | conforms to POSIX.1-2001 and | |
cc9befa9 MK |
343 | 4.4BSD |
344 | .RB ( select () | |
345 | first appeared in 4.2BSD). Generally portable to/from | |
fea681da MK |
346 | non-BSD systems supporting clones of the BSD socket layer (including |
347 | System V variants). However, note that the System V variant typically | |
348 | sets the timeout variable before exit, but the BSD variant does not. | |
349 | .PP | |
e511ffb6 | 350 | .BR pselect () |
97c1eac8 MK |
351 | is defined in POSIX.1g, and in |
352 | POSIX.1-2001. | |
fea681da | 353 | .SH NOTES |
22f348ca MK |
354 | An |
355 | .I fd_set | |
356 | is a fixed size buffer. | |
357 | Executing | |
358 | .BR FD_CLR () | |
359 | or | |
360 | .BR FD_SET () | |
361 | with a value of | |
fea681da MK |
362 | .I fd |
363 | that is negative or is equal to or larger than FD_SETSIZE will result | |
364 | in undefined behavior. Moreover, POSIX requires | |
365 | .I fd | |
366 | to be a valid file descriptor. | |
367 | ||
368 | Concerning the types involved, the classical situation is that | |
22f348ca MK |
369 | the two fields of a |
370 | .I timeval | |
371 | structure are longs (as shown above), | |
372 | and the structure is defined in | |
fea681da | 373 | .IR <sys/time.h> . |
97c1eac8 | 374 | The POSIX.1-2001 situation is |
fea681da MK |
375 | |
376 | .RS | |
377 | .nf | |
378 | struct timeval { | |
379 | time_t tv_sec; /* seconds */ | |
380 | suseconds_t tv_usec; /* microseconds */ | |
381 | }; | |
382 | .fi | |
383 | .RE | |
384 | ||
22f348ca | 385 | where the structure is defined in |
fea681da | 386 | .I <sys/select.h> |
d883fde0 | 387 | and the data types |
22f348ca MK |
388 | .I time_t |
389 | and | |
390 | .I suseconds_t | |
391 | are defined in | |
fea681da MK |
392 | .IR <sys/types.h> . |
393 | .LP | |
394 | Concerning prototypes, the classical situation is that one should | |
395 | include | |
396 | .I <time.h> | |
397 | for | |
e511ffb6 | 398 | .BR select (). |
97c1eac8 | 399 | The POSIX.1-2001 situation is that one should include |
fea681da MK |
400 | .I <sys/select.h> |
401 | for | |
e511ffb6 | 402 | .BR select () |
fea681da | 403 | and |
e511ffb6 | 404 | .BR pselect (). |
fea681da MK |
405 | Libc4 and libc5 do not have a |
406 | .I <sys/select.h> | |
407 | header; under glibc 2.0 and later this header exists. | |
408 | Under glibc 2.0 it unconditionally gives the wrong prototype for | |
e511ffb6 | 409 | .BR pselect (), |
fea681da | 410 | under glibc 2.1-2.2.1 it gives |
e511ffb6 | 411 | .BR pselect () |
fea681da MK |
412 | when |
413 | .B _GNU_SOURCE | |
414 | is defined, under glibc 2.2.2-2.2.4 it gives it when | |
415 | .B _XOPEN_SOURCE | |
416 | is defined and has a value of 600 or larger. | |
97c1eac8 | 417 | No doubt, since POSIX.1-2001, it should give the prototype by default. |
cc9befa9 MK |
418 | .SH VERSIONS |
419 | .BR pselect () | |
420 | was added to Linux in kernel 2.6.16. | |
cc9befa9 MK |
421 | Prior to this, |
422 | .BR pselect () | |
423 | was emulated in glibc (but see BUGS). | |
77f00d75 MK |
424 | .SH "LINUX NOTES" |
425 | The Linux | |
426 | .BR pselect () | |
2f11acf5 MK |
427 | system call modifies its |
428 | .I timeout | |
429 | argument. | |
430 | However, the glibc wrapper function hides this behaviour | |
77f00d75 MK |
431 | by using a local variable for the timeout argument that |
432 | is passed to the system call. | |
433 | Thus, the glibc | |
434 | .BR pselect () | |
435 | function does not modify its timeout argument; | |
436 | this is the behaviour required by POSIX.1-2001. | |
fea681da | 437 | .SH BUGS |
cc9befa9 | 438 | Glibc 2.0 provided a version of |
e511ffb6 | 439 | .BR pselect () |
cc9befa9 MK |
440 | that did not take a |
441 | .I sigmask | |
442 | argument. | |
443 | ||
444 | Since version 2.1, glibc has provided an emulation of | |
445 | .BR pselect () | |
446 | that is implemented using | |
447 | .BR sigprocmask (2) | |
448 | and | |
449 | .BR select (). | |
450 | This implementation remains vulnerable to the very race condition that | |
451 | .BR pselect () | |
452 | was designed to prevent. | |
453 | On systems that lack | |
454 | .BR pselect () | |
455 | reliable (and more portable) signal trapping can be achieved | |
456 | using the self-pipe trick | |
457 | (where a signal handler writes a byte to a pipe whose other end | |
458 | is monitored by | |
459 | .BR select () | |
460 | in the main program.) | |
fea681da MK |
461 | |
462 | Under Linux, | |
e511ffb6 | 463 | .BR select () |
fea681da MK |
464 | may report a socket file descriptor as "ready for reading", while |
465 | nevertheless a subsequent read blocks. This could for example | |
466 | happen when data has arrived but upon examination has wrong | |
2f11acf5 MK |
467 | checksum and is discarded. There may be other circumstances |
468 | in which a file descriptor is spuriously reported as ready. | |
fea681da MK |
469 | .\" Stevens discusses a case where accept can block after select |
470 | .\" returns successfully because of an intervening RST from the client. | |
471 | Thus it may be safer to use O_NONBLOCK on sockets that should not block. | |
472 | .\" Maybe the kernel should have returned EIO in such a situation? | |
d02aa9bc MK |
473 | .\" |
474 | .\" FIXME select() (and pselect()?) also modify the timeout | |
777f5a9e | 475 | .\" on an EINTR error return; POSIX.1-2001 doesn't permit this. |
fea681da MK |
476 | .SH "SEE ALSO" |
477 | For a tutorial with discussion and examples, see | |
478 | .BR select_tut (2). | |
479 | .LP | |
480 | For vaguely related stuff, see | |
481 | .BR accept (2), | |
482 | .BR connect (2), | |
483 | .BR poll (2), | |
484 | .BR read (2), | |
485 | .BR recv (2), | |
486 | .BR send (2), | |
487 | .BR sigprocmask (2), | |
50e5322c | 488 | .BR write (2), |
6efed4df | 489 | .BR epoll (7), |
a8e7c990 | 490 | .BR feature_test_macros (7) |