eventfd(2) System Calls Manual eventfd(2) eventfd - LIBRARY Standard C library (libc, -lc) #include int eventfd(unsigned int initval, int flags); eventfd() << eventfd>>, / . 64- (uint64_t) , . , initval. eventfd() , eventfd. eventfd() flags ( OR): EFD_CLOEXEC ( Linux 2.6.27) close-on-exec (FD_CLOEXEC) . O_CLOEXEC open(2) , . EFD_NONBLOCK ( Linux 2.6.27) O_NONBLOCK ( open(2)), . fcntl(2) . EFD_SEMAPHORE ( Linux 2.6.30) - . . Up to Linux 2.6.26, the flags argument is unused, and must be specified as zero. eventfd(): read(2) read(2) 8- . read(2) EINVAL, 8 . The value returned by read(2) is in host byte order--that is, the native byte order for integers on the host machine. read(2) eventfd -- , EFD_SEMAPHORE eventfd: o EFD_SEMAPHORE eventfd , read(2) 8 . o EFD_SEMAPHORE eventfd , read(2) 8 , 1, 1. o eventfd read(2), , ( read(2) ) EAGAIN, . write(2) write(2) 8- . , , 64- 1 (.., 0xfffffffffffffffe). , write(2) , read(2), EAGAIN, . write(2) EINVAL, 8 , 0xffffffffffffffff. poll(2) select(2) (and similar) poll(2) (, , epoll(7)) select(2) : o ( select(2) readfds; poll(2) POLLIN), 0. o ( select(2) writefds; poll(2) POLLOUT), , , "1" . o If an overflow of the counter value was detected, then select(2) indicates the file descriptor as being both readable and writable, and poll(2) returns a POLLERR event. As noted above, write(2) can never overflow the counter. However an overflow can occur if 2^64 eventfd "signal posts" were performed by the KAIO subsystem (theoretically possible, but practically unlikely). If an overflow has occurred, then read(2) will return that maximum uint64_t value (i.e., 0xffffffffffffffff). eventfd : pselect(2) ppoll(2). close(2) , . , eventfd, , . , eventfd(), , fork(2). eventfd. , eventfd(), execve(2), close-on-exec. eventfd() eventfd. -1, errno . EINVAL flags . EMFILE . ENFILE . ENODEV () inode. ENOMEM eventfd. attributes(7). +----------------------------+----------------------------------------------------------+--------------------------+ | | | | +----------------------------+----------------------------------------------------------+--------------------------+ |eventfd() | | MT-Safe | +----------------------------+----------------------------------------------------------+--------------------------+ C Linux: eventfd() eventfd2(). flags. flags, . glibc eventfd2(), . glibc GNU C , eventfd: typedef uint64_t eventfd_t; int eventfd_read(int fd, eventfd_t *value); int eventfd_write(int fd, eventfd_t value); eventfd, 0, -1 . Linux, GNU. eventfd() Linux 2.6.22, glibc 2.8. eventfd2() Linux 2.6.27 (see VERSIONS). Since glibc 2.9, the eventfd() wrapper will employ the eventfd2() system call, if it is supported by the kernel. eventfd (. pipe(2)) , . eventfd , ( , ). eventfd , , KAIO ( AIO), , - . eventfd , select(2), poll(2) epoll(7). , "" , eventfd. ( eventfd() select(2), poll(2) epoll(7).) eventfd /proc/pid/fdinfo. proc(5). eventfd . , , , eventfd. , eventfd. : $ ./a.out 1 2 4 7 14 Child writing 1 to efd Child writing 2 to efd Child writing 4 to efd Child writing 7 to efd Child writing 14 to efd Child completed write loop Parent about to read Parent read 28 (0x1c) from efd #include #include #include #include #include #include int main(int argc, char *argv[]) { int efd; uint64_t u; ssize_t s; if (argc < 2) { fprintf(stderr, "Usage: %s ...\n", argv[0]); exit(EXIT_FAILURE); } efd = eventfd(0, 0); if (efd == -1) err(EXIT_FAILURE, "eventfd"); switch (fork()) { case 0: for (size_t j = 1; j < argc; j++) { printf("Child writing %s to efd\n", argv[j]); u = strtoull(argv[j], NULL, 0); /* strtoull() allows various bases */ s = write(efd, &u, sizeof(uint64_t)); if (s != sizeof(uint64_t)) err(EXIT_FAILURE, "write"); } printf("Child completed write loop\n"); exit(EXIT_SUCCESS); default: sleep(2); printf("Parent about to read\n"); s = read(efd, &u, sizeof(uint64_t)); if (s != sizeof(uint64_t)) err(EXIT_FAILURE, "read"); printf("Parent read %"PRIu64" (%#"PRIx64") from efd\n", u, u); exit(EXIT_SUCCESS); case -1: err(EXIT_FAILURE, "fork"); } } . futex(2), pipe(2), poll(2), read(2), select(2), signalfd(2), timerfd_create(2), write(2), epoll(7), sem_overview(7) Azamat Hackimov , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . eventfd(2)