Skip to content
Advertisement

timer using timerfd_create() and timerfd_settime() continually expires even though interval is set to zero

Here is the stripped-down code. The timer expires(reported by epoll_wait) in time but even though interval is set to zero, epoll_wait continually triggers a ready-to-read event (EPOLLIN) for the timer fd. Should it not be triggered just once as interval is set to 0?

timerFd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
struct itimerspec *my_itimerspec_new, *my_itimerspec_old;
my_itimerspec_new = malloc(sizeof(struct itimerspec));
my_itimerspec_old = malloc(sizeof(struct itimerspec));

bzero(my_itimerspec_new, sizeof(struct itimerspec));
bzero(my_itimerspec_old, sizeof(struct itimerspec));


my_itimerspec_new->it_value.tv_sec  = 60;
my_itimerspec_new->it_value.tv_nsec = 0; 
my_itimerspec_new->it_interval.tv_sec  = 0; 
my_itimerspec_new->it_interval.tv_nsec = 0; 

my_itimerspec_old->it_value.tv_sec  = 0; 
my_itimerspec_old->it_value.tv_nsec = 0; 
my_itimerspec_old->it_interval.tv_sec  = 0; 
my_itimerspec_old->it_interval.tv_nsec = 0; 

timerfd_settime(timerFd, 0, my_itimerspec_new, my_itimerspec_old);

Advertisement

Answer

epoll, poll(), select(), etc. will tell you if the timer descriptor is readable without blocking. It becomes readable when the timer expires and remains so until you read() a 64 bit unsigned integer from it that contains the count of times the timer expired since the last read. If you don’t do that, it continues to poll as readable (modulo however ET and ONESHOT options affect the behavior if you’re using those)

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement