加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Linux > 正文

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

发布时间:2020-12-13 22:49:26 所属栏目:Linux 来源:网络整理
导读:需要每X(例如5)秒调用一个函数,下面的代码就是这样做的.但它阻止了代码的执行.因为我希望它像setitimer一样工作,我可以说每5秒调用一次函数并做其他事情.谢谢 #include sys/timerfd.h #include time.h #include unistd.h #include stdlib.h #include stdio.h
需要每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 startedn");
       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=%llun",(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(秒内的纳秒数).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读