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