]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - lib/libc/sys/select.2
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / lib / libc / sys / select.2
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 .\" 4. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)select.2    8.2 (Berkeley) 3/25/94
29 .\" $FreeBSD$
30 .\"
31 .Dd November 17, 2002
32 .Dt SELECT 2
33 .Os
34 .Sh NAME
35 .Nm select
36 .Nd synchronous I/O multiplexing
37 .Sh LIBRARY
38 .Lb libc
39 .Sh SYNOPSIS
40 .In sys/select.h
41 .Ft int
42 .Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
43 .Fn FD_SET fd &fdset
44 .Fn FD_CLR fd &fdset
45 .Fn FD_ISSET fd &fdset
46 .Fn FD_ZERO &fdset
47 .Sh DESCRIPTION
48 The
49 .Fn select
50 system call
51 examines the I/O descriptor sets whose addresses are passed in
52 .Fa readfds ,
53 .Fa writefds ,
54 and
55 .Fa exceptfds
56 to see if some of their descriptors
57 are ready for reading, are ready for writing, or have an exceptional
58 condition pending, respectively.
59 The only exceptional condition detectable is out-of-band
60 data received on a socket.
61 The first
62 .Fa nfds
63 descriptors are checked in each set;
64 i.e., the descriptors from 0 through
65 .Fa nfds Ns No -1
66 in the descriptor sets are examined.
67 On return,
68 .Fn select
69 replaces the given descriptor sets
70 with subsets consisting of those descriptors that are ready
71 for the requested operation.
72 The
73 .Fn select
74 system call
75 returns the total number of ready descriptors in all the sets.
76 .Pp
77 The descriptor sets are stored as bit fields in arrays of integers.
78 The following macros are provided for manipulating such descriptor sets:
79 .Fn FD_ZERO &fdset
80 initializes a descriptor set
81 .Fa fdset
82 to the null set.
83 .Fn FD_SET fd &fdset
84 includes a particular descriptor
85 .Fa fd
86 in
87 .Fa fdset .
88 .Fn FD_CLR fd &fdset
89 removes
90 .Fa fd
91 from
92 .Fa fdset .
93 .Fn FD_ISSET fd &fdset
94 is non-zero if
95 .Fa fd
96 is a member of
97 .Fa fdset ,
98 zero otherwise.
99 The behavior of these macros is undefined if
100 a descriptor value is less than zero or greater than or equal to
101 .Dv FD_SETSIZE ,
102 which is normally at least equal
103 to the maximum number of descriptors supported by the system.
104 .Pp
105 If
106 .Fa timeout
107 is not a null pointer, it specifies the maximum interval to wait for the
108 selection to complete.
109 System activity can lengthen the interval by
110 an indeterminate amount.
111 .Pp
112 If
113 .Fa timeout
114 is a null pointer, the select blocks indefinitely.
115 .Pp
116 To effect a poll, the
117 .Fa timeout
118 argument should not be a null pointer,
119 but it should point to a zero-valued timeval structure.
120 .Pp
121 Any of
122 .Fa readfds ,
123 .Fa writefds ,
124 and
125 .Fa exceptfds
126 may be given as null pointers if no descriptors are of interest.
127 .Sh RETURN VALUES
128 The
129 .Fn select
130 system call
131 returns the number of ready descriptors that are contained in
132 the descriptor sets,
133 or -1 if an error occurred.
134 If the time limit expires,
135 .Fn select
136 returns 0.
137 If
138 .Fn select
139 returns with an error,
140 including one due to an interrupted system call,
141 the descriptor sets will be unmodified.
142 .Sh ERRORS
143 An error return from
144 .Fn select
145 indicates:
146 .Bl -tag -width Er
147 .It Bq Er EBADF
148 One of the descriptor sets specified an invalid descriptor.
149 .It Bq Er EFAULT
150 One of the arguments
151 .Fa readfds , writefds , exceptfds ,
152 or
153 .Fa timeout
154 points to an invalid address.
155 .It Bq Er EINTR
156 A signal was delivered before the time limit expired and
157 before any of the selected events occurred.
158 .It Bq Er EINVAL
159 The specified time limit is invalid.
160 One of its components is
161 negative or too large.
162 .It Bq Er EINVAL
163 The
164 .Fa nfds
165 argument
166 was invalid.
167 .El
168 .Sh SEE ALSO
169 .Xr accept 2 ,
170 .Xr connect 2 ,
171 .Xr getdtablesize 2 ,
172 .Xr gettimeofday 2 ,
173 .Xr kqueue 2 ,
174 .Xr poll 2 ,
175 .Xr read 2 ,
176 .Xr recv 2 ,
177 .Xr send 2 ,
178 .Xr write 2 ,
179 .Xr clocks 7
180 .Sh NOTES
181 The default size of
182 .Dv FD_SETSIZE
183 is currently 1024.
184 In order to accommodate programs which might potentially
185 use a larger number of open files with
186 .Fn select ,
187 it is possible
188 to increase this size by having the program define
189 .Dv FD_SETSIZE
190 before the inclusion of any header which includes
191 .In sys/types.h .
192 .Pp
193 If
194 .Fa nfds
195 is greater than the number of open files,
196 .Fn select
197 is not guaranteed to examine the unused file descriptors.
198 For historical
199 reasons,
200 .Fn select
201 will always examine the first 256 descriptors.
202 .Sh STANDARDS
203 The
204 .Fn select
205 system call and
206 .Fn FD_CLR ,
207 .Fn FD_ISSET ,
208 .Fn FD_SET ,
209 and
210 .Fn FD_ZERO
211 macros conform with
212 .St -p1003.1-2001 .
213 .Sh HISTORY
214 The
215 .Fn select
216 system call appeared in
217 .Bx 4.2 .
218 .Sh BUGS
219 .St -susv2
220 allows systems to modify the original timeout in place.
221 Thus, it is unwise to assume that the timeout value will be unmodified
222 by the
223 .Fn select
224 system call.
225 .Fx
226 does not modify the return value, which can cause problems for applications
227 ported from other systems.