1 <!-- Copyright (C) 2003 Red Hat, Inc. -->
2 <!-- This material may be distributed only subject to the terms -->
3 <!-- and conditions set forth in the Open Publication License, v1.0 -->
4 <!-- or later (the latest version is presently available at -->
5 <!-- http://www.opencontent.org/openpub/). -->
6 <!-- Distribution of the work or derivative of the work in any -->
7 <!-- standard (paper) book form is prohibited unless prior -->
8 <!-- permission is obtained from the copyright holder. -->
13 ><meta name="MSSmartTagsPreventParsing" content="TRUE">
16 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+
19 TITLE="eCos Reference Manual"
20 HREF="ecos-ref.html"><LINK
22 TITLE="TCP/IP Library Reference"
23 HREF="tcpip-library-reference.html"><LINK
26 HREF="net-common-tcpip-manpages-poll.html"><LINK
29 HREF="net-common-tcpip-manpages-send.html"></HEAD
40 SUMMARY="Header navigation table"
49 >eCos Reference Manual</TH
57 HREF="net-common-tcpip-manpages-poll.html"
65 >Chapter 38. TCP/IP Library Reference</TD
71 HREF="net-common-tcpip-manpages-send.html"
85 NAME="NET-COMMON-TCPIP-MANPAGES-SELECT">select</H1
94 >SELECT(2) System Calls Manual SELECT(2)
97 select - synchronous I/O multiplexing
100 #include <sys/types.h>
101 #include <sys/time.h>
102 #include <unistd.h>
105 select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
106 struct timeval *timeout);
108 FD_SET(fd, &fdset);
110 FD_CLR(fd, &fdset);
112 FD_ISSET(fd, &fdset);
117 select() examines the I/O descriptor sets whose addresses are passed in
118 readfds, writefds, and exceptfds to see if some of their descriptors are
119 ready for reading, are ready for writing, or have an exceptional condi-
120 tion pending, respectively. The first nfds descriptors are checked in
121 each set; i.e., the descriptors from 0 through nfds-1 in the descriptor
122 sets are examined. On return, select() replaces the given descriptor
123 sets with subsets consisting of those descriptors that are ready for the
124 requested operation. select() returns the total number of ready descrip-
125 tors in all the sets.
127 The descriptor sets are stored as bit fields in arrays of integers. The
128 following macros are provided for manipulating such descriptor sets:
129 FD_ZERO(&fdset) initializes a descriptor set fdset to the null set.
130 FD_SET(fd, &fdset) includes a particular descriptor fd in fdset.
131 FD_CLR(fd, &fdset) removes fd from fdset. FD_ISSET(fd, &fdset) is non-
132 zero if fd is a member of fdset, zero otherwise. The behavior of these
133 macros is undefined if a descriptor value is less than zero or greater
134 than or equal to FD_SETSIZE, which is normally at least equal to the max-
135 imum number of descriptors supported by the system.
137 If timeout is a non-null pointer, it specifies a maximum interval to wait
138 for the selection to complete. If timeout is a null pointer, the select
139 blocks indefinitely. To effect a poll, the timeout argument should be
140 non-null, pointing to a zero-valued timeval structure. timeout is not
141 changed by select(), and may be reused on subsequent calls; however, it
142 is good style to re-initialize it before each invocation of select().
144 Any of readfds, writefds, and exceptfds may be given as null pointers if
145 no descriptors are of interest.
148 select() returns the number of ready descriptors that are contained in
149 the descriptor sets, or -1 is an error occurred. If the time limit
150 expires, select() returns 0. If select() returns with an error, includ-
151 ing one due to an interrupted call, the descriptor sets will be unmodi-
155 An error return from select() indicates:
157 [EFAULT] One or more of readfds, writefds, or exceptfds points
158 outside the process's allocated address space.
160 [EBADF] One of the descriptor sets specified an invalid
163 [EINTR] A signal was delivered before the time limit expired
164 and before any of the selected events occurred.
166 [EINVAL] The specified time limit is invalid. One of its com-
167 ponents is negative or too large.
170 accept(2), connect(2), gettimeofday(2), poll(2), read(2), recv(2),
171 send(2), write(2), getdtablesize(3)
174 Although the provision of getdtablesize(3) was intended to allow user
175 programs to be written independent of the kernel limit on the number of
176 open files, the dimension of a sufficiently large bit field for select
177 remains a problem. The default bit size of fd_set is based on the symbol
178 FD_SETSIZE (currently 256), but that is somewhat smaller than the current
179 kernel limit to the number of open files. However, in order to accommo-
180 date programs which might potentially use a larger number of open files
181 with select, it is possible to increase this size within a program by
182 providing a larger definition of FD_SETSIZE before the inclusion of
183 <sys/types.h>. The kernel will cope, and the userland libraries provided
184 with the system are also ready for large numbers of file descriptors.
186 Alternatively, to be really safe, it is possible to allocate fd_set bit-
187 arrays dynamically. The idea is to permit a program to work properly
188 even if it is execve(2)'d with 4000 file descriptors pre-allocated. The
189 following illustrates the technique which is used by userland libraries:
194 fdsr = (fd_set *)calloc(howmany(max+1, NFDBITS),
201 n = select(max+1, fdsr, NULL, NULL, &tv);
205 Alternatively, it is possible to use the poll(2) interface. poll(2) is
206 more efficient when the size of select()'s fd_set bit-arrays are very
207 large, and for fixed numbers of file descriptors one need not size and
208 dynamically allocate a memory object.
210 select() should probably have been designed to return the time remaining
211 from the original timeout, if any, by modifying the time value in place.
212 Even though some systems stupidly act in this different way, it is
213 unlikely this semantic will ever be commonly implemented, as the change
214 causes massive source code compatibility problems. Furthermore, recent
215 new standards have dictated the current behaviour. In general, due to
216 the existence of those brain-damaged non-conforming systems, it is unwise
217 to assume that the timeout value will be unmodified by the select() call,
218 and the caller should reinitialize it on each invocation. Calculating
219 the delta is easily done by calling gettimeofday(2) before and after the
220 call to select(), and using timersub() (as described in getitimer(2)).
222 Internally to the kernel, select() works poorly if multiple processes
223 wait on the same file descriptor. Given that, it is rather surprising to
224 see that many daemons are written that way (i.e., httpd(8)).
227 The select() function call appeared in 4.2BSD.
229 BSD March 25, 1994 BSD
240 SUMMARY="Footer navigation table"
251 HREF="net-common-tcpip-manpages-poll.html"
269 HREF="net-common-tcpip-manpages-send.html"
285 HREF="tcpip-library-reference.html"