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