views:

322

answers:

3

Greetings,

I was wondering if there is a way to use a timer in combination with linux poll/epoll API. I already use epoll and it would integrate very nice with the existing code if i could make the timer to be just another epoll event in my queue.

A possible way is maybe, a file-based Timer, like

echo 400;now > /dev/timer ; cat /dev/timer ; # outputs after 400ms "now"

just as an imaginary example. Is there something like this? Or are there other ways i could integrate it with the epoll API?

--Marenz

A: 

Would the watch -n 0.4 command be any use in this situation?

Question Mark
I don’t think so. I mentioned the /dev/timer example, because /dev/timer could be opened using a filedescriptor and i manage file descriptors with epoll, so that way it would work, but not by executing another process.--Marenz
Marenz
+3  A: 

It is trivial to do timers with epoll, because epoll_wait also takes a timeout parameter; you simply put all your timers into a priority queue and set timeout to the time between now and the first one, then fire the events for that timer.

Or use use libevent which provides something similar.

Yes you COULD use timerfd() if you really wanted, but there's no need.

MarkR
It’s not exactly the way I was hoping for, but it still works.thanks.--Marenz
Marenz
A: 

I was trying to use epoll_wait with a timeout, but I could not get the wait to break, I am using a 2 seconds timeout, like:

nfds = epoll_wait(epfd, events, cur_nfds, 2000);

In this case, should all the FDs added be non blocking ones ? am I missing something here?

vyom
if i recall correctly, they need to be blocking ones, but i am not sure.
Marenz