SELECT(2) UNIX Programmer's Manual SELECT(2) NAME pselect, select - synchronous I/O multiplexing SYNOPSIS #include #include #include #include nfound = pselect(nfds, readfds, writefds, exceptfds, timeout, sigmask); int nfound, nfds; fd_set *readfds, *writefds, *exceptfds; struct timespec *timeout; sigset_t *sigmask; nfound = select(nfds, readfds, writefds, exceptfds, timeout) int nfound, nfds; fd_set *readfds, *writefds, *exceptfds; struct timeval *timeout; FD_SET(fd, &fdset) FD_CLR(fd, &fdset) FD_ISSET(fd, &fdset) FD_ZERO(&fdset) int fd; fd_set fdset; DESCRIPTION _P_s_e_l_e_c_t and _s_e_l_e_c_t examine the I/O descriptor sets whose addresses are passed in _r_e_a_d_f_d_s, _w_r_i_t_e_f_d_s, and _e_x_c_e_p_t_f_d_s to see if some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively. The two functions are identical except for the type and format of the _t_i_m_e_o_u_t value, and the additonal _s_i_g_m_a_s_k parameter supplied to the _p_s_e_l_e_c_t() call. The first _n_f_d_s descriptors are checked in each set; i.e. the descriptors from 0 through _n_f_d_s-1 in the descriptor sets are examined. On return, _s_e_l_e_c_t replaces the given descriptor sets with subsets consisting of those descriptors that are ready for the requested operation. The total number of ready descriptors in all the sets is returned in _n_f_o_u_n_d. The descriptor sets are stored as bit fields in arrays of integers. The following macros are provided for manipulat- ing such descriptor sets: _F_D__Z_E_R_O(&_f_d_s_e_t) initializes a descriptor set _f_d_s_e_t to the null set. _F_D__S_E_T(_f_d, &_f_d_s_e_t) includes a particular descriptor _f_d in _f_d_s_e_t. _F_D__C_L_R(_f_d, &_f_d_s_e_t) removes _f_d from _f_d_s_e_t. _F_D__I_S_S_E_T(_f_d, &_f_d_s_e_t) is nonzero if _f_d is a member of _f_d_s_e_t, zero otherwise. The behavior of these macros is undefined if a descriptor value is less than zero or greater than or equal to _F_D__S_E_T_S_I_Z_E, Printed 3/4/100 March 4, 2000 1 SELECT(2) UNIX Programmer's Manual SELECT(2) which is normally at least equal to the maximum number of descriptors supported by the system. If _t_i_m_e_o_u_t is a non-zero pointer, it specifies a maximum interval to wait for the selection to complete. If _t_i_m_e_o_u_t is a zero pointer, _s_e_l_e_c_t blocks indefinitely. To affect a poll, the _t_i_m_e_o_u_t argument should be non-zero, pointing to a zero-valued timeval structure. If the _s_i_g_m_a_s_k parameter to _p_s_e_l_e_c_t() is not NULL, it points to a signal mask that replaces the previous signal mask for the process for the duration of the call, and the previous mask is restored upon return; see sigprocmask(3). This is normally used so that signals can be blocked while preparing for a call to _p_s_e_l_e_c_t() and then atomically unblocking the signals while selecting. Any of _r_e_a_d_f_d_s, _w_r_i_t_e_f_d_s, and _e_x_c_e_p_t_f_d_s may be given as zero pointers if no descriptors are of interest. RETURN VALUE _S_e_l_e_c_t returns the number of ready descriptors that are con- tained in the descriptor sets, or -1 if an error occurred. If the time limit expires then _s_e_l_e_c_t returns 0. If _s_e_l_e_c_t returns with an error, including one due to an interrupted call, the descriptor sets will be unmodified. ERRORS An error return from _s_e_l_e_c_t indicates: [EBADF] One of the descriptor sets specified an invalid descriptor. [EINTR] A signal was delivered before the time limit expired and before any of the selected events occurred. [EINVAL] The specified time limit is invalid. One of its components is negative or too large. SEE ALSO accept(2), connect(2), read(2), write(2), recv(2), send(2), getdtablesize(2) BUGS Although the provision of _g_e_t_d_t_a_b_l_e_s_i_z_e(2) was intended to allow user programs to be written independent of the kernel limit on the number of open files, the dimension of a suffi- ciently large bit field for select remains a problem. The default size FD_SETSIZE (currently 256) is somewhat larger than the current kernel limit to the number of open files. However, in order to accommodate programs which might Printed 3/4/100 March 4, 2000 2 SELECT(2) UNIX Programmer's Manual SELECT(2) potentially use a larger number of open files with select, it is possible to increase this size within a program by providing a larger definition of FD_SETSIZE before the inclusion of . _S_e_l_e_c_t should probably return the time remaining from the original timeout, if any, by modifying the time value in place. This may be implemented in future versions of the system. Thus, it is unwise to assume that the timeout value will be unmodified by the _s_e_l_e_c_t call. In 2BSD the timeout is implemented in the kernel using the _c_a_l_l_o_u_t table. Since a _c_a_l_l_o_u_t structure only has a signed short to store the number of ticks till expiration the max- imum value of a kernel timeout is 32767 ticks. In the US (60hz power) this gives a maximum timeout of approximately 9 minutes. In countries using 50hz power the maximum timeout is about 13 minutes. _s_t_r_u_c_t _t_i_m_e_s_p_e_c on a PDP-11 is silly since the hardware has nowhere near microsecond much less nanosecond clock resolu- tion. Printed 3/4/100 March 4, 2000 3