linux c 应用编程定时器函数

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

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

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>

#define TIMER_INTERVAL_SEC 2

void 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;
}
cpp 复制代码
#include <stdio.h>
#include <time.h>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>

#define TIMER_SIG SIGRTMIN

void 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;
}
相关推荐
belldeep2 小时前
如何阅读、学习 Tcc (Tiny C Compiler) 源代码?如何解析 Tcc 源代码?
c语言·开发语言
LuckyTHP2 小时前
java 使用zxing生成条形码(可自定义文字位置、边框样式)
java·开发语言·python
Blossom.1185 小时前
使用Python实现简单的人工智能聊天机器人
开发语言·人工智能·python·低代码·数据挖掘·机器人·云计算
da-peng-song5 小时前
ArcGIS Desktop使用入门(二)常用工具条——数据框工具(旋转视图)
开发语言·javascript·arcgis
galaxy_strive5 小时前
qtc++ qdebug日志生成
开发语言·c++·qt
TNTLWT6 小时前
Qt功能区:简介与安装
开发语言·qt
等等5437 小时前
Java EE初阶——wait 和 notify
java·开发语言
低代码布道师7 小时前
第五部分:第一节 - Node.js 简介与环境:让 JavaScript 走进厨房
开发语言·javascript·node.js
盛夏绽放7 小时前
Python字符串常用方法详解
开发语言·python·c#
小狗祈祷诗8 小时前
day22-数据结构之 栈&&队列
c语言·数据结构