xref: /netbsd-src/lib/libc/sys/select.2 (revision 38023541164cff097d5fadec63134189b1453b8c)
1.\" Copyright (c) 1983, 1991 The Regents of the University of California.
2.\" 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.\"     from: @(#)select.2	6.8 (Berkeley) 3/10/91
33.\"	$Id: select.2,v 1.3 1993/11/25 00:39:33 jtc Exp $
34.\"
35.Dd March 10, 1991
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 <unistd.h>
43.Fd #include <sys/types.h>
44.Fd #include <sys/time.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 first
62.Fa nfds
63descriptors are checked in each set;
64i.e., the descriptors from 0 through
65.Fa nfds Ns No -1
66in the descriptor sets are examined.
67On return,
68.Fn select
69replaces the given descriptor sets
70with subsets consisting of those descriptors that are ready
71for the requested operation.
72.Fn Select
73returns the total number of ready descriptors in all the sets.
74.Pp
75The descriptor sets are stored as bit fields in arrays of integers.
76The following macros are provided for manipulating such descriptor sets:
77.Fn FD_ZERO &fdsetx
78initializes a descriptor set
79.Fa fdset
80to the null set.
81.Fn FD_SET fd &fdset
82includes a particular descriptor
83.Fa fd
84in
85.Fa fdset .
86.Fn FD_CLR fd &fdset
87removes
88.Fa fd
89from
90.Fa fdset .
91.Fn FD_ISSET fd &fdset
92is non-zero if
93.Fa fd
94is a member of
95.Fa fdset ,
96zero otherwise.
97The behavior of these macros is undefined if
98a descriptor value is less than zero or greater than or equal to
99.Dv FD_SETSIZE ,
100which is normally at least equal
101to the maximum number of descriptors supported by the system.
102.Pp
103If
104.Fa timeout
105is a non-nil pointer, it specifies a maximum interval to wait for the
106selection to complete.  If
107.Fa timeout
108is a nil pointer, the select blocks indefinitely.  To affect a poll, the
109.Fa timeout
110argument should be non-nil, pointing to a zero-valued timeval structure.
111.Pp
112Any of
113.Fa readfds ,
114.Fa writefds ,
115and
116.Fa exceptfds
117may be given as nil pointers if no descriptors are of interest.
118.Sh RETURN VALUES
119.Fn Select
120returns the number of ready descriptors that are contained in
121the descriptor sets,
122or -1 if an error occurred.
123If the time limit expires,
124.Fn select
125returns 0.
126If
127.Fn select
128returns with an error,
129including one due to an interrupted call,
130the descriptor sets will be unmodified.
131.Sh ERRORS
132An error return from
133.Fn select
134indicates:
135.Bl -tag -width Er
136.It Bq Er EBADF
137One of the descriptor sets specified an invalid descriptor.
138.It Bq Er EINTR
139A signal was delivered before the time limit expired and
140before any of the selected events occurred.
141.It Bq Er EINVAL
142The specified time limit is invalid.  One of its components is
143negative or too large.
144.El
145.Sh SEE ALSO
146.Xr accept 2 ,
147.Xr connect 2 ,
148.Xr read 2 ,
149.Xr write 2 ,
150.Xr recv 2 ,
151.Xr send 2 ,
152.Xr getdtablesize 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.Fn select
182function call appeared in
183.Bx 4.2 .
184