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