更改默認操作在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