version 1, including all changes.
.
Rev |
Author |
# |
Line |
1 |
perry |
1 |
SELECT |
|
|
2 |
!!!SELECT |
|
|
3 |
NAME |
|
|
4 |
SYNOPSIS |
|
|
5 |
DESCRIPTION |
|
|
6 |
RETURN VALUE |
|
|
7 |
ERRORS |
|
|
8 |
NOTES |
|
|
9 |
EXAMPLE |
|
|
10 |
CONFORMING TO |
|
|
11 |
SEE ALSO |
|
|
12 |
---- |
|
|
13 |
!!NAME |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
select, pselect, FD_CLR, FD_ISSET, FD_SET, FD_ZERO - synchronous I/O multiplexing |
|
|
17 |
!!SYNOPSIS |
|
|
18 |
|
|
|
19 |
|
|
|
20 |
__#include __ |
|
|
21 |
#include __ |
|
|
22 |
#include __ |
|
|
23 |
|
|
|
24 |
|
|
|
25 |
__int select(int__ ''n''__, fd_set |
|
|
26 |
*__''readfds''__, fd_set *__''writefds''__, |
|
|
27 |
fd_set *__''exceptfds''__, struct timeval |
|
|
28 |
*__''timeout''__);__ |
|
|
29 |
|
|
|
30 |
|
|
|
31 |
__int pselect(int__ ''n''__, fd_set |
|
|
32 |
*__''readfds''__, fd_set *__''writefds''__, |
|
|
33 |
fd_set *__''exceptfds''__, const struct timespec |
|
|
34 |
*__''timeout''__, sigset_t *__ |
|
|
35 |
''sigmask''__);__ |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
__FD_CLR(int__ ''fd''__, fd_set |
|
|
39 |
*__''set''__); |
|
|
40 |
FD_ISSET(int__ ''fd''__, fd_set |
|
|
41 |
*__''set''__); |
|
|
42 |
FD_SET(int__ ''fd''__, fd_set *__''set''__); |
|
|
43 |
FD_ZERO(fd_set *__''set''__);__ |
|
|
44 |
!!DESCRIPTION |
|
|
45 |
|
|
|
46 |
|
|
|
47 |
The functions __select__ and __pselect__ wait for a |
|
|
48 |
number of file descriptors to change status. |
|
|
49 |
|
|
|
50 |
|
|
|
51 |
Their function is identical, with three |
|
|
52 |
differences: |
|
|
53 |
|
|
|
54 |
|
|
|
55 |
(i) |
|
|
56 |
|
|
|
57 |
|
|
|
58 |
The __select__ function uses a timeout that is a |
|
|
59 |
''struct timeval'' (with seconds and microseconds), while |
|
|
60 |
__pselect__ uses a ''struct timespec'' (with seconds |
|
|
61 |
and nanoseconds). |
|
|
62 |
|
|
|
63 |
|
|
|
64 |
(ii) |
|
|
65 |
|
|
|
66 |
|
|
|
67 |
The __select__ function may update the ''timeout'' |
|
|
68 |
parameter to indicate how much time was left. The |
|
|
69 |
__pselect__ function does not change this |
|
|
70 |
parameter. |
|
|
71 |
|
|
|
72 |
|
|
|
73 |
(iii) |
|
|
74 |
|
|
|
75 |
|
|
|
76 |
The __select__ function has no ''sigmask'' parameter, |
|
|
77 |
and behaves as __pselect__ called with NULL |
|
|
78 |
''sigmask''. |
|
|
79 |
|
|
|
80 |
|
|
|
81 |
Three independent sets of descriptors are watched. Those |
|
|
82 |
listed in ''readfds'' will be watched to see if |
|
|
83 |
characters become available for reading (more precisely, to |
|
|
84 |
see if a read will not block - in particular, a file |
|
|
85 |
descriptor is also ready on end-of-file), those in |
|
|
86 |
''writefds'' will be watched to see if a write will not |
|
|
87 |
block, and those in ''exceptfds'' will be watched for |
|
|
88 |
exceptions. On exit, the sets are modified in place to |
|
|
89 |
indicate which descriptors actually changed |
|
|
90 |
status. |
|
|
91 |
|
|
|
92 |
|
|
|
93 |
Four macros are provided to manipulate the sets. |
|
|
94 |
__FD_ZERO__ will clear a set. __FD_SET__ and |
|
|
95 |
__FD_CLR__ add or remove a given descriptor from a set. |
|
|
96 |
__FD_ISSET__ tests to see if a descriptor is part of the |
|
|
97 |
set; this is useful after __select__ |
|
|
98 |
returns. |
|
|
99 |
|
|
|
100 |
|
|
|
101 |
''n'' is the highest-numbered descriptor in any of the |
|
|
102 |
three sets, plus 1. |
|
|
103 |
|
|
|
104 |
|
|
|
105 |
''timeout'' is an upper bound on the amount of time |
|
|
106 |
elapsed before __select__ returns. It may be zero, |
|
|
107 |
causing __select__ to return immediately. (This is useful |
|
|
108 |
for polling.) If ''timeout'' is NULL (no timeout), |
|
|
109 |
__select__ can block indefinitely. |
|
|
110 |
|
|
|
111 |
|
|
|
112 |
''sigmask'' is a pointer to a signal mask (see |
|
|
113 |
sigprocmask(2)); if it is not NULL, then |
|
|
114 |
__pselect__ first replaces the current signal mask by the |
|
|
115 |
one pointed to by ''sigmask'', then does the `select' |
|
|
116 |
function, and then restores the original signal mask |
|
|
117 |
again. |
|
|
118 |
|
|
|
119 |
|
|
|
120 |
The idea of __pselect__ is that if one wants to wait for |
|
|
121 |
an event, either a signal or something on a file descriptor, |
|
|
122 |
an atomic test is needed to prevent race conditions. |
|
|
123 |
(Suppose the signal handler sets a global flag and returns. |
|
|
124 |
Then a test of this global flag followed by a call of |
|
|
125 |
__select__() could hang indefinitely if the signal |
|
|
126 |
arrived just after the test but just before the call. On the |
|
|
127 |
other hand, __pselect__ allows one to first block |
|
|
128 |
signals, handle the signals that have come in, then call |
|
|
129 |
__pselect__() with the desired ''sigmask'', avoiding |
|
|
130 |
the race.) Since Linux today does not have a |
|
|
131 |
''pselect''() system call, the current glibc2 routine |
|
|
132 |
still contains this race. |
|
|
133 |
!!RETURN VALUE |
|
|
134 |
|
|
|
135 |
|
|
|
136 |
On success, __select__ and __pselect__ return the |
|
|
137 |
number of descriptors contained in the descriptor sets, |
|
|
138 |
which may be zero if the timeout expires before anything |
|
|
139 |
interesting happens. On error, -1 is returned, and |
|
|
140 |
''errno'' is set appropriately; the sets and |
|
|
141 |
''timeout'' become undefined, so do not rely on their |
|
|
142 |
contents after an error. |
|
|
143 |
!!ERRORS |
|
|
144 |
|
|
|
145 |
|
|
|
146 |
__EBADF__ |
|
|
147 |
|
|
|
148 |
|
|
|
149 |
An invalid file descriptor was given in one of the |
|
|
150 |
sets. |
|
|
151 |
|
|
|
152 |
|
|
|
153 |
__EINTR__ |
|
|
154 |
|
|
|
155 |
|
|
|
156 |
A non blocked signal was caught. |
|
|
157 |
|
|
|
158 |
|
|
|
159 |
__EINVAL__ |
|
|
160 |
|
|
|
161 |
|
|
|
162 |
''n'' is negative. |
|
|
163 |
|
|
|
164 |
|
|
|
165 |
__ENOMEM__ |
|
|
166 |
|
|
|
167 |
|
|
|
168 |
__select__ was unable to allocate memory for internal |
|
|
169 |
tables. |
|
|
170 |
!!NOTES |
|
|
171 |
|
|
|
172 |
|
|
|
173 |
Some code calls __select__ with all three sets empty, |
|
|
174 |
__n__ zero, and a non-null ''timeout'' as a fairly |
|
|
175 |
portable way to sleep with subsecond precision. |
|
|
176 |
|
|
|
177 |
|
|
|
178 |
On Linux, ''timeout'' is modified to reflect the amount |
|
|
179 |
of time not slept; most other implementations do not do |
|
|
180 |
this. This causes problems both when Linux code which reads |
|
|
181 |
''timeout'' is ported to other operating systems, and |
|
|
182 |
when code is ported to Linux that reuses a struct timeval |
|
|
183 |
for multiple __select__s in a loop without reinitializing |
|
|
184 |
it. Consider ''timeout'' to be undefined after |
|
|
185 |
__select__ returns. |
|
|
186 |
!!EXAMPLE |
|
|
187 |
|
|
|
188 |
|
|
|
189 |
#include |
|
|
190 |
!!CONFORMING TO |
|
|
191 |
|
|
|
192 |
|
|
|
193 |
4.4BSD (the __select__ function first appeared in |
|
|
194 |
4.2BSD). Generally portable to/from non-BSD systems |
|
|
195 |
supporting clones of the BSD socket layer (including System |
|
|
196 |
V variants). However, note that the System V variant |
|
|
197 |
typically sets the timeout variable before exit, but the BSD |
|
|
198 |
variant does not. |
|
|
199 |
|
|
|
200 |
|
|
|
201 |
The __pselect__ function is defined in IEEE Std |
|
|
202 |
1003.1g-2000 (POSIX.1g). It is found in glibc2.1 and later. |
|
|
203 |
Glibc2.0 has a function with this name, that however does |
|
|
204 |
not take a ''sigmask'' parameter. |
|
|
205 |
!!SEE ALSO |
|
|
206 |
|
|
|
207 |
|
|
|
208 |
accept(2), connect(2), poll(2), |
|
|
209 |
read(2), recv(2), send(2), |
|
|
210 |
sigprocmask(2), write(2) |
|
|
211 |
---- |