select.2 (2012 03)
s
select(2) select(2)
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 associated 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 descrip-
tors. 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 of
select() 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 and execution time. 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;
/*
* 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).
* Note that, if you are not using this 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,
pselect() and select() return 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,
pselect() and select() fail and set 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 initialized or
HP-UX 11i Version 3: March 2012 − 3 − Hewlett-Packard Company 3