de71b179e921d8ec97c64d757a200ae6e8a95a5e
[dragonfly.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 .\" 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    8.2 (Berkeley) 3/25/94
33 .\" $FreeBSD: src/lib/libc/sys/select.2,v 1.14.2.5 2001/12/14 18:34:01 ru Exp $
34 .\" $DragonFly: src/lib/libc/sys/select.2,v 1.8 2007/08/02 00:21:54 swildner Exp $
35 .\"
36 .Dd March 25, 1994
37 .Dt SELECT 2
38 .Os
39 .Sh NAME
40 .Nm select
41 .Nd synchronous I/O multiplexing
42 .Sh LIBRARY
43 .Lb libc
44 .Sh SYNOPSIS
45 .In sys/select.h
46 .Ft int
47 .Fn select "int nfds" "fd_set *readfds" "fd_set *writefds" "fd_set *exceptfds" "struct timeval *timeout"
48 .Fn FD_SET fd &fdset
49 .Fn FD_CLR fd &fdset
50 .Fn FD_ISSET fd &fdset
51 .Fn FD_ZERO &fdset
52 .Sh DESCRIPTION
53 .Fn Select
54 examines the I/O descriptor sets whose addresses are passed in
55 .Fa readfds ,
56 .Fa writefds ,
57 and
58 .Fa exceptfds
59 to see if some of their descriptors
60 are ready for reading, are ready for writing, or have an exceptional
61 condition pending, respectively.
62 The only exceptional condition detectable is out-of-band
63 data received on a socket.
64 The first
65 .Fa nfds
66 descriptors are checked in each set;
67 i.e., the descriptors from 0 through
68 .Fa nfds Ns No -1
69 in the descriptor sets are examined.
70 On return,
71 .Fn select
72 replaces the given descriptor sets
73 with subsets consisting of those descriptors that are ready
74 for the requested operation.
75 .Fn Select
76 returns the total number of ready descriptors in all the sets.
77 .Pp
78 The descriptor sets are stored as bit fields in arrays of integers.
79 The following macros are provided for manipulating such descriptor sets:
80 .Fn FD_ZERO &fdset
81 initializes a descriptor set
82 .Fa fdset
83 to the null set.
84 .Fn FD_SET fd &fdset
85 includes a particular descriptor
86 .Fa fd
87 in
88 .Fa fdset .
89 .Fn FD_CLR fd &fdset
90 removes
91 .Fa fd
92 from
93 .Fa fdset .
94 .Fn FD_ISSET fd &fdset
95 is non-zero if
96 .Fa fd
97 is a member of
98 .Fa fdset ,
99 zero otherwise.
100 The behavior of these macros is undefined if
101 a descriptor value is less than zero or greater than or equal to
102 .Dv FD_SETSIZE ,
103 which is normally at least equal
104 to the maximum number of descriptors supported by the system.
105 .Pp
106 If
107 .Fa timeout
108 is a non-nil pointer, it specifies the maximum interval to wait for the
109 selection to complete.  System activity can lengthen the interval by
110 an indeterminate amount.
111 .Pp
112 If
113 .Fa timeout
114 is a nil pointer, the select blocks indefinitely.
115 .Pp
116 To effect a poll, the
117 .Fa timeout
118 argument should be non-nil, pointing to a zero-valued timeval structure.
119 .Pp
120 Any of
121 .Fa readfds ,
122 .Fa writefds ,
123 and
124 .Fa exceptfds
125 may be given as nil pointers if no descriptors are of interest.
126 .Pp
127 Even if no descriptors are of interest, the timeout works as described,
128 effectively putting the process into an interruptible sleep for the
129 specified timeout.
130 If
131 .Fa timeout
132 is nil, the process will block until a signal is received.
133 .Sh RETURN VALUES
134 .Fn Select
135 returns the number of ready descriptors that are contained in
136 the descriptor sets,
137 or -1 if an error occurred.
138 If the time limit expires,
139 .Fn select
140 returns 0.
141 If
142 .Fn select
143 returns with an error,
144 including one due to an interrupted call,
145 the descriptor sets will be unmodified.
146 .Sh ERRORS
147 An error return from
148 .Fn select
149 indicates:
150 .Bl -tag -width Er
151 .It Bq Er EBADF
152 One of the descriptor sets specified an invalid descriptor.
153 .It Bq Er EINTR
154 A signal was delivered before the time limit expired and
155 before any of the selected events occurred.
156 .It Bq Er EINVAL
157 The specified time limit is invalid.  One of its components is
158 negative or too large.
159 .It Bq Er EINVAL
160 .Fa nfds
161 was invalid.
162 .El
163 .Sh SEE ALSO
164 .Xr accept 2 ,
165 .Xr connect 2 ,
166 .Xr getdtablesize 2 ,
167 .Xr gettimeofday 2 ,
168 .Xr read 2 ,
169 .Xr recv 2 ,
170 .Xr send 2 ,
171 .Xr write 2 ,
172 .Xr clocks 7
173 .Sh NOTES
174 The default size of
175 .Dv FD_SETSIZE
176 is currently 1024.
177 In order to accommodate programs which might potentially
178 use a larger number of open files with
179 .Fn select ,
180 it is possible
181 to increase this size by having the program define
182 .Dv FD_SETSIZE
183 before the inclusion of any header which includes
184 .In sys/types.h .
185 .Pp
186 If
187 .Fa nfds
188 is greater than the number of open files,
189 .Fn select
190 is not guaranteed to examine the unused file descriptors.   For historical
191 reasons,
192 .Fn select
193 will always examine the first 256 descriptors.
194 .Sh HISTORY
195 The
196 .Fn select
197 function call appeared in
198 .Bx 4.2 .
199 .Sh BUGS
200 .St -susv2
201 allows systems to modify the original timeout in place.
202 Thus, it is unwise to assume that the timeout value will be unmodified
203 by the
204 .Fn select
205 call.