linux多进程通讯---信号新增API timerfd

发布时间:2026/9/23 2:33:06
linux多进程通讯---信号新增API  timerfd 文章目录定时器说明举例定时器说明•函数功能通过文件I/O方式去获取定时器的通知事件•相关API–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, sturct itimerspec *curr_value);举例#include stdio.h #include stdlib.h #include unistd.h #include stdint.h #include time.h #include sys/time.h #include sys/timerfd.h #define handle_error(msg) \ do{perror(msg);exit(EXIT_FAILURE);}while(0) void print_time () { struct timeval tv; gettimeofday (tv, NULL); printf (current time: %ld.%ld\n, tv.tv_sec, tv.tv_usec); } int main (int argc, char *argv[]) { struct timespec now; if (clock_gettime (CLOCK_REALTIME, now) -1) handle_error (clock_gettime); struct itimerspec new_value; new_value.it_value.tv_sec now.tv_sec 10; // 10s later,timer will timeout new_value.it_value.tv_nsec now.tv_nsec; new_value.it_interval.tv_sec 3; //timer intelval new_value.it_interval.tv_nsec 0; int fd timerfd_create (CLOCK_REALTIME, 0); if (fd -1) handle_error (timerfd_creat); if (timerfd_settime(fd, TFD_TIMER_ABSTIME, new_value, NULL) -1) handle_error (timerfd_settime); print_time (); printf (timer started\n); for (uint64_t tot_exp 0; tot_exp 20;) //timer run times: 20 { uint64_t exp; ssize_t s read (fd, exp, sizeof(uint64_t)); if (s ! sizeof(uint64_t)) handle_error (read); tot_exp exp; print_time (); printf (read: %llu, total: %llu\n, exp, tot_exp); } return 0; }

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询