1SELECT(2) Linux Programmer's Manual SELECT(2)
2
3
4
6 select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O
7 multiplexing
8
10 #include <sys/select.h>
11
12 int select(int nfds, fd_set *restrict readfds,
13 fd_set *restrict writefds, fd_set *restrict exceptfds,
14 struct timeval *restrict timeout);
15
16 void FD_CLR(int fd, fd_set *set);
17 int FD_ISSET(int fd, fd_set *set);
18 void FD_SET(int fd, fd_set *set);
19 void FD_ZERO(fd_set *set);
20
21 int pselect(int nfds, fd_set *restrict readfds,
22 fd_set *restrict writefds, fd_set *restrict exceptfds,
23 const struct timespec *restrict timeout,
24 const sigset_t *restrict sigmask);
25
26 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
27
28 pselect():
29 _POSIX_C_SOURCE >= 200112L
30
32 WARNING: select() can monitor only file descriptors numbers that are
33 less than FD_SETSIZE (1024)—an unreasonably low limit for many modern
34 applications—and this limitation will not change. All modern applica‐
35 tions should instead use poll(2) or epoll(7), which do not suffer this
36 limitation.
37
38 select() allows a program to monitor multiple file descriptors, waiting
39 until one or more of the file descriptors become "ready" for some class
40 of I/O operation (e.g., input possible). A file descriptor is consid‐
41 ered ready if it is possible to perform a corresponding I/O operation
42 (e.g., read(2), or a sufficiently small write(2)) without blocking.
43
44 File descriptor sets
45 The principal arguments of select() are three "sets" of file descrip‐
46 tors (declared with the type fd_set), which allow the caller to wait
47 for three classes of events on the specified set of file descriptors.
48 Each of the fd_set arguments may be specified as NULL if no file de‐
49 scriptors are to be watched for the corresponding class of events.
50
51 Note well: Upon return, each of the file descriptor sets is modified in
52 place to indicate which file descriptors are currently "ready". Thus,
53 if using select() within a loop, the sets must be reinitialized before
54 each call.
55
56 The contents of a file descriptor set can be manipulated using the fol‐
57 lowing macros:
58
59 FD_ZERO()
60 This macro clears (removes all file descriptors from) set. It
61 should be employed as the first step in initializing a file de‐
62 scriptor set.
63
64 FD_SET()
65 This macro adds the file descriptor fd to set. Adding a file
66 descriptor that is already present in the set is a no-op, and
67 does not produce an error.
68
69 FD_CLR()
70 This macro removes the file descriptor fd from set. Removing a
71 file descriptor that is not present in the set is a no-op, and