加入收藏 | 设为首页 | 会员中心 | 我要投稿 南平站长网 (https://www.0599zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 交互 > 正文

Linux 新的API signalfd、timerfd、eventfd使用说明

发布时间:2016-11-01 05:20:11 所属栏目:交互 来源:站长网
导读:副标题#e# Linux 新的API signalfd、timerfd、eventfd使用说明 三种新的fd加入linux内核的的版本: signalfd:2.6.22 timerfd:2.6.25 eventfd:2.6.22 三种fd的意义: l s ignalfd 传统的处理信号的方式是注册信号处理函数;由于信号是异步发生的,要解决

点击(此处)折叠或打开

  1. #include
  2. int timerfd_create(int clockid, int flags);
  3. int timerfd_settime(int fd, int flags, const struct itimerspec *new_value,struct itimerspec *old_value);
  4. int timerfd_gettime(int fd, struct itimerspec *curr_value);

timerfd_create:创建一个timerfd;返回的fd可以进行如下操作:read、select(poll、epoll)、close  

timerfd_settime:设置timer的周期,以及起始间隔  

timerfd_gettime:获取到期时间。 

点击(此处)折叠或打开

  1. //函数参数中数据结构如下:
  2. struct timespec
  3. {
  4.     time_t tv_sec; /* Seconds */
  5.     long tv_nsec; /* Nanoseconds */
  6. };
  7.   struct itimerspec
  8. {
  9.     struct timespec it_interval; /* Interval for periodic timer */
  10.     struct timespec it_value; /* Initial expiration */
  11. };

l例子


点击(此处)折叠或打开

  1.   #include
  2.   #include
  3.   #include
  4.   #include
  5.   #include
  6.   #include
  7.   #include /* Definition of uint64_t */
  8.  
  9.   #define handle_error(msg)
  10.   do { perror(msg); exit(EXIT_FAILURE); } while (0)
  11.  
  12.   void printTime()
  13.   {
  14.       struct timeval tv;
  15.       gettimeofday(&tv, NULL);
  16.       printf("printTime: current time:%ld.%ld ", tv.tv_sec, tv.tv_usec);
  17.   }
  18.  
  19.   int main(int argc, char *argv[])
  20.   {
  21.       struct timespec now;
  22.       if (clock_gettime(CLOCK_REALTIME, &now) == -1)
  23.           handle_error("clock_gettime");
  24.  
  25.       struct itimerspec new_value;
  26.       new_value.it_value.tv_sec = now.tv_sec + atoi(argv[1]);
  27.       new_value.it_value.tv_nsec = now.tv_nsec;
  28.       new_value.it_interval.tv_sec = atoi(argv[2]);
  29.       new_value.it_interval.tv_nsec = 0;
  30.  
  31.       int fd = timerfd_create(CLOCK_REALTIME, 0);
  32.       if (fd == -1)
  33.       handle_error("timerfd_create");
  34.  
  35.       if (timerfd_settime(fd, TFD_TIMER_ABSTIME, &new_value, NULL) == -1)
  36.           handle_error("timerfd_settime");
  37.  
  38.       printTime();
  39.       printf("timer startedn");
  40.  
  41.       for (uint64_t tot_exp = 0; tot_exp < atoi(argv[3]);)
  42.       {
  43.           uint64_t exp;
  44.           ssize_t s = read(fd, &exp, sizeof(uint64_t));
  45.           if (s != sizeof(uint64_t))
  46.               handle_error("read");
  47.  
  48.           tot_exp += exp;
  49.           printTime();
  50.           printf("read: %llu; total=%llun",exp, tot_exp);
  51.   }
  52.  
  53.   exit(EXIT_SUCCESS);
  54.  }


代码L25-L29:初始化定时器的参数,初始间隔与定时间隔。

L32:创建定时器fd,CLOCK_REALTIME:真实时间类型,修改时钟会影响定时器;CLOCK_MONOTONIC:相对时间类型,修改时钟不影响定时器。

L35:设置定时器的值。

L44:阻塞等待定时器到期。返回值是未处理的到期次数。比如定时间隔为2秒,但过了10秒才去读取,则读取的值是5。

编译运行:编译时要加rt库(g++ -lrt timerfd.cc -o timerfd)

(编辑:南平站长网)

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

热点阅读