xref: /freebsd-12.1/lib/libc/sys/select.2 (revision 411d727c)
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 November 17, 2002
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/select.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
52The
53.Fn select
54system call
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.
76The
77.Fn select
78system call
79returns the total number of ready descriptors in all the sets.
80.Pp
81The descriptor sets are stored as bit fields in arrays of integers.
82The following macros are provided for manipulating such descriptor sets:
83.Fn FD_ZERO &fdset
84initializes a descriptor set
85.Fa fdset
86to the null set.
87.Fn FD_SET fd &fdset
88includes a particular descriptor
89.Fa fd
90in
91.Fa fdset .
92.Fn FD_CLR fd &fdset
93removes
94.Fa fd
95from
96.Fa fdset .
97.Fn FD_ISSET fd &fdset
98is non-zero if
99.Fa fd
100is a member of
101.Fa fdset ,
102zero otherwise.
103The behavior of these macros is undefined if
104a descriptor value is less than zero or greater than or equal to
105.Dv FD_SETSIZE ,
106which is normally at least equal
107to the maximum number of descriptors supported by the system.
108.Pp
109If
110.Fa timeout
111is not a
112.Dv NULL
113pointer, it specifies the maximum interval to wait for the
114selection to complete.
115System activity can lengthen the interval by
116an indeterminate amount.
117.Pp
118If
119.Fa timeout
120is a
121.Dv NULL
122pointer, the select blocks indefinitely.
123.Pp
124To effect a poll, the
125.Fa timeout
126argument should not be
127.Dv NULL ,
128but it should point to a zero-valued timeval structure.
129.Pp
130Any of
131.Fa readfds ,
132.Fa writefds ,
133and
134.Fa exceptfds
135may be given as
136.Dv NULL
137pointers if no descriptors are of interest.
138.Sh RETURN VALUES
139The
140.Fn select
141system call
142returns the number of ready descriptors that are contained in
143the descriptor sets,
144or -1 if an error occurred.
145If the time limit expires,
146.Fn select
147returns 0.
148If
149.Fn select
150returns with an error,
151including one due to an interrupted system call,
152the descriptor sets will be unmodified.
153.Sh ERRORS
154An error return from
155.Fn select
156indicates:
157.Bl -tag -width Er
158.It Bq Er EBADF
159One of the descriptor sets specified an invalid descriptor.
160.It Bq Er EFAULT
161One of the arguments
162.Fa readfds , writefds , exceptfds ,
163or
164.Fa timeout
165points to an invalid address.
166.It Bq Er EINTR
167A signal was delivered before the time limit expired and
168before any of the selected events occurred.
169.It Bq Er EINVAL
170The specified time limit is invalid.
171One of its components is
172negative or too large.
173.It Bq Er EINVAL
174The
175.Fa nfds
176argument
177was invalid.
178.El
179.Sh SEE ALSO
180.Xr accept 2 ,
181.Xr connect 2 ,
182.Xr getdtablesize 2 ,
183.Xr gettimeofday 2 ,
184.Xr kqueue 2 ,
185.Xr poll 2 ,
186.Xr read 2 ,
187.Xr recv 2 ,
188.Xr send 2 ,
189.Xr write 2 ,
190.Xr clocks 7
191.Sh NOTES
192The default size of
193.Dv FD_SETSIZE
194is currently 1024.
195In order to accommodate programs which might potentially
196use a larger number of open files with
197.Fn select ,
198it is possible
199to increase this size by having the program define
200.Dv FD_SETSIZE
201before the inclusion of any header which includes
202.In sys/types.h .
203.Pp
204If
205.Fa nfds
206is greater than the number of open files,
207.Fn select
208is not guaranteed to examine the unused file descriptors.
209For historical
210reasons,
211.Fn select
212will always examine the first 256 descriptors.
213.Sh BUGS
214.St -susv2
215allows systems to modify the original timeout in place.
216Thus, it is unwise to assume that the timeout value will be unmodified
217by the
218.Fn select
219system call.
220.Sh STANDARDS
221The
222.Fn select
223system call and
224.Fn FD_CLR ,
225.Fn FD_ISSET ,
226.Fn FD_SET ,
227and
228.Fn FD_ZERO
229macros conform with
230.St -p1003.1-2001 .
231.Sh HISTORY
232The
233.Fn select
234system call appeared in
235.Bx 4.2 .
236