首页 > 其他分享 >sigaction

sigaction

时间:2023-05-01 10:44:49浏览次数:37  
标签:int void value act sa sigaction

/*
        #include <signal.h>

        int sigaction(int signum, const struct sigaction *act,
                        struct sigaction *oldact);    
            功能:检查或改变信号的处理方式
            参数:
                signum:信号编号
                act:处理方式
                    struct sigaction {
                        // 指向信号处理函数的指针
                        void     (*sa_handler)(int);
                        // 不常用
                        void     (*sa_sigaction)(int, siginfo_t *, void *);
                        // 临时阻塞信号集
                        sigset_t   sa_mask;
                        // 标识使用哪个一个处理函数,0表示sa_handler,1表示sa_sigaction
                        int        sa_flags;
                        // 废弃
                        void     (*sa_restorer)(void);
                    };
                oldact:上一次对信号捕捉相关的设置,一般为NULL
            返回值:
                成功:0
                失败:-1
        







*/


#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>

void print(int num)
{
    
    printf("到时间啦!\n");
}


int main()
{

    // void (*handle) (int) = &print;
    // signal(SIGALRM, SIG_DFL);
    // signal(SIGALRM, SIG_IGN);

    // signal(SIGALRM, &print);

    struct sigaction act;
    act.sa_handler = &print;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);

    sigaction(SIGALRM, &act, NULL);




    struct itimerval new_value;
    new_value.it_interval.tv_sec = 2;
    new_value.it_interval.tv_usec = 0;

    new_value.it_value.tv_sec = 3;
    new_value.it_value.tv_usec = 0;

    int ret = setitimer(ITIMER_REAL, &new_value, NULL);
    if(ret == -1)
    {
        perror("setitimer");
        exit(-1);
    }
    

    // getchar();
    while(1);




    return 0;
}

 

标签:int,void,value,act,sa,sigaction
From: https://www.cnblogs.com/WTSRUVF/p/17366252.html

相关文章