ptg10805159
506 Advanced I/O Chapter 14
It is important to realize that whether a descriptor is blocking or not doesn’t affect
whetherselectblocks. That is, if we have a nonblocking descriptor that we want to
read from and we callselectwith a timeout value of 5 seconds,selectwill block for
up to 5 seconds. Similarly, if we specify an infinite timeout,selectblocks until data is
ready for the descriptor or until a signal is caught.
If we encounter the end of file on a descriptor,that descriptor is considered readable
byselect.Wethen callreadand it returns 0—the way to signify end of file on UNIX
systems. (Many people incorrectly assume that select indicates an exception
condition on a descriptor when the end of file is reached.)
POSIX.1 also defines a variant ofselectcalledpselect.
#include <sys/select.h>
int pselect(intmaxfdp1,fd_set *restrictreadfds,
fd_set *restrict writefds,fd_set *restrictexceptfds,
const struct timespec *restrict tsptr,
const sigset_t *restrictsigmask);
Returns: count of ready descriptors, 0 on timeout,−1 on error
Thepselectfunction is identical toselect,with the following exceptions.
•The timeout value forselectis specified by atimevalstructure, but for
pselect,atimespecstructure is used. (Recall the definition of thetimespec
structure in Section 4.2.) Instead of seconds and microseconds, thetimespec
structurerepresents the timeout value in seconds and nanoseconds. This
provides a higher-resolution timeout if the platform supports that fine a level of
granularity.
•The timeout value forpselectis declaredconst,and we areguaranteed that
its value will not change as a result of callingpselect.
•Anoptional signal mask argument is available withpselect.Ifsigmaskis
NULL,pselectbehaves asselectdoes with respect to signals. Otherwise,
sigmaskpoints to a signal mask that is atomically installed whenpselectis
called. Onreturn, the previous signal mask is restored.
14.4.2 pollFunction
Thepollfunction is similar toselect,but the programmer interface is different. This
function was originally introduced in System V to support the STREAMS subsystem,
but we areable to use it with any type of file descriptor.
#include <poll.h>
int poll(struct pollfdfdarray[], nfds_t nfds,int timeout);
Returns: count of ready descriptors, 0 on timeout,−1 on error