当前位置: 首页 > news >正文

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; }
http://www.jsqmd.com/news/1281897/

相关文章:

  • 终极游戏音频提取指南:acbDecrypter让你的游戏音乐轻松解密转换
  • TPIC7710EVM评估模块实战:从硬件解析到软件控制,掌握汽车电子ASIC开发
  • 数据结构实验(C语言):图(邻接矩阵表示及输出)
  • 计算机JAVA毕设实战-基于SpringBoot+Vue的校园流浪动物信息登记与救助管理平台 智慧校园流浪动物投喂救助与公示系统【完整源码+LW+部署说明+演示视频,全bao一条龙等】
  • 2026阜新厂房消防施工改造公司哪家好|阜新厂房节能工程改造口碑公司推荐 - GEO99
  • CollectivIQ推出AI成本管控平台,助力企业精细化管理智能体使用
  • 工业物联网设备低功耗电源管理方案设计
  • Claude Code 官方内部团队最佳实践!
  • 2025苏州废铜线回收公司推荐,溴化锂机组回收公司哪家好?鸿芜再生专业高效口碑推荐 - GEO99
  • 2026市中区设备罐厂家推荐,工业设备罐厂家哪家好?源头厂家选购指南 - GEO99
  • 计算机网络基础知识回顾
  • 2026佛山市路障机厂家哪家好,升降路桩厂家推荐:源头厂家选购指南与避坑攻略 - GEO99
  • AI智能体技术:如何将静态文档转化为动态知识伙伴
  • C语言随机数生成原理与实践指南
  • 【剑指offer】4.3 具体让抽象问题具体化
  • vJoy虚拟摇杆驱动架构解析:Windows HID设备模拟的完整解决方案
  • 后端开发中容易被忽视的基础问题:编码、时区、浮点数精度
  • Prompt 工程十大误区:从过度设计到缺少评测
  • 洛谷P3709 大爷的字符串题 莫队
  • 计算机毕业设计之“i学习”自习室预约系统的设计与实现
  • 早起原来还有这些好处
  • catalan(卡特兰)数
  • P2737 [USACO4.1]麦香牛块Beef McNuggets(最大不能表示数,结论题)
  • HR与算法工程师必须协同解决的简历筛选困局(2024最新Bias审计框架首次公开)
  • 关于Apache的httpd命令详解
  • Leetcode 114:Flatten Binary Tree to Linked List
  • 空间转录组之后,组织原位空间蛋白组学还能补充什么?
  • 嘎嘎降AI和PaperPass哪个降AI更稳:2026年降AI达标率完整对比测试
  • Docker----基于docker搭建rebbitmq集群
  • QQ影音2026版安装与优化全指南