HP-UX Reference (11i v1 05/09) - 2 System Calls (vol 5)

s
select(2) select(2)
HP-UX EXTENSIONS
SYNOPSIS
#include <time.h>
int select(
size_t nfds,
int *readfds,
int *writefds,
int *exceptfds,
const struct timeval *timeout
);
DESCRIPTION
This select() function prototype is provided for backward compatibility only. For this prototype to be
used,
<time.h>, instead of <sys/time.h> , must be included and the symbol
_XOPEN_SOURCE_EXTENDED must not be defined in the compilation time. Otherwise, the X/Open com-
pliant version will be used.
select() examines the files or devices associated with the file descriptors specified by the bit masks
readfds, writefds, and exceptfds. The bits from 0 through nfds1 are examined. File descriptor f is
represented by the bit 1<<f in the masks. More formally, a file descriptor is represented by:
fds[(f / BITS_PER_INT)] & (1 << (f % BITS_PER_INT))
Ttys and sockets are ready for reading or writing, respectively, if a
read() or write() would not block
for one or more of the following reasons:
input data is available.
output data can be accepted.
an error condition exists, such as a broken pipe, no carrier, or a lost connection.
Sockets select true on exceptions if out-of-band data is available.
Pipes are ready for reading if there is any data in the pipe, or if there are no writers left for the pipe. Pipes
are ready for writing if there is room for more data in the pipe AND there are one or more readers for the
pipe, OR there are no readers left for the pipe.
select() returns the same results for a pipe whether a
file descriptor associated with the read-only end or the write-only end of the pipe is used, since both file
descriptors refer to the same underlying pipe. So a select() of a read-only file descriptor that is associ-
ated with a pipe can return ready to write, even though that particular file descriptor cannot be written to.
ERRORS
[EFAULT] One or more of the pointers was invalid. The reliable detection of this error is imple-
mentation dependent.
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;
/* First open each terminal for reading and put the
* file descriptors into array tty[NTTYS]. The code
HP-UX 11i Version 1: September 2005 1 Hewlett-Packard Company Section 2297