HP-UX Reference (11i v2 07/12) - 2 System Calls (vol 5)

s
select(2) select(2)
If the readfds, writefds, and errorfds arguments are all null pointers and the timeout argument is not a
null pointer,
select() blocks for the time specified, or until interrupted by a signal. If the readfds, wri-
tefds, and errorfds arguments are all null pointers and the timeout argument is a null pointer,
select()
blocks until interrupted by a signal.
File descriptors associated with regular files always select true for ready to read, ready to write, and error
conditions.
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.
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.
TCP sockets select true on reads only for normal data. They do not select true on reads if out-of-band data
("urgent" data) arrives.
TCP sockets select true on exceptions for out-of-band data.
AF_CCITT sockets select true on reads for normal and out-of-band data and information, including super-
visory frames.
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.
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
argument is less than 0 or greater than or equal to
FD_SETSIZE .
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 descrip-
tors 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;
/*
* Set num_of_fds to the required value.
* User can set it to the maximum possible value the kernel is
* configured for, by using sysconf(_SC_OPEN_MAX).
338 Hewlett-Packard Company 2 HP-UX 11i Version 2: December 2007 Update