Linux 提供的基于文件描述符的定时器接口

浮云一梦 2010-09-09 10:22:01
#include <sys/timerfd.h>

int timerfd_create(int clockid, int flags);
int timerfd_settime(int fd, int flags,
const struct itimerspec *new_value,
struct itimerspec *old_value);
int timerfd_gettime(int fd, struct itimerspec *curr_value);

不是说这个定时器,支持 select(2) 之类的吗,为什么我用select就接收不到定时器的超时信号。
而直接 read却能收到定时器的信号???

...全文
123 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
浮云一梦 2010-09-09
  • 打赏
  • 举报
回复
The file descriptor is readable (the select(2) readfds argument; the
poll(2) POLLIN flag) if one or more timer expirations have occurred.

这里说的检查从 readfds 中的描述符。
浮云一梦 2010-09-09
  • 打赏
  • 举报
回复
一样的,全都返回0,没有收到信号。
呵呵,奇怪!
justkk 2010-09-09
  • 打赏
  • 举报
回复
猜测一下,也许不是通过readfds返回呢,试试writefds或者exceptfds
或者,你3个fd_set都设置一下,看有什么返回

你可以看看这种定时器的联机帮助,对于select()是怎么支持的
浮云一梦 2010-09-09
  • 打赏
  • 举报
回复
对呀,我一值打印,都N久了没见到有定时器信号输出。

但如果我不用 select 的话,就能隔10秒输出定时器触发信号
justkk 2010-09-09
  • 打赏
  • 举报
回复
select()返回0表示超时了吧,那就是3秒之内定时器没有触发吧
浮云一梦 2010-09-09
  • 打赏
  • 举报
回复
这个功能好像是 2.6.25中才加进去的吧。
返回值全是0.
justkk 2010-09-09
  • 打赏
  • 举报
回复
在我的环境中还没有这个功能。。
你的select()的返回值是什么?
浮云一梦 2010-09-09
  • 打赏
  • 举报
回复

int fd = -1, max_loop = 100;
struct itimerspec new_value;
ssize_t len = 0;
long long sig_timer;

new_value.it_value.tv_sec = 20;
new_value.it_value.tv_nsec = 1000;

new_value.it_interval.tv_sec = 10;
new_value.it_interval.tv_nsec = 1000;

fd = timerfd_create(CLOCK_REALTIME, 0);
if( -1 == fd )
{
printf("Timer create Error!\n");
exit(-1);
}

if( -1 == timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL))
{
printf("Timer setting Error:%s\n", strerror(errno));
exit(-1);
}

fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
struct timeval tv;
tv.tv_sec = 3;
tv.tv_usec = 1000;


while(max_loop)
{
len = 0;
int retval = select( fd+1, &rfds, NULL, NULL, &tv);
if( retval < 0 )
{
printf("select error \n");
}
if( retval > 0 )
{
if( FD_ISSET(fd, &rfds) )
{
len = read(fd, (void*)&sig_timer, sizeof(long long) );
printf("Loop:%d Len = %u, sig_timer = %lld \n", max_loop, len, sig_timer);
max_loop --;
}
}
else
{
//sleep( 3 );
printf( "select retval %d \n", retval );
}
}
justkk 2010-09-09
  • 打赏
  • 举报
回复
没用过..
贴代码看看

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧