STREAMS-UX Programmer's Guide (February 2007)
STREAMS Mechanism and System Calls
Polling Streams
Chapter 2
34
the application. However, during application execution, there can be a small number of open streams. The
unused file descriptors inside the corresponding pollfd structures must be set to -1. These pollfd
structures (with a negative fd) are ignored by poll (2). If all the pollfd structures passed to the poll (2)
call have negative fd values, poll (2) returns 0 and has no other results.
•The poll (2) system call returns to the calling application when one of the following two conditions is met.
It encounters at least one read-queue with at least one of the requested events (or a POLLERR, POLLHUP, or
POLLNVAL), or the wait period reaches the specified timeout value in milliseconds.
• A timeout value of -1 implies no timeout, that is, poll (2) will wait until at least one requested event (or a
POLLERR, POLLHUP or POLLNVAL) occurs on one of the read queues.
• For a timeout value of 0, poll (2) checks to see if any of the requested events occurred on any one of the
read-queues, and returns immediately, regardless of the results.
•The poll (2) system call behavior is not affected by the O_NONBLOCK flag set on any of the specified file
descriptors.
The /dev/poll Interface
HP-UX offers an alternative polling mechanism using a special device, /dev/poll (refer to poll (7) for
details). This device is supported by STREAMS.
The poll (2) system call and the /dev/poll device behave differently. With poll (2), the application sends all
relevant file descriptors and the requested events as parameters. The kernel routines associated with the
stream head copy these structures into the kernel memory, and then copy back the
revents
parameter
bitmask into every pollfd structure. This happens with every poll (2) call.
With /dev/poll, the user registers a set of file descriptors and a corresponding set of events. This is achieved
by the application opening the (/dev/poll) device, and then writing the relevant pollfd structures and
parameter values to the (/dev/poll) device. Next, the application polls the stream head read-queue with an
ioctl (2) directive specifically meant for polling.
User applications working with the /dev/poll device use the open (2), write (2), and ioctl (2) functions as
follows:
#include <sys/devpoll.h>
#include <fcntl.h>
int open(“/dev/poll”, O_RDWR);
int write(int fd, struct pollfd *buf, size_t nbyte);
int ioctl(int fd, DP_POLL, struct dvpoll *arg);
int ioctl(int fd, DP_ISPOLLED, struct pollfd *arg);
int close(fd);
The user applications performs the following steps:
1. Opens the polling device or event port (/dev/poll).
2. Registers file descriptors and corresponding events of interest with /dev/poll.
3. Deregisters (or modify selected event requests).
4. Polls the device for the occurrence of requested events (or error conditions).
5. Closes the polling device.
Opening an event port:
Each open (2) call on /dev/poll device enables an event port where a registered set of file
descriptors can be polled for relevant events. The file descriptor returned by the open (2)
system call represents the event port as follows: