Alternative I/O Models 1343
On some other UNIX implementations, if the read end of a pipe is closed,
then, instead of returning with POLLERR set, poll() returns with either the POLLOUT bit
or the POLLHUP bit set. Portable applications need to check to see if any of these bits
is set to determine if a write() will block.
Sockets
Table 63-6 summarizes the behavior of select() and poll() for sockets. For the poll()
column, we assume that events was specified as (POLLIN | POLLOUT | POLLPRI). For the
select() column, we assume that the file descriptor is being tested to see if input is
possible, output is possible, or an exceptional condition occurred (i.e., the file
descriptor is specified in all three sets passed to select()). This table covers just the
common cases, not all possible scenarios.
The Linux poll() behavior for UNIX domain sockets after a peer close() differs
from that shown in Table 63-6. As well as the other flags, poll() additionally
returns POLLHUP in revents.
The Linux-specific POLLRDHUP flag (available since Linux 2.6.17) needs a little further
explanation. This flag—actually in the form of EPOLLRDHUP—is designed primarily for
use with the edge-triggered mode of the epoll API (Section 63.4). It is returned
when the remote end of a stream socket connection has shut down the writing half
Table 63-4: select() and poll() indications for the read end of a pipe or FIFO
Condition or event
select() poll()
Data in pipe? Write end open?
no no r POLLHUP
yes yes r POLLIN
yes no r POLLIN | POLLHUP
Table 63-5: select() and poll() indications for the write end of a pipe or FIFO
Condition or event
select() poll()
Space for PIPE_BUF bytes? Read end open?
no no w POLLERR
yes yes w POLLOUT
yes no w POLLOUT | POLLERR
Table 63-6: select() and poll() indications for sockets
Condition or event select() poll()
Input available r POLLIN
Output possible w POLLOUT
Incoming connection established on listening socket r POLLIN
Out-of-band data received (TCP only) x POLLPRI
Stream socket peer closed connection or executed
shutdown(SHUT_WR)
rw POLLIN | POLLOUT |
POLLRDHUP