tech-kern archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

read side of pipe and poll() events



poll(2) reads:

     POLLHUP        The device or socket has been disconnected.  This flag is
                    always checked, even if not present in the events bitmask.
                    Note that POLLHUP and POLLOUT should never be present in
                    the revents bitmask at the same time.  If the remote end
                    of a socket is closed, poll() returns a POLLIN event,
                    rather than a POLLHUP.

I believe, and the pipe_poll() seems to confirm, that POLLIN and POLLHUP
are NOT mutually exclusive in the case of the read side of a pipe reaching
EOF.  That both, and POLLRDNORM if requested, are set.  Which is correct?

#include <stdio.h>
#include <unistd.h>
#include <poll.h>

int
main() {
int fds[2];
struct pollfd pfds[1];

pipe(fds);
write(fds[1], ".", 1);
close(fds[1]);

pfds[0].fd = fds[0];
pfds[0].events = POLLIN | POLLRDNORM;

while (poll(pfds, 1, 1)) {
   printf("revents 0x%x\n", pfds[0].revents);
   if (pfds[0].revents & POLLIN)
        if (read(pfds[0].fd, fds, 8) == 0)
            break;
}
}


Home | Main Index | Thread Index | Old Index