一个捕获linux ctrl+c的小程序

来源:互联网 发布:龙卷风软件下载 编辑:程序博客网 时间:2024/05/01 06:05


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>


void handler(int signo, siginfo_t * info, void * data)
{
printf("got a signal!\n");
exit(-1);
}




int main (void)
{
struct sigaction sa;
    
memset (&sa, 0, sizeof(struct sigaction));
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
    sigfillset (&sa.sa_mask);
    int err = sigaction (SIGINT, &sa, NULL);
if (err)
{
printf("set signal error!\n");
}
    
while(1)
{
sleep(1);
}

}
0 0