HP-UX Reference (11i v2 04/09) - 2 System Calls (vol 5)
s
select(2) select(2)
* User can set it to the maximum possible value the kernel is
* configured for, by using sysconf(_SC_OPEN_MAX).
* Note that, if you are not using these many files, you are
* wasting too much space.
*/
num_of_fds = sysconf(_SC_OPEN_MAX);
s = sizeof(long);
/*
* howmany is a macro defined in sys/types.h
*/
f = (struct fd_set *)malloc(s*howmany(num_of_fds, s*8);
/*
* Use f wherever struct fd_set * is used.
* It can be used to test num_of_fds file descriptors.
*/
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 ini-
tialized 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].
[EFAULT] One or more of the pointers was invalid. The reliable detection of this error is
implementation dependent.
[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.
EXAMPLES
The following call to select() checks if any of 4 terminals are ready for reading. select() times
out after 5 seconds if no terminals are ready for reading. Note that the code for opening the terminals or
reading from the terminals is not shown in this example. Also, note that this example must be modified if
the calling process has more than 32 file descriptors open. Following this first example is an example of
select with more than 32 file descriptors.
#define MASK(f) (1 << (f))
#define NTTYS 4
int tty[NTTYS];
int ttymask[NTTYS];
int readmask = 0;
int readfds;
int nfound, i;
struct timeval timeout;
HP-UX 11i Version 2: September 2004 − 3 − Hewlett-Packard Company Section 2−−305