Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2173字,纯文字阅读大概需要4分钟。
内容图文
![Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行](/upload/InfoBanner/zyjiaocheng/965/ee56ea5e71bc43788b0709c6a412f57d.jpg)
需要每X(例如5)秒调用一个函数,下面的代码就是这样做的.但它阻止了代码的执行.因为我希望它像setitimer一样工作,我可以说每5秒调用一次函数并做其他事情.谢谢
#include <sys/timerfd.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h> /* 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(秒内的纳秒数).
内容总结
以上是互联网集市为您收集整理的Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行全部内容,希望文章能够帮你解决Linux timerfd,每隔x秒调用一次函数,不会阻塞代码执行所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。