Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

508 Advanced I/O Chapter 14


The first four rows of Figure14.17 test for readability,the next three test for
writability,and the final three arefor exception conditions. The last three rows in
Figure14.17 areset by the kernel on return. These three values arereturned in
reventswhen the condition occurs, even if they weren’t specified in theeventsfield.

The poll event names containing the termBANDrefer to priority bands in STREAMS. Refer to
Rago[ 1993 ]for moreinformation about STREAMS and priority bands.

When a descriptor is hung up (POLLHUP), we can no longer write to the descriptor.
Theremay,however,still be data to be read from the descriptor.
The final argument topollspecifies how long we want to wait. As withselect,
thereare three cases.
timeout==− 1
Wait forever.(Some systems define the constantINFTIMin<stropts.h>as
−1.) Wereturn when one of the specified descriptors is ready or when a signal
is caught. If a signal is caught,pollreturns−1witherrnoset toEINTR.
timeout== 0
Don’t wait. All the specified descriptors aretested, and we return immediately.
This is a way to poll the system to find out the status of multiple descriptors,
without blocking in the call topoll.
timeout> 0
Waittimeoutmilliseconds. Wereturn when one of the specified descriptors is
ready or when thetimeoutexpires. If thetimeoutexpires beforeany of the
descriptors is ready,the return value is 0. (If your system doesn’t provide
millisecond resolution,timeoutis rounded up to the nearest supported value.)

It is important to realize the difference between an end of file and a hangup. If
we’reentering data from the terminal and type the end-of-file character,POLLINis
turned on so we can read the end-of-file indication (readreturns 0). POLLHUPis not
turned on inrevents.Ifwe’rereading from a modem and the telephone line is hung
up, we’ll receive thePOLLHUPnotification.
As withselect,whether a descriptor is blocking doesn’t affect whetherpoll
blocks.

Interruptibility ofselectandpoll


When the automatic restarting of interrupted system calls was introduced with 4.2BSD
(Section 10.5), theselectfunction was never restarted. This characteristic continues
with most systems even if theSA_RESTARToption is specified. But under SVR4, if
SA_RESTARTwas specified, evenselectandpollwereautomatically restarted. To
prevent this from catching us when we port software to systems derived from SVR4,
we’ll always use thesignal_intrfunction (Figure10.19) if the signal could interrupt
acall toselectorpoll.
Free download pdf