]> git.saurik.com Git - apple/xnu.git/blame - bsd/man/man2/select.2
xnu-792.13.8.tar.gz
[apple/xnu.git] / bsd / man / man2 / select.2
CommitLineData
9bccf70c
A
1.\" $NetBSD: select.2,v 1.5 1995/06/27 22:32:28 cgd Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\" The Regents of the University of California. All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\" notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\" notice, this list of conditions and the following disclaimer in the
13.\" documentation and/or other materials provided with the distribution.
14.\" 3. All advertising materials mentioning features or use of this software
15.\" must display the following acknowledgement:
16.\" This product includes software developed by the University of
17.\" California, Berkeley and its contributors.
18.\" 4. Neither the name of the University nor the names of its contributors
19.\" may be used to endorse or promote products derived from this software
20.\" without specific prior written permission.
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32.\" SUCH DAMAGE.
33.\"
34.\" @(#)select.2 8.2 (Berkeley) 3/25/94
35.\"
36.Dd March 25, 1994
37.Dt SELECT 2
38.Os BSD 4.2
39.Sh NAME
40.Nm select
41.Nd synchronous I/O multiplexing
42.Sh SYNOPSIS
55e303ae
A
43.Fd #include <sys/select.h>
44.D1 "- or -"
9bccf70c
A
45.Fd #include <sys/types.h>
46.Fd #include <sys/time.h>
47.Fd #include <unistd.h>
48.Ft int
49.Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
50.Fn FD_SET fd &fdset
51.Fn FD_CLR fd &fdset
52.Fn FD_ISSET fd &fdset
91447636 53.Fn FD_COPY &fdset_orig &fdset_copy
9bccf70c
A
54.Fn FD_ZERO &fdset
55.Sh DESCRIPTION
56.Fn Select
57examines the I/O descriptor sets whose addresses are passed in
58.Fa readfds ,
59.Fa writefds ,
60and
61.Fa exceptfds
62to see if some of their descriptors
63are ready for reading, are ready for writing, or have an exceptional
64condition pending, respectively.
65The first
66.Fa nfds
67descriptors are checked in each set;
68i.e., the descriptors from 0 through
69.Fa nfds Ns No -1
91447636
A
70in the descriptor sets are examined. (Example: If you have set two file descriptors "4" and "17",
71.Fa nfds
72should not be "2", but rather "17 + 1" or "18".)
9bccf70c
A
73On return,
74.Fn select
75replaces the given descriptor sets
76with subsets consisting of those descriptors that are ready
77for the requested operation.
78.Fn Select
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.
91447636
A
103.Fn FD_COPY &fdset_orig &fdset_copy
104replaces an already allocated
105.Fa &fdset_copy
106file descriptor set with a copy of
107.Fa &fdset_orig .
9bccf70c
A
108The behavior of these macros is undefined if
109a descriptor value is less than zero or greater than or equal to
110.Dv FD_SETSIZE ,
111which is normally at least equal
112to the maximum number of descriptors supported by the system.
113.Pp
114If
115.Fa timeout
116is a non-nil pointer, it specifies a maximum interval to wait for the
117selection to complete. If
118.Fa timeout
119is a nil pointer, the select blocks indefinitely. To effect a poll, the
120.Fa timeout
121argument should be non-nil, pointing to a zero-valued timeval structure.
122.Fa Timeout
123is not changed by
124.Fn select ,
125and may be reused on subsequent calls, however it is good style to re-initialize
126it before each invocation of
127.Fn select .
128.Pp
129Any of
130.Fa readfds ,
131.Fa writefds ,
132and
133.Fa exceptfds
134may be given as nil pointers if no descriptors are of interest.
135.Sh RETURN VALUES
136.Fn Select
137returns the number of ready descriptors that are contained in
138the descriptor sets,
139or -1 if an error occurred.
140If the time limit expires,
141.Fn select
142returns 0.
143If
144.Fn select
145returns with an error,
146including one due to an interrupted call,
147the descriptor sets will be unmodified.
148.Sh ERRORS
149An error return from
150.Fn select
151indicates:
152.Bl -tag -width Er
153.It Bq Er EBADF
154One of the descriptor sets specified an invalid descriptor.
155.It Bq Er EINTR
156A signal was delivered before the time limit expired and
157before any of the selected events occurred.
158.It Bq Er EINVAL
159The specified time limit is invalid. One of its components is
160negative or too large.
161.El
162.Sh SEE ALSO
163.Xr accept 2 ,
164.Xr connect 2 ,
165.Xr getdtablesize 2 ,
166.Xr gettimeofday 2 ,
167.Xr read 2 ,
168.Xr recv 2 ,
169.Xr send 2 ,
170.Xr write 2
171.Sh BUGS
172Although the provision of
173.Xr getdtablesize 2
174was intended to allow user programs to be written independent
175of the kernel limit on the number of open files, the dimension
176of a sufficiently large bit field for select remains a problem.
177The default size
178.Dv FD_SETSIZE
179(currently 1024) is somewhat smaller than
180the current kernel limit to the number of open files.
181However, in order to accommodate programs which might potentially
182use a larger number of open files with select, it is possible
183to increase this size within a program by providing
184a larger definition of
185.Dv FD_SETSIZE
186before the inclusion of
187.Aq Pa sys/types.h .
188.Pp
189.Fn Select
190should probably have been designed to return the time remaining from the
191original timeout, if any, by modifying the time value in place.
192However, it is unlikely this semantic will ever be implemented, as the
193change would cause source code compatibility problems.
194In general it is unwise to assume that the timeout value will be
195unmodified by the
196.Fn select
197call, and the caller should reinitialize it on each invocation.
198.Sh HISTORY
199The
200.Fn select
201function call appeared in
202.Bx 4.2 .