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.\" $FreeBSD: src/lib/libc/sys/select.2,v 1.14.2.5 2001/12/14 18:34:01 ru Exp $ 34.\" 35.Dd March 25, 1994 36.Dt SELECT 2 37.Os 38.Sh NAME 39.Nm select 40.Nd synchronous I/O multiplexing 41.Sh LIBRARY 42.Lb libc 43.Sh SYNOPSIS 44.In sys/types.h 45.In sys/time.h 46.In unistd.h 47.Ft int 48.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout" 49.Fn FD_SET fd &fdset 50.Fn FD_CLR fd &fdset 51.Fn FD_ISSET fd &fdset 52.Fn FD_ZERO &fdset 53.Sh DESCRIPTION 54.Fn Select 55examines the I/O descriptor sets whose addresses are passed in 56.Fa readfds , 57.Fa writefds , 58and 59.Fa exceptfds 60to see if some of their descriptors 61are ready for reading, are ready for writing, or have an exceptional 62condition pending, respectively. 63The only exceptional condition detectable is out-of-band 64data received on a socket. 65The first 66.Fa nfds 67descriptors are checked in each set; 68i.e., the descriptors from 0 through 69.Fa nfds Ns No -1 70in the descriptor sets are examined. 71On return, 72.Fn select 73replaces the given descriptor sets 74with subsets consisting of those descriptors that are ready 75for the requested operation. 76.Fn Select 77returns the total number of ready descriptors in all the sets. 78.Pp 79The descriptor sets are stored as bit fields in arrays of integers. 80The following macros are provided for manipulating such descriptor sets: 81.Fn FD_ZERO &fdset 82initializes a descriptor set 83.Fa fdset 84to the null set. 85.Fn FD_SET fd &fdset 86includes a particular descriptor 87.Fa fd 88in 89.Fa fdset . 90.Fn FD_CLR fd &fdset 91removes 92.Fa fd 93from 94.Fa fdset . 95.Fn FD_ISSET fd &fdset 96is non-zero if 97.Fa fd 98is a member of 99.Fa fdset , 100zero otherwise. 101The behavior of these macros is undefined if 102a descriptor value is less than zero or greater than or equal to 103.Dv FD_SETSIZE , 104which is normally at least equal 105to the maximum number of descriptors supported by the system. 106.Pp 107If 108.Fa timeout 109is a non-nil pointer, it specifies the maximum interval to wait for the 110selection to complete. System activity can lengthen the interval by 111an indeterminate amount. 112.Pp 113If 114.Fa timeout 115is a nil pointer, the select blocks indefinitely. 116.Pp 117To effect a poll, the 118.Fa timeout 119argument should be non-nil, pointing to a zero-valued timeval structure. 120.Pp 121Any of 122.Fa readfds , 123.Fa writefds , 124and 125.Fa exceptfds 126may be given as nil pointers if no descriptors are of interest. 127.Sh RETURN VALUES 128.Fn Select 129returns the number of ready descriptors that are contained in 130the descriptor sets, 131or -1 if an error occurred. 132If the time limit expires, 133.Fn select 134returns 0. 135If 136.Fn select 137returns with an error, 138including one due to an interrupted call, 139the descriptor sets will be unmodified. 140.Sh ERRORS 141An error return from 142.Fn select 143indicates: 144.Bl -tag -width Er 145.It Bq Er EBADF 146One of the descriptor sets specified an invalid descriptor. 147.It Bq Er EINTR 148A signal was delivered before the time limit expired and 149before any of the selected events occurred. 150.It Bq Er EINVAL 151The specified time limit is invalid. One of its components is 152negative or too large. 153.It Bq Er EINVAL 154.Fa nfds 155was invalid. 156.El 157.Sh SEE ALSO 158.Xr accept 2 , 159.Xr connect 2 , 160.Xr getdtablesize 2 , 161.Xr gettimeofday 2 , 162.Xr read 2 , 163.Xr recv 2 , 164.Xr send 2 , 165.Xr write 2 , 166.Xr clocks 7 167.Sh NOTES 168The default size of 169.Dv FD_SETSIZE 170is currently 1024. 171In order to accommodate programs which might potentially 172use a larger number of open files with 173.Fn select , 174it is possible 175to increase this size by having the program define 176.Dv FD_SETSIZE 177before the inclusion of any header which includes 178.Aq Pa sys/types.h . 179.Pp 180If 181.Fa nfds 182is greater than the number of open files, 183.Fn select 184is not guaranteed to examine the unused file descriptors. For historical 185reasons, 186.Fn select 187will always examine the first 256 descriptors. 188.Sh BUGS 189.St -susv2 190allows systems to modify the original timeout in place. 191Thus, it is unwise to assume that the timeout value will be unmodified 192by the 193.Fn select 194call. 195.Sh HISTORY 196The 197.Fn select 198function call appeared in 199.Bx 4.2 . 200