#define _XOPEN_SOURCE 700 #include /* creat, O_CREAT */ #include /* poll */ #include /* printf, puts, snprintf */ #include /* EXIT_FAILURE, EXIT_SUCCESS */ #include /* read */ int main(int argc, char **argv) { char buf[1024]; int fd, n; short revents; struct pollfd pfd; fd = open(argv[1], O_RDONLY | O_NONBLOCK); pfd.fd = fd; pfd.events = POLLIN; while (1) { puts("loop"); poll(&pfd, 1, -1); revents = pfd.revents; if (revents & POLLIN) { n = read(pfd.fd, buf, sizeof(buf)); printf("POLLIN n=%d buf=%.*s\n", n, n, buf); } if (revents & POLLHUP) { printf("POLLHUP\n"); close(pfd.fd); break; } if (revents & POLLNVAL) { printf("POLLNVAL\n"); } if (revents & POLLERR) { printf("POLLERR\n"); } } }