poll(2) System Calls Manual poll(2) poll, ppoll - (libc -lc) #include int poll(struct pollfd *fds, nfds_t nfds, int timeout); #define _GNU_SOURCE /* feature_test_macros(7) */ #include int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *_Nullable tmo_p, const sigset_t *_Nullable sigmask); poll() select(2): /. epoll(7) poll(). fds : struct pollfd { int fd; /* */ short events; /* */ short revents; /* */ }; fds nfds. fd . events revents . ( poll() : fd .) events fd. revents POLLHUP POLLERR POLLNVAL ( ). revents . revents events POLLERR POLLHUP POLLNVAL. ( events revents .) ( ) poll() . timeout poll() . : o o o . "" poll() . timeout . timeout . timeout poll() . / events revents : POLLIN . POLLPRI . : o TCP ( tcp(7)). o ( ioctl_tty(2)). o cgroup.events ( cgroups(7)). POLLOUT ( O_NONBLOCK). POLLRDHUP ( 2.6.17) . _GNU_SOURCE ( ) . POLLERR ( revents events). . POLLHUP ( revents events). . 0 ( ) . POLLNVAL : fd ( revents events). _XOPEN_SOURCE : POLLRDNORM POLLIN. POLLRDBAND ( ). POLLWRNORM POLLOUT. POLLWRBAND . POLLMSG . ppoll() poll() ppoll() select(2) pselect(2): pselect(2) ppoll() . timeout ppoll() : ready = ppoll(&fds, nfds, tmo_p, &sigmask); : sigset_t origmask; int timeout; timeout = (tmo_p == NULL) ? -1 : (tmo_p->tv_sec * 1000 + tmo_p->tv_nsec / 1000000); pthread_sigmask(SIG_SETMASK, &sigmask, &origmask); ready = poll(&fds, nfds, timeout); pthread_sigmask(SIG_SETMASK, &origmask, NULL); timeout poll() *tmo_p ppoll(). pselect(2) ppoll(). sigmask NULL ( ppoll() poll() timeout). tmo_p ppoll() . timespec(3). tmo_p NULL ppoll() . poll() pollfds revents ( ). . -1 errno . EFAULT fds . . EINTR signal(7). EINVAL nfds RLIMIT_NOFILE. EINVAL (ppoll()) *tmo_p (). ENOMEM . UNIX poll() EAGAIN ENOMEM . POSIX . EAGAIN EINTR. INFTIM -1 timeout poll(). glibc. C ppoll() tmo_p. glibc . ppoll() glibc tmo_p. ppoll() size_t sigsetsize sigmask. ppoll() glibc ( sizeof(kernel_sigset_t)). sigprocmask(2) libc sigset. POSIX.1-2024. poll() POSIX.1-2001. 2.1.23. poll() glibc select(2). ppoll() POSIX.1-2024. 2.6.16 glibc 2.4. poll() ppoll() O_NONBLOCK. poll() select(2). BUGS select(2). (POLLIN). poll() . : o revents o o ( POLLHUP) . FIFO: $ mkfifo myfifo; $ ./poll_input myfifo; FIFO FIFO: $ echo aaaaabbbbbccccc > myfifo; : Opened "myfifo" on fd 3 About to poll() Ready: 1 fd=3; events: POLLIN POLLHUP read 10 bytes: aaaaabbbbb About to poll() Ready: 1 fd=3; events: POLLIN POLLHUP read 6 bytes: ccccc About to poll() Ready: 1 fd=3; events: POLLHUP closing fd 3 All file descriptors closed. Bye. poll() : o revents POLLIN POLLHUP FIFO . . o poll() POLLIN POLLHUP . o poll() POLLHUP FIFO . /* poll_input.c Licensed under GNU General Public License v2 or later. */ #include #include #include #include #include #include #include int main(int argc, char *argv[]) { int ready; char buf[10]; nfds_t num_open_fds, nfds; ssize_t s; struct pollfd *pfds; if (argc < 2) { fprintf(stderr, "Usage: %s file...\n", argv[0]); exit(EXIT_FAILURE); } num_open_fds = nfds = argc - 1; pfds = calloc(nfds, sizeof(struct pollfd)); if (pfds == NULL) err(EXIT_FAILURE, "malloc"); /* Open each file on command line, and add it to 'pfds' array. */ for (nfds_t j = 0; j < nfds; j++) { pfds[j].fd = open(argv[j + 1], O_RDONLY); if (pfds[j].fd == -1) err(EXIT_FAILURE, "open"); printf("Opened \"%s\" on fd %d\n", argv[j + 1], pfds[j].fd); pfds[j].events = POLLIN; } /* Keep calling poll() as long as at least one file descriptor is open. */ while (num_open_fds > 0) { printf("About to poll()\n"); ready = poll(pfds, nfds, -1); if (ready == -1) err(EXIT_FAILURE, "poll"); printf("Ready: %d\n", ready); /* Deal with array returned by poll(). */ for (nfds_t j = 0; j < nfds; j++) { if (pfds[j].revents != 0) { printf(" fd=%d; events: %s%s%s\n", pfds[j].fd, (pfds[j].revents & POLLIN) ? "POLLIN " : "", (pfds[j].revents & POLLHUP) ? "POLLHUP " : "", (pfds[j].revents & POLLERR) ? "POLLERR " : ""); if (pfds[j].revents & POLLIN) { s = read(pfds[j].fd, buf, sizeof(buf)); if (s == -1) err(EXIT_FAILURE, "read"); printf(" read %zd bytes: %.*s\n", s, (int) s, buf); } else { /* POLLERR | POLLHUP */ printf(" closing fd %d\n", pfds[j].fd); if (close(pfds[j].fd) == -1) err(EXIT_FAILURE, "close"); num_open_fds--; } } } } printf("All file descriptors closed. Bye.\n"); exit(EXIT_SUCCESS); } restart_syscall(2), select(2), select_tut(2), timespec(3), epoll(7), time(7) 3 . . : . 6.18 8 2026 poll(2)