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