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