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

Linux 定时器应用示例(修正版)

1.运行果

2.程序

#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include <string.h> // For memsetint count = 0;void timer_handler(int sig) {printf("timer_handler: signal=%d, count=%d\n", sig, ++count);if(count >= 8) {printf("cancel timer\n");// Zero out the timer to cancel itstruct itimerval cancel_timer;memset(&cancel_timer, 0, sizeof(cancel_timer));setitimer(ITIMER_VIRTUAL, &cancel_timer, NULL);}
}int main() {struct itimerval timer;// Set up signal handler for SIGVTALRMsignal(SIGVTALRM, timer_handler);// Configure timer to fire every 1 second (initial + interval)timer.it_value.tv_sec = 1;       // First expiration after 1 sectimer.it_value.tv_usec = 0;timer.it_interval.tv_sec = 1;    // Subsequent intervals of 1 sectimer.it_interval.tv_usec = 0;setitimer(ITIMER_VIRTUAL, &timer, NULL);printf("looping: enter Control-C to terminate\n");printf("Hello World!\n-----------------------------------\n");while(1); // Infinite loop to keep the program running
}

 

http://www.lryc.cn/news/590180.html

相关文章:

  • GIT版本回退
  • Python中可以反转的数据类型
  • GaussDB 数据库架构师修炼(五) 存储容量评估
  • 搜索框的显示与隐藏(展开与收起)
  • el-input 回显怎么用符号¥和变量拼接展示?
  • openEuler 22.03 LTS Rootless Docker 安装指南
  • MongoDB复杂查询 聚合框架
  • 洛谷 P11247 [GESP202409 六级] 算法学习-普及/提高-
  • pymongo库:简易方式存取数据
  • ETL还是ELT,大数据处理怎么选更靠谱?
  • 步态循环(Gait Cycle)
  • 【MySQL事务和锁】回顾事务
  • 图像质量评价(Image Quality Assessment,IQA)
  • 调试bug记录
  • 【基于飞浆训练车牌识别模型】
  • Docker——Redis
  • 【C语言网络编程】HTTP 客户端请求(发送请求报文过程)
  • Mybatis07-缓存
  • 比特币技术简史 第二章:密码学基础 - 哈希函数、公钥密码学与数字签名
  • 今日行情明日机会——20250716
  • S7-200 SMART PLC:模拟量模块接线全解析
  • 汽车功能安全-相关项集成和测试(系统集成测试系统合格性测试)-12
  • xss-labs通关
  • “Datawhale AI夏令营”基于带货视频评论的用户洞察挑战赛2
  • lesson15:Python的文件操作
  • Docker 中的动态配置:docker update 命令与环境变量管理
  • Hadoop架构演进:从1.0到2.0的深度对比与优化解析
  • Docker 安装和配置 MySQL 8.0.36 的详细步骤
  • 力扣-146.LRU缓存机制
  • Linux-局域网构建+VLAN 划分 + 端口 MAC-IP 绑定 + 静态 DHCP