linux timerfd_settime函数,Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行

需要每X(例如5)秒调用一个函数,下面的代码就是这样做的.但它阻止了代码的执行.因为我希望它像setitimer一样工作,我可以说每5秒调用一次函数并做其他事情.谢谢

#include

#include

#include

#include

#include

#include /* Definition of uint64_t */

#define handle_error(msg) \

do { perror(msg); exit(EXIT_FAILURE); } while (0)

int

main(int argc, char *argv[])

{

struct itimerspec new_value;

int max_exp, fd;

struct timespec now;

uint64_t exp, tot_exp;

ssize_t s;

if (clock_gettime(CLOCK_REALTIME, &now) == -1)

handle_error("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 + 1;

new_value.it_value.tv_nsec = now.tv_nsec;

new_value.it_interval.tv_sec = 5;

new_value.it_interval.tv_nsec = 0;

max_exp = 5; //say 5 times

fd = timerfd_create(CLOCK_REALTIME, 0);

if (fd == -1)

handle_error("timerfd_create");

if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)

handle_error("timerfd_settime");

printf("timer started\n");

for (tot_exp = 0; tot_exp < max_exp;) {

s = read(fd, &exp, sizeof(uint64_t));

if (s != sizeof(uint64_t))

handle_error("read");

tot_exp += exp;

printf("read: %llu; total=%llu\n",

(unsigned long long) exp,

(unsigned long long) tot_exp);

}

//Do something else ?

//while(1);

exit(EXIT_SUCCESS);

}

编辑

我还有一个问题.

在上面的代码中更改这些行

new_value.it_interval.tv_sec = 5;

new_value.it_interval.tv_nsec = 0;

new_value.it_interval.tv_sec = 0;

new_value.it_interval.tv_nsec = 5000000000;

我看到没有5秒的延迟,这里发生了什么?

解决方法:

您需要了解如何使用多路复用系统调用(如poll(2)(或较旧的select(2),它往往会过时)并使用它们来测试在读取(2)之前由timerfd_create(2)获得的文件描述符的可读性.

但是,请注意timerfd_create仅在读取调用成功时才起作用.因此,只有当民意调查显示fd不可读时,你才能做其他事情.其他东西应该很快(持续不到5秒).

您可能想要调查事件循环库,例如libevent(包装民意调查)如果您正在编写图形应用程序(使用Qt或Gtk),它已经拥有自己的事件循环.如果足够聪明,你可以在没有任何timerfd_create的情况下进行5秒的周期,只需通过你的事件循环(通过仔细设置轮询的超时等).

附加物:

tv_nsec字段应始终为非负且小于1000000000(秒内的纳秒数).

标签:c-3,linux,timer

来源: https://codeday.me/bug/20190704/1374074.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值