1PSELECT(3P) POSIX Programmer's Manual PSELECT(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 pselect, select - synchronous I/O multiplexing
13
15 #include <sys/select.h>
16
17 int pselect(int nfds, fd_set *restrict readfds,
18 fd_set *restrict writefds, fd_set *restrict errorfds,
19 const struct timespec *restrict timeout,
20 const sigset_t *restrict sigmask);
21 int select(int nfds, fd_set *restrict readfds,
22 fd_set *restrict writefds, fd_set *restrict errorfds,
23 struct timeval *restrict timeout);
24 void FD_CLR(int fd, fd_set *fdset);
25 int FD_ISSET(int fd, fd_set *fdset);
26 void FD_SET(int fd, fd_set *fdset);
27 void FD_ZERO(fd_set *fdset);
28
29
31 The pselect() function shall examine the file descriptor sets whose
32 addresses are passed in the readfds, writefds, and errorfds parameters
33 to see whether some of their descriptors are ready for reading, are
34 ready for writing, or have an exceptional condition pending, respec‐
35 tively.
36
37 The select() function shall be equivalent to the pselect() function,
38 except as follows:
39
40 * For the select() function, the timeout period is given in seconds
41 and microseconds in an argument of type struct timeval, whereas for
42 the pselect() function the timeout period is given in seconds and
43 nanoseconds in an argument of type struct timespec.
44
45 * The select() function has no sigmask argument; it shall behave as
46 pselect() does when sigmask is a null pointer.
47
48 * Upon successful completion, the select() function may modify the
49 object pointed to by the timeout argument.
50
51 The pselect() and select() functions shall support regular files, ter‐
52 minal and pseudo-terminal devices, STREAMS-based files, FIFOs, pipes,
53 and sockets. The behavior of pselect() and select() on file descriptors
54 that refer to other types of file is unspecified.
55
56 The nfds argument specifies the range of descriptors to be tested. The
57 first nfds descriptors shall be checked in each set; that is, the
58 descriptors from zero through nfds-1 in the descriptor sets shall be
59 examined.
60
61 If the readfds argument is not a null pointer, it points to an object
62 of type fd_set that on input specifies the file descriptors to be
63 checked for being ready to read, and on output indicates which file
64 descriptors are ready to read.
65
66 If the writefds argument is not a null pointer, it points to an object
67 of type fd_set that on input specifies the file descriptors to be
68 checked for being ready to write, and on output indicates which file
69 descriptors are ready to write.
70
71 If the errorfds argument is not a null pointer, it points to an object
72 of type fd_set that on input specifies the file descriptors to be
73 checked for error conditions pending, and on output indicates which
74 file descriptors have error conditions pending.
75
76 Upon successful completion, the pselect() or select() function shall
77 modify the objects pointed to by the readfds, writefds, and errorfds
78 arguments to indicate which file descriptors are ready for reading,
79 ready for writing, or have an error condition pending, respectively,
80 and shall return the total number of ready descriptors in all the out‐
81 put sets. For each file descriptor less than nfds, the corresponding
82 bit shall be set on successful completion if it was set on input and
83 the associated condition is true for that file descriptor.
84
85 If none of the selected descriptors are ready for the requested opera‐
86 tion, the pselect() or select() function shall block until at least one
87 of the requested operations becomes ready, until the timeout occurs, or
88 until interrupted by a signal. The timeout parameter controls how long
89 the pselect() or select() function shall take before timing out. If the
90 timeout parameter is not a null pointer, it specifies a maximum inter‐
91 val to wait for the selection to complete. If the specified time
92 interval expires without any requested operation becoming ready, the
93 function shall return. If the timeout parameter is a null pointer, then
94 the call to pselect() or select() shall block indefinitely until at
95 least one descriptor meets the specified criteria. To effect a poll,
96 the timeout parameter should not be a null pointer, and should point to
97 a zero-valued timespec structure.
98
99 The use of a timeout does not affect any pending timers set up by
100 alarm(), ualarm(), or setitimer().
101
102 Implementations may place limitations on the maximum timeout interval
103 supported. All implementations shall support a maximum timeout interval
104 of at least 31 days. If the timeout argument specifies a timeout inter‐
105 val greater than the implementation-defined maximum value, the maximum
106 value shall be used as the actual timeout value. Implementations may
107 also place limitations on the granularity of timeout intervals. If the
108 requested timeout interval requires a finer granularity than the imple‐
109 mentation supports, the actual timeout interval shall be rounded up to
110 the next supported value.
111
112 If sigmask is not a null pointer, then the pselect() function shall
113 replace the signal mask of the process by the set of signals pointed to
114 by sigmask before examining the descriptors, and shall restore the sig‐
115 nal mask of the process before returning.
116
117 A descriptor shall be considered ready for reading when a call to an
118 input function with O_NONBLOCK clear would not block, whether or not
119 the function would transfer data successfully. (The function might
120 return data, an end-of-file indication, or an error other than one
121 indicating that it is blocked, and in each of these cases the descrip‐
122 tor shall be considered ready for reading.)
123
124 A descriptor shall be considered ready for writing when a call to an
125 output function with O_NONBLOCK clear would not block, whether or not
126 the function would transfer data successfully.
127
128 If a socket has a pending error, it shall be considered to have an
129 exceptional condition pending. Otherwise, what constitutes an excep‐
130 tional condition is file type-specific. For a file descriptor for use
131 with a socket, it is protocol-specific except as noted below. For other
132 file types it is implementation-defined. If the operation is meaning‐
133 less for a particular file type, pselect() or select() shall indicate
134 that the descriptor is ready for read or write operations, and shall
135 indicate that the descriptor has no exceptional condition pending.
136
137 If a descriptor refers to a socket, the implied input function is the
138 recvmsg() function with parameters requesting normal and ancillary
139 data, such that the presence of either type shall cause the socket to
140 be marked as readable. The presence of out-of-band data shall be
141 checked if the socket option SO_OOBINLINE has been enabled, as out-of-
142 band data is enqueued with normal data. If the socket is currently lis‐
143 tening, then it shall be marked as readable if an incoming connection
144 request has been received, and a call to the accept() function shall
145 complete without blocking.
146
147 If a descriptor refers to a socket, the implied output function is the
148 sendmsg() function supplying an amount of normal data equal to the cur‐
149 rent value of the SO_SNDLOWAT option for the socket. If a non-blocking
150 call to the connect() function has been made for a socket, and the con‐
151 nection attempt has either succeeded or failed leaving a pending error,
152 the socket shall be marked as writable.
153
154 A socket shall be considered to have an exceptional condition pending
155 if a receive operation with O_NONBLOCK clear for the open file descrip‐
156 tion and with the MSG_OOB flag set would return out-of-band data with‐
157 out blocking. (It is protocol-specific whether the MSG_OOB flag would
158 be used to read out-of-band data.) A socket shall also be considered to
159 have an exceptional condition pending if an out-of-band data mark is
160 present in the receive queue. Other circumstances under which a socket
161 may be considered to have an exceptional condition pending are proto‐
162 col-specific and implementation-defined.
163
164 If the readfds, writefds, and errorfds arguments are all null pointers
165 and the timeout argument is not a null pointer, the pselect() or
166 select() function shall block for the time specified, or until inter‐
167 rupted by a signal. If the readfds, writefds, and errorfds arguments
168 are all null pointers and the timeout argument is a null pointer, the
169 pselect() or select() function shall block until interrupted by a sig‐
170 nal.
171
172 File descriptors associated with regular files shall always select true
173 for ready to read, ready to write, and error conditions.
174
175 On failure, the objects pointed to by the readfds, writefds, and
176 errorfds arguments shall not be modified. If the timeout interval
177 expires without the specified condition being true for any of the spec‐
178 ified file descriptors, the objects pointed to by the readfds,
179 writefds, and errorfds arguments shall have all bits set to 0.
180
181 File descriptor masks of type fd_set can be initialized and tested with
182 FD_CLR(), FD_ISSET(), FD_SET(), and FD_ZERO(). It is unspecified
183 whether each of these is a macro or a function. If a macro definition
184 is suppressed in order to access an actual function, or a program
185 defines an external identifier with any of these names, the behavior is
186 undefined.
187
188 FD_CLR(fd, fdsetp) shall remove the file descriptor fd from the set
189 pointed to by fdsetp. If fd is not a member of this set, there shall be
190 no effect on the set, nor will an error be returned.
191
192 FD_ISSET(fd, fdsetp) shall evaluate to non-zero if the file descriptor
193 fd is a member of the set pointed to by fdsetp, and shall evaluate to
194 zero otherwise.
195
196 FD_SET(fd, fdsetp) shall add the file descriptor fd to the set pointed
197 to by fdsetp. If the file descriptor fd is already in this set, there
198 shall be no effect on the set, nor will an error be returned.
199
200 FD_ZERO(fdsetp) shall initialize the descriptor set pointed to by
201 fdsetp to the null set. No error is returned if the set is not empty at
202 the time FD_ZERO() is invoked.
203
204 The behavior of these macros is undefined if the fd argument is less
205 than 0 or greater than or equal to FD_SETSIZE, or if fd is not a valid
206 file descriptor, or if any of the arguments are expressions with side
207 effects.
208
210 Upon successful completion, the pselect() and select() functions shall
211 return the total number of bits set in the bit masks. Otherwise, -1
212 shall be returned, and errno shall be set to indicate the error.
213
214 FD_CLR(), FD_SET(), and FD_ZERO() do not return a value. FD_ISSET()
215 shall return a non-zero value if the bit for the file descriptor fd is
216 set in the file descriptor set pointed to by fdset, and 0 otherwise.
217
219 Under the following conditions, pselect() and select() shall fail and
220 set errno to:
221
222 EBADF One or more of the file descriptor sets specified a file
223 descriptor that is not a valid open file descriptor.
224
225 EINTR The function was interrupted before any of the selected events
226 occurred and before the timeout interval expired.
227
228 If SA_RESTART has been set for the interrupting signal, it is implemen‐
229 tation-defined whether the function restarts or returns with [EINTR].
230
231 EINVAL An invalid timeout interval was specified.
232
233 EINVAL The nfds argument is less than 0 or greater than FD_SETSIZE.
234
235 EINVAL One of the specified file descriptors refers to a STREAM or mul‐
236 tiplexer that is linked (directly or indirectly) downstream from
237 a multiplexer.
238
239
240 The following sections are informative.
241
243 None.
244
246 None.
247
249 In previous versions of the Single UNIX Specification, the select()
250 function was defined in the <sys/time.h> header. This is now changed to
251 <sys/select.h>. The rationale for this change was as follows: the
252 introduction of the pselect() function included the <sys/select.h>
253 header and the <sys/select.h> header defines all the related defini‐
254 tions for the pselect() and select() functions. Backwards-compatibility
255 to existing XSI implementations is handled by allowing <sys/time.h> to
256 include <sys/select.h>.
257
259 None.
260
262 accept(), alarm(), connect(), fcntl(), poll(), read(), recvmsg(),
263 sendmsg(), setitimer(), ualarm(), write(), the Base Definitions volume
264 of IEEE Std 1003.1-2001, <sys/select.h>, <sys/time.h>
265
267 Portions of this text are reprinted and reproduced in electronic form
268 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
269 -- Portable Operating System Interface (POSIX), The Open Group Base
270 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
271 Electrical and Electronics Engineers, Inc and The Open Group. In the
272 event of any discrepancy between this version and the original IEEE and
273 The Open Group Standard, the original IEEE and The Open Group Standard
274 is the referee document. The original Standard can be obtained online
275 at http://www.opengroup.org/unix/online.html .
276
277
278
279IEEE/The Open Group 2003 PSELECT(3P)