• 【Linux】使用timer_create()创建定时器发送信号并使用sigaction()处理信号


    0x00 前言

    最后更新时间:2023-10-19

    0x01 主要函数介绍

    1.sigaction函数

    #include 
    int sigaction(int signum, const struct sigaction *act,struct sigaction *oldact);
    
    • 1
    • 2

    功能:
    用于改变进程接收到特定信号后的行为。

    参数:
    signum:要捕获的信号
    act:接收到信号之后对信号进行处理的结构体
    oldact:接收到信号之后,保存原来对此信号处理的各种方式与信号(可用来做备份)。如果不需要备份,此处可以填NULL

    返回值:
    成功返回0,失败返回-1,errno被设置。


    2.timer_create函数

    #include 
    #include 
    int timer_create(clockid_t clock_id, struct sigevent *evp, timer_t *timerid)
    
    • 1
    • 2
    • 3

    功能:
    创建特定的定时器

    参数:
    clock_id:可选系统的宏
    evp:环境值,结构体struct sigevent变量的地址
    timerid:定时器标识, 结构体timer_t变量的地址

    返回值:
    成功返回0,失败返回-1,errno被设置。


    3.timer_settime函数

    #include 
    int timer_settime(timer_t timerid, int flags,
                             const struct itimerspec *new_value,
                             struct itimerspec *old_value);
    
    • 1
    • 2
    • 3
    • 4

    功能:
    将创建特定的定时器关联到一个到期时间以及启动时钟周期。

    参数:
    timerid:定时器标识, 结构体timer_t变量的地址
    flags:为TIMER_ABSTIME,则new_value所指定的时间值会被解读成绝对值
    new_value:指定时间间隔的时间,结构体itimerspec变量的地址
    old_value:old_value的值不是NULL,则之前的定时器到期时间会被存入其所提供的itimerspec。如果定时器之前处在未启动状态,则此结构的成员全都会被设定成0。

    返回值:
    成功返回0,失败返回-1,errno被设置。

    0x02 代码实现

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    void do_handle()
    {
        printf("send signal\n");
    }
    
    void set_send_timer(int time)
    {
        struct sigaction sig_act;
        struct sigevent sig_event;
        struct itimerspec timer_setting;
        timer_t timer_id;
        
        sigemptyset(&sig_act.sa_mask);
        sig_act.sa_flags = 0;
        sig_act.sa_handler = do_handle;
        if (sigaction(SIGRTMAX - 1, &sig_act, NULL) < 0) //The range SIGRTMIN to SIGRTMAX.
        {
            return;
        }
    
        // set signal event for the timer timeout.
        sig_event.sigev_notify = SIGEV_SIGNAL;
        sig_event.sigev_signo = SIGRTMAX - 1;
        if (timer_create(CLOCK_REALTIME, &sig_event, &timer_id) < 0)
        {
            return;
        }
    
        // timer setting. The setting can be used by more than one timer.
        timer_setting.it_value.tv_sec = 0;
        timer_setting.it_value.tv_nsec = time * 1000000;
        timer_setting.it_interval.tv_sec = 0;
        timer_setting.it_interval.tv_nsec = time * 1000000;
        if (timer_settime(timer_id, 0, &timer_setting, NULL) < 0)
        {
            return;
        }
    }
    
    int main()
    {
        set_send_timer(100); // 100ms
    
        while (1)
        {
            sleep(1);
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60

    运行结果:
    编译时需加上 -lrt
    在这里插入图片描述
    以上。
    参考文档:
    1.http://kimi.it/508.html
    2.https://www.cnblogs.com/muyi23333/articles/13523251.html
    3.https://blog.csdn.net/qq_20853741/article/details/113547906

  • 相关阅读:
    Java刷题day29
    Django框架简介
    Web APIs:事件高级--事件对象
    <图像处理> 空间滤波基础
    机器学习终极指南:统计和统计建模03/3 — 第 -3 部分
    目标检测YOLO实战应用案例100讲-SAR图像多尺度舰船目标检测
    Linux:shell编程1(内含:1.shell简介+2.shell实操+3.shell的变量介绍+4.shell变量的定义)
    关键词搜索API接口之1688平台
    IOS安全测试学习-DVIA-v2
    四化智造MES(WEB)与金蝶云星空对接集成原材料/标准件采购查询(待采购)连通采购订单新增(原材料采购-采购订单(变更)-TEST)
  • 原文地址:https://blog.csdn.net/wangqingchuan92/article/details/133857758