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