更改默认操作在C语言中,我要赶在SIGINT
信号,并打印出一个消息像 使用的sigaction并通过无法为SIGINT
sa.sa_sigaction = handler;
我不“收到SIGINT”不想终止程序。
如果我通过shell运行程序并用Ctrl + c生成信号,信号处理程序将捕获信号并打印出我的信息。
之后,它将执行终止进程的默认操作。
我在做什么错?
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
static void handler(int sig, siginfo_t* si, void *unused){
if(sig == SIGINT){
printf("Signal %i received\n",si->si_signo);
}
}
int main(int argc, char* argv[]){
char s [256];
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, SIGINT);
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = handler;
if(sigaction(SIGINT, &sa, NULL) < 0){
perror("sigaction");
}
fgets(s,sizeof(s), stdin);
printf("%s", s);
return 0;
}
你在sigaction之后和从main返回之前做了什么? – rici
我更新了代码。我只是回应用户输入。 –
那么你怎么知道sigint终止你的程序?我认为它只是完成,因为fgets返回。 – rici