2 select, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous
8 int select(int n, fd_set *readfds, fd_set *writefds,
9 fd_set *exceptfds, struct timeval *timeout);
11 FD_CLR(int fd, fd_set *set);
12 FD_ISSET(int fd, fd_set *set);
13 FD_SET(int fd, fd_set *set);
17 select waits for a number of file descriptors to change
20 Three independent sets of descriptors are watched. Those
21 listed in readfds will be watched to see if characters
22 become available for reading, those in writefds will be
23 watched to see if it is ok to immediately write on them,
24 and those in exceptfds will be watched for exceptions. On
25 exit, the sets are modified in place to indicate which
26 descriptors actually changed status.
28 Four macros are provided to manipulate the sets. FD_ZERO
29 will clear a set. FD_SET and FD_CLR add or remove a given
30 descriptor from a set. FD_ISSET tests to see if a
31 descriptor is part of the set; this is useful after select
34 n is the highest-numbered descriptor in any of the three
37 timeout is an upper bound on the amount of time elapsed
38 before select returns. It may be zero, causing select to
39 return immediately. If timeout is NULL (no timeout),
40 select can block indefinitely.
43 On success, select returns the number of descriptors con-
44 tained in the descriptor sets, which may be zero if the
45 timeout expires before anything interesting happens. On
46 error, -1 is returned, and errno is set appropriately; the
47 sets and timeout become undefined, so do not rely on their
48 contents after an error.
51 EBADF An invalid file descriptor was given in one of the
54 EINTR A non blocked signal was caught.
58 ENOMEM select was unable to allocate memory for internal
62 Some code calls select with all three sets empty, n zero,
63 and a non-null timeout as a fairly portable way to sleep
64 with subsecond precision.
69 #include <sys/types.h>
79 /* Watch stdin (fd 0) to see when it has input. */
82 /* Wait up to five seconds. */
86 retval = select(1, &rfds, NULL, NULL, &tv);
87 /* Don't rely on the value of tv now! */
90 printf("Data is available now.\n");
91 /* FD_ISSET(0, &rfds) will be true. */
93 printf("No data within five seconds.\n");
98 Generally portable to/from non-BSD systems supporting
99 clones of the BSD socket layer (including System V vari-
100 ants). However, note that the System V variant typically
101 sets the timeout variable before exit, but the BSD variant