BSD 4_4_Lite1 release
[unix-history] / usr / src / lib / libc / sys / select.2
CommitLineData
3f6f0ccf
KB
1.\" Copyright (c) 1983, 1991, 1993
2.\" The Regents of the University of California. All rights reserved.
0f4ff25c 3.\"
ad787160
C
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.
da268306 19.\"
ad787160
C
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.
0f4ff25c 31.\"
ed554bc5 32.\" @(#)select.2 8.2 (Berkeley) 3/25/94
ad787160 33.\"
ed554bc5 34.Dd March 25, 1994
931b8415
CL
35.Dt SELECT 2
36.Os BSD 4.2
37.Sh NAME
38.Nm select
39.Nd synchronous I/O multiplexing
40.Sh SYNOPSIS
931b8415
CL
41.Fd #include <sys/types.h>
42.Fd #include <sys/time.h>
71ef2e1d 43.Fd #include <unistd.h>
931b8415
CL
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
3dc1a400 52examines the I/O descriptor sets whose addresses are passed in
931b8415
CL
53.Fa readfds ,
54.Fa writefds ,
0f4ff25c 55and
931b8415 56.Fa exceptfds
71f64171
DS
57to see if some of their descriptors
58are ready for reading, are ready for writing, or have an exceptional
0f4ff25c 59condition pending, respectively.
71f64171 60The first
931b8415 61.Fa nfds
71f64171 62descriptors are checked in each set;
931b8415
CL
63i.e., the descriptors from 0 through
64.Fa nfds Ns No -1
71f64171
DS
65in the descriptor sets are examined.
66On return,
931b8415 67.Fn select
71f64171 68replaces the given descriptor sets
3dc1a400
MK
69with subsets consisting of those descriptors that are ready
70for the requested operation.
931b8415
CL
71.Fn Select
72returns the total number of ready descriptors in all the sets.
73.Pp
3dc1a400
MK
74The descriptor sets are stored as bit fields in arrays of integers.
75The following macros are provided for manipulating such descriptor sets:
931b8415 76.Fn FD_ZERO &fdsetx
71f64171 77initializes a descriptor set
931b8415 78.Fa fdset
71f64171 79to the null set.
931b8415 80.Fn FD_SET fd &fdset
71f64171 81includes a particular descriptor
931b8415 82.Fa fd
71f64171 83in
931b8415
CL
84.Fa fdset .
85.Fn FD_CLR fd &fdset
71f64171 86removes
931b8415 87.Fa fd
71f64171 88from
931b8415
CL
89.Fa fdset .
90.Fn FD_ISSET fd &fdset
91is non-zero if
92.Fa fd
71f64171 93is a member of
931b8415 94.Fa fdset ,
71f64171
DS
95zero otherwise.
96The behavior of these macros is undefined if
97a descriptor value is less than zero or greater than or equal to
931b8415 98.Dv FD_SETSIZE ,
71f64171
DS
99which is normally at least equal
100to the maximum number of descriptors supported by the system.
931b8415 101.Pp
0f4ff25c 102If
931b8415
CL
103.Fa timeout
104is a non-nil pointer, it specifies a maximum interval to wait for the
0f4ff25c 105selection to complete. If
931b8415
CL
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
0f4ff25c 111Any of
931b8415
CL
112.Fa readfds ,
113.Fa writefds ,
0f4ff25c 114and
931b8415
CL
115.Fa exceptfds
116may be given as nil pointers if no descriptors are of interest.
117.Sh RETURN VALUES
118.Fn Select
71f64171
DS
119returns the number of ready descriptors that are contained in
120the descriptor sets,
931b8415
CL
121or -1 if an error occurred.
122If the time limit expires,
123.Fn select
0f4ff25c 124returns 0.
3dc1a400 125If
931b8415 126.Fn select
3dc1a400
MK
127returns with an error,
128including one due to an interrupted call,
129the descriptor sets will be unmodified.
931b8415
CL
130.Sh ERRORS
131An error return from
132.Fn select
133indicates:
134.Bl -tag -width Er
135.It Bq Er EBADF
71f64171 136One of the descriptor sets specified an invalid descriptor.
931b8415 137.It Bq Er EINTR
da051635
SS
138A signal was delivered before the time limit expired and
139before any of the selected events occurred.
931b8415 140.It Bq Er EINVAL
71f64171 141The specified time limit is invalid. One of its components is
da051635 142negative or too large.
931b8415
CL
143.El
144.Sh SEE ALSO
145.Xr accept 2 ,
146.Xr connect 2 ,
71ef2e1d
KB
147.Xr getdtablesize 2 ,
148.Xr gettimeofday 2 ,
931b8415 149.Xr read 2 ,
931b8415
CL
150.Xr recv 2 ,
151.Xr send 2 ,
71ef2e1d 152.Xr write 2
931b8415 153.Sh BUGS
3dc1a400 154Although the provision of
931b8415 155.Xr getdtablesize 2
3dc1a400
MK
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.
931b8415
CL
159The default size
160.Dv FD_SETSIZE
161(currently 256) is somewhat larger than
3dc1a400 162the current kernel limit to the number of open files.
a5c56ebf 163However, in order to accommodate programs which might potentially
3dc1a400
MK
164use a larger number of open files with select, it is possible
165to increase this size within a program by providing
931b8415
CL
166a larger definition of
167.Dv FD_SETSIZE
168before the inclusion of
169.Aq Pa sys/types.h .
170.Pp
171.Fn Select
3dc1a400
MK
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
931b8415 177.Fn select
3dc1a400 178call.
931b8415
CL
179.Sh HISTORY
180The
181.Nm
182function call appeared in
183.Bx 4.2 .