Reputation: 12064
I have created a timer which can expire, in 5 seconds using timerfd_create
, but i can see that it is waiting indefinitely.
Can someone help me?
Thanks in advance.
Here is my code:
enter code here
#include <sys/timerfd.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/epoll.h>
#include <time.h>
int main()
{
struct itimerspec its;
struct epoll_event event, revent;
int timer_fd, efd;
/* Setting timer interval */
its.it_interval.tv_sec=1;
its.it_interval.tv_nsec=0;
/* Setting timer expiration */
its.it_value.tv_sec=5;
its.it_value.tv_nsec=0;
efd=epoll_create(2);
event.data.fd=timer_fd;
event.events=EPOLLIN;
epoll_ctl(efd, EPOLL_CTL_ADD, timer_fd, &event);
timer_fd=timerfd_create(CLOCK_REALTIME, 0);
if(timer_fd==-1)
{
perror("timerfd:");
}
if(timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL)==-1)
{
perror("timerfd_settime error:");
}
printf("Starting the timer...");
while(1) {
epoll_wait(efd, &revent, 1, -1);
}
}
Upvotes: 1
Views: 3208
Reputation: 84169
Reverse the order of calls to epoll_ctl
and timerfd_create
. Right now you are adding some random integer value to the event set.
Several points:
timerfd_create(2)
produces a file descriptor, just like open(2)
or socket(2)
. You have to assign the return value to the timer_fd
variable before giving it to the epoll_ctl(2)
, otherwise it's just a random integer value from the stack.TFD_TIMER_ABSTIME
- you are asking the kernel to start a timer that expires one second after the Epoch (which is not that big of a deal - it'll just expire immediately).epoll_wait(2)
returns the number of ready file descriptors, 1
in your example, and you are expected to handle that. You, on the other hand, just ignore that return value and spin around in a tight loop, so you don't even know the timer is expiring.epoll_wait(2)
will just return immediately since the descriptor remains in the "signaled" state.errno(3)
- manual page for each call gives you possible error values.You do want a loop around the epoll_wait(2)
(or select(2)
. or poll(2)
), but you need:
Hope this helps.
Upvotes: 3