导航

信号处理

  • sigaction
#include <signal.h>


int sigaction(int sig, const struct sigaction *act, struct sigaction *oldact);
  • 和signal比较
    • 移植性更佳 
  • 常用信号
SIGINT Ctrl-C,终止进程
SIGALRM 调用alarm和setitimer设置的定时器到期
SIGABRT 调用abort
SIGCHLD 父进程的某一子进程终止
SIGQUIT Ctrl-\,终止进程,生成core
  • struct sigaction
struct sigaction {//简化版,相关成员
    void (*sa_handler)(int);
    sigset_t sa_mask;

    int sa_flags;
    void (*sa_restorer)(void);
};
  • 测试代码
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>


void handler(int a)
{
        printf("This is func which process sig: %d.\n", a);
}


int main(void)
{
        struct sigaction sa;
        int a;

        bzero(&sa, sizeof(struct sigaction));
        sigemptyset(&sa.sa_mask);
        sa.sa_flags = 0;

        sa.sa_handler = handler;

        sigaction(SIGALRM, &sa, NULL);
        alarm(5);
        while (EOF != scanf("%d", &a)){
                printf("%d\n", a);
        }

        return 0;
}

 

posted on 2021-04-26 10:34  toughcactus  阅读(110)  评论(0编辑  收藏  举报