timerfd_create(2) System Calls Manual timerfd_create(2) timerfd_create, timerfd_settime, timerfd_gettime - (libc -lc) #include int timerfd_create(int clockid, int flags); int timerfd_settime(int fd, int flags, const struct itimerspec *new_value, struct itimerspec *_Nullable old_value); int timerfd_gettime(int fd, struct itimerspec *curr_value); . setitimer(2) timer_create(2) select(2) poll(2) epoll(7). timer_create(2) timer_settime(2) timer_gettime(2). ( timer_getoverrun(2) read(2) .) timerfd_create() timerfd_create() . clockid : CLOCK_REALTIME . CLOCK_MONOTONIC . CLOCK_BOOTTIME ( 3.15) CLOCK_MONOTONIC . CLOCK_MONOTONIC CLOCK_BOOTTIME . . CLOCK_REALTIME . CLOCK_REALTIME_ALARM ( 3.11) CLOCK_REALTIME . CAP_WAKE_ALARM . CLOCK_BOOTTIME_ALARM ( 3.11) CLOCK_BOOTTIME . CAP_WAKE_ALARM . clock_getres(2) . clock_gettime(2). 2.6.27 OR flags timerfd_create(): TFD_NONBLOCK O_NONBLOCK ( open(2)) . fcntl(2) . TFD_CLOEXEC (FD_CLOEXEC) . O_CLOEXEC open(2) . 2.6.26 flags . timerfd_settime() () () timerfd_settime() fd. new_value . itimerspec itimerspec(3type). new_value.it_value . new_value.it_value . . new_value.it_interval . new_value.it_value. new_value ( new_value.it_value clockid). flags. flags : TFD_TIMER_ABSTIME new_value.it_value . new_value.it_value. TFD_TIMER_CANCEL_ON_SET TFD_TIMER_ABSTIME CLOCK_REALTIME CLOCK_REALTIME_ALARM (settimeofday(2) clock_settime(2) ). read(2) ECANCELED. old_value NULL itimerspec timerfd_gettime() . timerfd_gettime() timerfd_gettime() curr_value itimerspec fd. it_value . . TFD_TIMER_ABSTIME . it_interval . curr_value.it_value. timerfd_create() : read(2) timerfd_settime() read(2) read(2) 8 (uint64_t) . ( -- .) read(2) EAGAIN ( fcntl(2) F_SETFL O_NONBLOCK). read(2) EINVAL 8 . CLOCK_REALTIME CLOCK_REALTIME_ALARM (TFD_TIMER_ABSTIME) TFD_TIMER_CANCEL_ON_SET timerfd_settime() read(2) ECANCELED . ( .) CLOCK_REALTIME CLOCK_REALTIME_ALARM (TFD_TIMER_ABSTIME) TFD_TIMER_CANCEL_ON_SET timerfd_settime() ( clock_settime(2)) read(2) 0 ( ) read(2) . poll(2) select(2) ( ) ( select(2) readfds poll(2) POLLIN) . : pselect(2) ppoll(2) epoll(7). ioctl(2) timerfd : TFD_IOC_SET_TICKS ( 3.17) . 8 (uint64_t*) . . /. CONFIG_CHECKPOINT_RESTORE. close(2) . . fork(2) fork(2) timerfd_create(). read(2) . execve(2) timerfd_create() execve(2) . timerfd_create() . -1 errno . timerfd_settime() timerfd_gettime() 0 -1 errno . timerfd_create() : EINVAL clockid . EINVAL flags 2.6.26 flags . EMFILE . ENFILE . ENODEV inode () . ENOMEM . EPERM clockid CLOCK_REALTIME_ALARM CLOCK_BOOTTIME_ALARM CAP_WAKE_ALARM. timerfd_settime() timerfd_gettime() : EBADF fd . EFAULT new_value old_value curr_value . EINVAL fd timerfd . timerfd_settime() : ECANCELED . EINVAL new_value ( tv_nsec 999,999,999). EINVAL flags . . 2.6.25 glibc 2.8. CLOCK_REALTIME CLOCK_REALTIME_ALARM timerfd_create(): (1) (timerfd_settime()) TFD_TIMER_ABSTIME TFD_TIMER_CANCEL_ON_SET (2) ( settimeofday(2)) CLOCK_REALTIME (3) timerfd_settime() ( read(2) ). : o timerfd_settime() -1 errno ECANCELED. ( .) o timerfd_settime() . ( ). timerfd_create() timer_create(2). . . . . . . : $ a.out 3 1 100 0.000: timer started 3.000: read: 1; total=1 4.000: read: 1; total=2 ^Z # type control-Z to suspend the program [1]+ Stopped ./timerfd3_demo 3 1 100 $ fg # Resume execution after a few seconds a.out 3 1 100 9.660: read: 5; total=7 10.000: read: 1; total=8 11.000: read: 1; total=9 ^C # type control-C to suspend the program #include #include #include #include #include #include #include #include static void print_elapsed_time(void) { int secs, nsecs; static int first_call = 1; struct timespec curr; static struct timespec start; if (first_call) { first_call = 0; if (clock_gettime(CLOCK_MONOTONIC, &start) == -1) err(EXIT_FAILURE, "clock_gettime"); } if (clock_gettime(CLOCK_MONOTONIC, &curr) == -1) err(EXIT_FAILURE, "clock_gettime"); secs = curr.tv_sec - start.tv_sec; nsecs = curr.tv_nsec - start.tv_nsec; if (nsecs < 0) { secs--; nsecs += 1000000000; } printf("%d.%03d:\t", secs, (nsecs + 500000) / 1000000); } int main(int argc, char *argv[]) { int fd; ssize_t s; uint64_t expir, tot_expir, max_expir; struct timespec now; struct itimerspec new_value; if (argc != 2 && argc != 4) { fprintf(stderr, "%s init-secs [interval-secs max-num-expir]\n", argv[0]); exit(EXIT_FAILURE); } if (clock_gettime(CLOCK_REALTIME, &now) == -1) err(EXIT_FAILURE, "clock_gettime"); /* Create a CLOCK_REALTIME absolute timer with initial expiration and interval as specified in command line. */ new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]); new_value.it_value.tv_nsec = now.tv_nsec; if (argc == 2) { new_value.it_interval.tv_sec = 0; max_expir = 1; } else { new_value.it_interval.tv_sec = atoi(argv[2]); max_expir = atoi(argv[3]); } new_value.it_interval.tv_nsec = 0; fd = timerfd_create(CLOCK_REALTIME, 0); if (fd == -1) err(EXIT_FAILURE, "timerfd_create"); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1) err(EXIT_FAILURE, "timerfd_settime"); print_elapsed_time(); printf("timer started\n"); for (tot_expir = 0; tot_expir < max_expir;) { s = read(fd, &expir, sizeof(uint64_t)); if (s != sizeof(uint64_t)) err(EXIT_FAILURE, "read"); tot_expir += expir; print_elapsed_time(); printf("read: %w64u; total=%w64u\n", expir, tot_expir); } exit(EXIT_SUCCESS); } eventfd(2), poll(2), read(2), select(2), setitimer(2), signalfd(2), timer_create(2), timer_gettime(2), timer_settime(2), timespec(3), epoll(7), time(7) 3 . . : . 6.18 16 2026 timerfd_create(2)