1.\" $NetBSD: select.2,v 1.9 1997/11/01 06:37:49 mycroft Exp $ 2.\" 3.\" Copyright (c) 1983, 1991, 1993 4.\" The Regents of the University of California. All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 3. All advertising materials mentioning features or use of this software 15.\" must display the following acknowledgement: 16.\" This product includes software developed by the University of 17.\" California, Berkeley and its contributors. 18.\" 4. Neither the name of the University nor the names of its contributors 19.\" may be used to endorse or promote products derived from this software 20.\" without specific prior written permission. 21.\" 22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32.\" SUCH DAMAGE. 33.\" 34.\" @(#)select.2 8.2 (Berkeley) 3/25/94 35.\" 36.Dd March 25, 1994 37.Dt SELECT 2 38.Os BSD 4.2 39.Sh NAME 40.Nm select 41.Nd synchronous I/O multiplexing 42.Sh SYNOPSIS 43.Fd #include <sys/types.h> 44.Fd #include <sys/time.h> 45.Fd #include <unistd.h> 46.Ft int 47.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout" 48.Fn FD_SET fd &fdset 49.Fn FD_CLR fd &fdset 50.Fn FD_ISSET fd &fdset 51.Fn FD_ZERO &fdset 52.Sh DESCRIPTION 53.Fn Select 54examines the I/O descriptor sets whose addresses are passed in 55.Fa readfds , 56.Fa writefds , 57and 58.Fa exceptfds 59to see if some of their descriptors 60are ready for reading, are ready for writing, or have an exceptional 61condition pending, respectively. 62The first 63.Fa nfds 64descriptors are checked in each set; 65i.e., the descriptors from 0 through 66.Fa nfds Ns No -1 67in the descriptor sets are examined. 68On return, 69.Fn select 70replaces the given descriptor sets 71with subsets consisting of those descriptors that are ready 72for the requested operation. 73.Fn Select 74returns the total number of ready descriptors in all the sets. 75.Pp 76The descriptor sets are stored as bit fields in arrays of integers. 77The following macros are provided for manipulating such descriptor sets: 78.Fn FD_ZERO &fdset 79initializes a descriptor set 80.Fa fdset 81to the null set. 82.Fn FD_SET fd &fdset 83includes a particular descriptor 84.Fa fd 85in 86.Fa fdset . 87.Fn FD_CLR fd &fdset 88removes 89.Fa fd 90from 91.Fa fdset . 92.Fn FD_ISSET fd &fdset 93is non-zero if 94.Fa fd 95is a member of 96.Fa fdset , 97zero otherwise. 98The behavior of these macros is undefined if 99a descriptor value is less than zero or greater than or equal to 100.Dv FD_SETSIZE , 101which is normally at least equal 102to the maximum number of descriptors supported by the system. 103.Pp 104If 105.Fa timeout 106is a non-nil pointer, it specifies a maximum interval to wait for the 107selection to complete. If 108.Fa timeout 109is a nil pointer, the select blocks indefinitely. To affect a poll, the 110.Fa timeout 111argument should be non-nil, pointing to a zero-valued timeval structure. 112.Pp 113Any of 114.Fa readfds , 115.Fa writefds , 116and 117.Fa exceptfds 118may be given as nil pointers if no descriptors are of interest. 119.Sh RETURN VALUES 120.Fn Select 121returns the number of ready descriptors that are contained in 122the descriptor sets, 123or -1 if an error occurred. 124If the time limit expires, 125.Fn select 126returns 0. 127If 128.Fn select 129returns with an error, 130including one due to an interrupted call, 131the descriptor sets will be unmodified. 132.Sh ERRORS 133An error return from 134.Fn select 135indicates: 136.Bl -tag -width Er 137.It Bq Er EFAULT 138One or more of 139.Fa readfds , 140.Fa writefds , 141or 142.Fa exceptfds 143points outside the process's allocated address space. 144.It Bq Er EBADF 145One of the descriptor sets specified an invalid descriptor. 146.It Bq Er EINTR 147A signal was delivered before the time limit expired and 148before any of the selected events occurred. 149.It Bq Er EINVAL 150The specified time limit is invalid. One of its components is 151negative or too large. 152.El 153.Sh SEE ALSO 154.Xr accept 2 , 155.Xr connect 2 , 156.Xr gettimeofday 2 , 157.Xr poll 2 , 158.Xr read 2 , 159.Xr recv 2 , 160.Xr send 2 , 161.Xr write 2 , 162.Xr getdtablesize 3 163.Sh BUGS 164Although the provision of 165.Xr getdtablesize 3 166was intended to allow user programs to be written independent 167of the kernel limit on the number of open files, the dimension 168of a sufficiently large bit field for select remains a problem. 169The default size 170.Dv FD_SETSIZE 171(currently 256) is somewhat larger than 172the current kernel limit to the number of open files. 173However, in order to accommodate programs which might potentially 174use a larger number of open files with select, it is possible 175to increase this size within a program by providing 176a larger definition of 177.Dv FD_SETSIZE 178before the inclusion of 179.Aq Pa sys/types.h . 180.Pp 181.Fn Select 182should probably return the time remaining from the original timeout, 183if any, by modifying the time value in place. 184This may be implemented in future versions of the system. 185Thus, it is unwise to assume that the timeout value will be unmodified 186by the 187.Fn select 188call. 189.Sh HISTORY 190The 191.Fn select 192function call appeared in 193.Bx 4.2 . 194