OSDN Git Service

Update perkamon to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / 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 2015-01-22 "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 exceeds the
332 .BR RLIMIT_NOFILE
333 resource limit (see
334 .BR getrlimit (2)).
335 .TP
336 .B EINVAL
337 the value contained within
338 .I timeout
339 is invalid.
340 .TP
341 .B ENOMEM
342 unable to allocate memory for internal tables.
343 .SH VERSIONS
344 .BR pselect ()
345 was added to Linux in kernel 2.6.16.
346 Prior to this,
347 .BR pselect ()
348 was emulated in glibc (but see BUGS).
349 .SH CONFORMING TO
350 .BR select ()
351 conforms to POSIX.1-2001 and
352 4.4BSD
353 .RB ( select ()
354 first appeared in 4.2BSD).
355 Generally portable to/from
356 non-BSD systems supporting clones of the BSD socket layer (including
357 System\ V variants).
358 However, note that the System\ V variant typically
359 sets the timeout variable before exit, but the BSD variant does not.
360 .PP
361 .BR pselect ()
362 is defined in POSIX.1g, and in
363 POSIX.1-2001.
364 .SH NOTES
365 An
366 .I fd_set
367 is a fixed size buffer.
368 Executing
369 .BR FD_CLR ()
370 or
371 .BR FD_SET ()
372 with a value of
373 .I fd
374 that is negative or is equal to or larger than
375 .B FD_SETSIZE
376 will result
377 in undefined behavior.
378 Moreover, POSIX requires
379 .I fd
380 to be a valid file descriptor.
381
382 Concerning the types involved, the classical situation is that
383 the two fields of a
384 .I timeval
385 structure are typed as
386 .I long
387 (as shown above), and the structure is defined in
388 .IR <sys/time.h> .
389 The POSIX.1-2001 situation is
390
391 .in +4n
392 .nf
393 struct timeval {
394     time_t         tv_sec;     /* seconds */
395     suseconds_t    tv_usec;    /* microseconds */
396 };
397 .fi
398 .in
399
400 where the structure is defined in
401 .I <sys/select.h>
402 and the data types
403 .I time_t
404 and
405 .I suseconds_t
406 are defined in
407 .IR <sys/types.h> .
408 .LP
409 Concerning prototypes, the classical situation is that one should
410 include
411 .I <time.h>
412 for
413 .BR select ().
414 The POSIX.1-2001 situation is that one should include
415 .I <sys/select.h>
416 for
417 .BR select ()
418 and
419 .BR pselect ().
420
421 Under glibc 2.0,
422 .I <sys/select.h>
423 gives the wrong prototype for
424 .BR pselect ().
425 Under glibc 2.1 to 2.2.1, it gives
426 .BR pselect ()
427 when
428 .B _GNU_SOURCE
429 is defined.
430 Since glibc 2.2.2, the requirements are as shown in the SYNOPSIS.
431 .SS Multithreaded applications
432 If a file descriptor being monitored by
433 .BR select ()
434 is closed in another thread, the result is unspecified.
435 On some UNIX systems,
436 .BR select ()
437 unblocks and returns, with an indication that the file descriptor is ready
438 (a subsequent I/O operation will likely fail with an error,
439 unless another the file descriptor reopened between the time
440 .BR select ()
441 returned and the I/O operations was performed).
442 On Linux (and some other systems),
443 closing the file descriptor in another thread has no effect on
444 .BR select ().
445 In summary, any application that relies on a particular behavior
446 in this scenario must be considered buggy.
447 .\"
448 .SS C library/kernel ABI differences
449 The
450 .BR pselect ()
451 interface described in this page is implemented by glibc.
452 The underlying Linux system call is named
453 .BR pselect6 ().
454 This system call has somewhat different behavior from the glibc
455 wrapper function.
456
457 The Linux
458 .BR pselect6 ()
459 system call modifies its
460 .I timeout
461 argument.
462 However, the glibc wrapper function hides this behavior
463 by using a local variable for the timeout argument that
464 is passed to the system call.
465 Thus, the glibc
466 .BR pselect ()
467 function does not modify its
468 .I timeout
469 argument;
470 this is the behavior required by POSIX.1-2001.
471
472 The final argument of the
473 .BR pselect6 ()
474 system call is not a
475 .I "sigset_t\ *"
476 pointer, but is instead a structure of the form:
477 .in +4
478 .nf
479
480 struct {
481     const sigset_t *ss;     /* Pointer to signal set */
482     size_t          ss_len; /* Size (in bytes) of object pointed
483                                to by 'ss' */
484 };
485
486 .fi
487 .in
488 This allows the system call to obtain both
489 a pointer to the signal set and its size,
490 while allowing for the fact that most architectures
491 support a maximum of 6 arguments to a system call.
492 .SH BUGS
493 Glibc 2.0 provided a version of
494 .BR pselect ()
495 that did not take a
496 .I sigmask
497 argument.
498
499 Starting with version 2.1, glibc provided an emulation of
500 .BR pselect ()
501 that was implemented using
502 .BR sigprocmask (2)
503 and
504 .BR select ().
505 This implementation remained vulnerable to the very race condition that
506 .BR pselect ()
507 was designed to prevent.
508 Modern versions of glibc use the (race-free)
509 .BR pselect ()
510 system call on kernels where it is provided.
511
512 On systems that lack
513 .BR pselect (),
514 reliable (and more portable) signal trapping can be achieved
515 using the self-pipe trick.
516 In this technique,
517 a signal handler writes a byte to a pipe whose other end
518 is monitored by
519 .BR select ()
520 in the main program.
521 (To avoid possibly blocking when writing to a pipe that may be full
522 or reading from a pipe that may be empty,
523 nonblocking I/O is used when reading from and writing to the pipe.)
524
525 Under Linux,
526 .BR select ()
527 may report a socket file descriptor as "ready for reading", while
528 nevertheless a subsequent read blocks.
529 This could for example
530 happen when data has arrived but upon examination has wrong
531 checksum and is discarded.
532 There may be other circumstances
533 in which a file descriptor is spuriously reported as ready.
534 .\" Stevens discusses a case where accept can block after select
535 .\" returns successfully because of an intervening RST from the client.
536 Thus it may be safer to use
537 .B O_NONBLOCK
538 on sockets that should not block.
539 .\" Maybe the kernel should have returned EIO in such a situation?
540
541 On Linux,
542 .BR select ()
543 also modifies
544 .I timeout
545 if the call is interrupted by a signal handler (i.e., the
546 .B EINTR
547 error return).
548 This is not permitted by POSIX.1-2001.
549 The Linux
550 .BR pselect ()
551 system call has the same behavior,
552 but the glibc wrapper hides this behavior by internally copying the
553 .I timeout
554 to a local variable and passing that variable to the system call.
555 .SH EXAMPLE
556 .nf
557 #include <stdio.h>
558 #include <stdlib.h>
559 #include <sys/time.h>
560 #include <sys/types.h>
561 #include <unistd.h>
562
563 int
564 main(void)
565 {
566     fd_set rfds;
567     struct timeval tv;
568     int retval;
569
570     /* Watch stdin (fd 0) to see when it has input. */
571     FD_ZERO(&rfds);
572     FD_SET(0, &rfds);
573
574     /* Wait up to five seconds. */
575     tv.tv_sec = 5;
576     tv.tv_usec = 0;
577
578     retval = select(1, &rfds, NULL, NULL, &tv);
579     /* Don't rely on the value of tv now! */
580
581     if (retval == \-1)
582         perror("select()");
583     else if (retval)
584         printf("Data is available now.\\n");
585         /* FD_ISSET(0, &rfds) will be true. */
586     else
587         printf("No data within five seconds.\\n");
588
589     exit(EXIT_SUCCESS);
590 }
591 .fi
592 .SH SEE ALSO
593 .BR accept (2),
594 .BR connect (2),
595 .BR poll (2),
596 .BR read (2),
597 .BR recv (2),
598 .BR restart_syscall (2),
599 .BR send (2),
600 .BR sigprocmask (2),
601 .BR write (2),
602 .BR epoll (7),
603 .BR time (7)
604
605 For a tutorial with discussion and examples, see
606 .BR select_tut (2).
607 .SH COLOPHON
608 This page is part of release 3.78 of the Linux
609 .I man-pages
610 project.
611 A description of the project,
612 information about reporting bugs,
613 and the latest version of this page,
614 can be found at
615 \%http://www.kernel.org/doc/man\-pages/.