1SELECT(2) System Calls Manual SELECT(2)
2
3
4
6 pselect, select - synchronous I/O multiplexing
7
9 #include <sys/types.h>
10 #include <sys/time.h>
11 #include <sys/select.h>
12 #include <signal.h>
13
14 nfound = pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask);
15 int nfound, nfds;
16 fd_set *readfds, *writefds, *exceptfds;
17 struct timespec *timeout;
18 sigset_t *sigmask;
19
20 nfound = select(nfds, readfds, writefds, exceptfds, timeout)
21 int nfound, nfds;
22 fd_set *readfds, *writefds, *exceptfds;
23 struct timeval *timeout;
24
25 FD_SET(fd, &fdset)
26 FD_CLR(fd, &fdset)
27 FD_ISSET(fd, &fdset)
28 FD_ZERO(&fdset)
29 int fd;
30 fd_set fdset;
31
33 Pselect and select examine the I/O descriptor sets whose addresses are
34 passed in readfds, writefds, and exceptfds to see if some of their
35 descriptors are ready for reading, are ready for writing, or have an
36 exceptional condition pending, respectively. The two functions are
37 identical except for the type and format of the timeout value, and the
38 additonal sigmask parameter supplied to the pselect() call.
39
40 The first nfds descriptors are checked in each set; i.e. the descrip‐
41 tors from 0 through nfds-1 in the descriptor sets are examined. On
42 return, select replaces the given descriptor sets with subsets consist‐
43 ing of those descriptors that are ready for the requested operation.
44 The total number of ready descriptors in all the sets is returned in
45 nfound.
46
47 The descriptor sets are stored as bit fields in arrays of integers.
48 The following macros are provided for manipulating such descriptor
49 sets: FD_ZERO(&fdset) initializes a descriptor set fdset to the null
50 set. FD_SET(fd, &fdset) includes a particular descriptor fd in fdset.
51 FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is
52 nonzero if fd is a member of fdset, zero otherwise. The behavior of
53 these macros is undefined if a descriptor value is less than zero or
54 greater than or equal to FD_SETSIZE, which is normally at least equal
55 to the maximum number of descriptors supported by the system.
56
57 If timeout is a non-zero pointer, it specifies a maximum interval to
58 wait for the selection to complete. If timeout is a zero pointer,
59 select blocks indefinitely. To affect a poll, the timeout argument
60 should be non-zero, pointing to a zero-valued timeval structure.
61
62 If the sigmask parameter to pselect() is not NULL, it points to a sig‐
63 nal mask that replaces the previous signal mask for the process for the
64 duration of the call, and the previous mask is restored upon return;
65 see sigprocmask(3). This is normally used so that signals can be
66 blocked while preparing for a call to pselect() and then atomically
67 unblocking the signals while selecting.
68
69 Any of readfds, writefds, and exceptfds may be given as zero pointers
70 if no descriptors are of interest.
71
73 Select returns the number of ready descriptors that are contained in
74 the descriptor sets, or -1 if an error occurred. If the time limit
75 expires then select returns 0. If select returns with an error,
76 including one due to an interrupted call, the descriptor sets will be
77 unmodified.
78
80 An error return from select indicates:
81
82 [EBADF] One of the descriptor sets specified an invalid descrip‐
83 tor.
84
85 [EINTR] A signal was delivered before the time limit expired and
86 before any of the selected events occurred.
87
88 [EINVAL] The specified time limit is invalid. One of its compo‐
89 nents is negative or too large.
90
92 accept(2), connect(2), read(2), write(2), recv(2), send(2), getdtable‐
93 size(2)
94
96 Although the provision of getdtablesize(2) was intended to allow user
97 programs to be written independent of the kernel limit on the number of
98 open files, the dimension of a sufficiently large bit field for select
99 remains a problem. The default size FD_SETSIZE (currently 256) is
100 somewhat larger than the current kernel limit to the number of open
101 files. However, in order to accommodate programs which might poten‐
102 tially use a larger number of open files with select, it is possible to
103 increase this size within a program by providing a larger definition of
104 FD_SETSIZE before the inclusion of <sys/types.h>.
105
106 Select should probably return the time remaining from the original
107 timeout, if any, by modifying the time value in place. This may be
108 implemented in future versions of the system. Thus, it is unwise to
109 assume that the timeout value will be unmodified by the select call.
110
111 In 2BSD the timeout is implemented in the kernel using the callout ta‐
112 ble. Since a callout structure only has a signed short to store the
113 number of ticks till expiration the maximum value of a kernel timeout
114 is 32767 ticks. In the US (60hz power) this gives a maximum timeout of
115 approximately 9 minutes. In countries using 50hz power the maximum
116 timeout is about 13 minutes.
117
118 struct timespec on a PDP-11 is silly since the hardware has nowhere
119 near microsecond much less nanosecond clock resolution.
120
121
122
1234.2 Berkeley Distribution March 4, 2000 SELECT(2)