HP-UX Reference (11i v1 05/09) - 2 System Calls (vol 5)
s
select(2) select(2)
On failure, the objects pointed to by the readfds, writefds, and errorfds arguments are not modified. If the
timeout interval expires without the specified condition being true for any of the specified file descriptors,
the objects pointed to by the readfds, writefds, and errorfds arguments have all bits set to 0.
File descriptor masks of type fd_set can be initialized and tested with
FD_CLR(), FD_ISSET() ,
FD_SET(), and FD_ZERO() . It is unspecified whether each of these is a macro or a function. If a macro
definition is suppressed in order to access an actual function, or a program defines an external identifier
with any of these names, the behaviour is undefined.
FD_CLR(fd, &fdset) Clears the bit for the file descriptor fd in the file descriptor set fdset.
FD_ISSET(fd, &fdset) Returns a non-zero value if the bit for the file descriptor fd is set in
the file descriptor set pointed to by fdset, and 0 otherwise.
FD_SET(fd, &fdset) Sets the bit for the file descriptor fd in the file descriptor set fdset.
FD_ZERO(&fdset)
Initializes the file descriptor set fdset to have zero bits for all file
descriptors. The behaviour of these macros is undefined if the fd argu-
ment is less than 0 or greater than or equal to
FD_SETSIZE .
RETURN VALUE
FD_CLR(), FD_SET(), and FD_ZERO() return no value. FD_ISSET() returns a non-zero value if
the bit for the file descriptor fd is set in the file descriptor set pointed to by fdset, and 0 otherwise.
On successful completion,
select() returns the total number of bits set in the bit masks. Otherwise, −1
is returned, and
errno is set to indicate the error.
ERRORS
Under the following conditions, select() fails and sets errno to:
[EBADF] One or more of the file descriptor sets specified a file descriptor that is not a valid
open file descriptor. This could happen either if the file descriptor sets are not initial-
ized or nfds argument is greater than FD_SETSIZE.
[EINTR] The select() function was interrupted before any of the selected events occurred
and before the timeout interval expired. If SA_RESTART has been set for the inter-
rupting signal, it is implementation-dependent whether
select() restarts or
returns with
EINTR.
[EINVAL] An invalid timeout interval was specified. Valid values for number of microseconds
should be a non-negative integer less than 1,000,000 and, for number of seconds,
should be a non-negative integer.
[EINVAL] The nfds argument is less than 0, or is greater than or equal to the value of
MAXFU-
PLIM
, which specifies the absolute maximum number of files a process can have open
at one time. If the resource limit RLIMIT_NOFILE
for a process is less than or
equal to 2048,
MAXFUPLIM is considered to be 2048.
[EINVAL] One of the specified file descriptors refers to a STREAM or multiplexer that is linked
(directly or indirectly) downstream from a multiplexer.
APPLICATION USAGE
The use of a timeout does not affect any pending timers set up by alarm(), ualarm() ,orsetiti-
mer()
.
On successful completion, the object pointed to by the timeout argument may be modified.
The FD_SETSIZE is used in the definition of fd_set structure. It is set to a value of 2048 to accommo-
date 2048 file descriptors. Any user code that uses FD_SETSIZE or the structure fd_set should
redefine FD_SETSIZE to a smaller value (greater than or equal to the number of open files the process
will have) in order to save space. Similarly, any user code that wants to test more than 2048 file descriptors
should redefine FD_SETSIZE to the required higher value.
The user can also allocate the space for fd_set structure dynamically, depending upon the number of file
descriptors to be tested. The following code segment illustrates the basic concepts.
int num_of_fds,s;
struct fd_set *f;
HP-UX 11i Version 1: September 2005 − 2 − Hewlett-Packard Company Section 2−−295