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