Advanced Programming in the UNIX® Environment

(lily) #1
ptg10805159

Section 14.4 I/O Multiplexing 507


Withpoll,instead of building a set of descriptors for each condition (readability,
writability,and exception condition) as we did withselect, we build an array of
pollfdstructures, with each array element specifying a descriptor number and the
conditions that we’reinterested in for that descriptor:
struct pollfd {
int fd; /* file descriptor to check, or <0 to ignore */
short events; /* events of interest on fd */
short revents; /* events that occurred on fd */
};
The number of elements in thefdarrayarray is specified bynfds.

Historically,therehave been differences in how thenfdsparameter was declared. SVR3
specified the number of elements in the array as anunsigned long,which seems excessive.
In the SVR4 manual [AT&T 1990d], the prototype forpollshowed the data type of the second
argument assize_t.(Recall the primitive system data types from Figure2.21.) But the actual
prototype in the<poll.h>header still showed the second argument as anunsigned long.
The Single UNIX Specification defines the new typenfds_tto allow the implementation to
select the appropriate type and hide the details from applications. Note that this type has to be
large enough to hold an integer,since the return value represents the number of entries in the
array with satisfied events.
The SVID corresponding to SVR4 [AT&T 1989] showed the first argument topollasstruct
pollfdfdarray[],whereas the SVR4 manual page [AT&T 1990d] showed this argument as
struct pollfd *fdarray.Inthe C language, both declarations areequivalent. Weuse the
first declaration to reiterate thatfdarraypoints to an array of structures and not a pointer to
asingle structure.

To tell the kernel which events we’reinterested in for each descriptor, we have to set
theeventsmember of each array element to one or more of the values in Figure14.17.
On return, thereventsmember is set by the kernel, thereby specifying which events
have occurred for each descriptor.(Note that poll doesn’t change the events
member.This behavior differs from that ofselect,which modifies its arguments to
indicate what is ready.)

Input to Result from
Name events? revents? Description

POLLIN ••Data other than high priority data can be read without blocking
(equivalent toPOLLRDNORM|POLLRDBAND).
POLLRDNORM ••Normal data can be read without blocking.
POLLRDBAND ••Priority data can be read without blocking.
POLLPRI ••High-priority data can be read without blocking.
POLLOUT ••Normal data can be written without blocking.
POLLWRNORM ••Same asPOLLOUT.
POLLWRBAND ••Priority data can be written without blocking.
POLLERR • An error has occurred.
POLLHUP • Ahangup has occurred.
POLLNVAL • The descriptor does not reference an open file.

Figure 14.17 Theeventsandreventsflags forpoll
Free download pdf