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

linux c 应用编程定时器函数

在 Linux C 应用编程中,对于多线程编程中的定时器函数使用,通常可以借助 pthread 库和系统提供的定时器相关的函数来实现。

首先,常见的定时器函数有 setitimer() 和 alarm() 。setitimer() 函数可以更精确地设置定时器,它可以设置为 ITIMER_REAL(以实时时间递减)、ITIMER_VIRTUAL(以进程在用户态执行的时间递减)和 ITIMER_PROF(以进程在用户态和内核态执行的时间递减)三种模式。

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>#define TIMER_INTERVAL_SEC 2void timer_handler(union sigval sv)
{printf("Timer expired. Thread ID: %ld\n", pthread_self());
}void* thread_function(void* arg)
{timer_t timer_id;struct sigevent sev;sev.sigev_notify = SIGEV_THREAD;sev.sigev_notify_function = timer_handler;sev.sigev_value.sival_ptr = &timer_id;timer_create(CLOCK_REALTIME, &sev, &timer_id);struct itimerspec its;its.it_interval.tv_sec = TIMER_INTERVAL_SEC;its.it_interval.tv_nsec = 0;its.it_value.tv_sec = 1;its.it_value.tv_nsec = 0;timer_settime(timer_id, 0, &its, NULL);while (1) {sleep(1);}timer_delete(timer_id);pthread_exit(NULL);
}int main()
{pthread_t thread_id1, thread_id2;pthread_create(&thread_id1, NULL, thread_function, NULL);pthread_create(&thread_id2, NULL, thread_function, NULL);pthread_join(thread_id1, NULL);pthread_join(thread_id2, NULL);return 0;
}
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>#define TIMER_SIG SIGRTMINvoid timer_handler(int sig, siginfo_t *si, void *uc) {// 定时器到期时被调用的处理函数printf("Timer fired!\n");
}int main() {timer_t timerid;struct sigevent sev;struct itimerspec its;struct sigaction sa;// 设置处理函数sa.sa_flags = SA_SIGINFO;sa.sa_sigaction = timer_handler;sigemptyset(&sa.sa_mask);if (sigaction(TIMER_SIG, &sa, NULL) == -1) {perror("sigaction");exit(1);}// 创建定时器sev.sigev_notify = SIGEV_SIGNAL;sev.sigev_signo = TIMER_SIG;sev.sigev_value.sival_ptr = &timerid;if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {perror("timer_create");exit(1);}// 启动定时器its.it_value.tv_sec = 2; // 初次到期时间,2秒后its.it_value.tv_nsec = 0;its.it_interval.tv_sec = 1; // 定时器周期,每隔1秒its.it_interval.tv_nsec = 0;if (timer_settime(timerid, 0, &its, NULL) == -1) {perror("timer_settime");exit(1);}sleep(10); // 等待定时器触发几次return 0;
}

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

相关文章:

  • 设备调试上位机GUI
  • 项目管理系统厂商:奥博思发布《项目管理系统助力 IPD 高效落地》演讲
  • Java项目总结1
  • Java中的类加载机制详解
  • SwiftUI 中 Grid 内多个 NavigationLink 同时发生导航之诡异问题的解决
  • 51单片机第21步_将TIM0用作两个8位定时器同时将TIM1用作波特率发生器
  • API-元素尺寸与位置
  • C语言中的基础指针操作
  • LabVIEW环境下OCR文字识别的实现策略与挑战解析
  • 中英双语介绍美国的州:堪萨斯州(Kansas)
  • 信息收集---端口服务信息收集
  • Python知识点背诵手册,超详细知识梳理
  • 【Pytorch实用教程】如何在多个GPU上使用分布式数据并行进行训练模型
  • PIL,OpenCV,Pytorch处理图像时的通道顺序(颜色,长宽深)
  • 经纬恒润亮相2024世界智能产业博览会
  • Python序列化和反序列化
  • Stream toArray 好过collect
  • qt/c++/mysql教务管理系统
  • Echarts公共方法
  • 鸿蒙学习(二)
  • 企业机构营销目前106短信群发还有用吗?此文告诉你该如何抉择!
  • DJYGUI AI低代码图形编程开发平台:开启嵌入式软件图形编程新纪元
  • 为什么不能在foreach中删除元素
  • python学习-tuple及str
  • Python深度理解系列之【排序算法——冒泡排序】
  • 边界框在目标检测中的作用与应用
  • linux 环境报错:Peer reports incompatible or unsupported protocol version
  • 深入解析:Java爬虫的本质是什么?
  • 【Matlab 六自由度机器人】机器人动力学之推导拉格朗日方程(附MATLAB机器人动力学拉格朗日方程推导代码)
  • 线下生鲜蔬果店做小程序有什么方法