- 论坛徽章:
- 0
|
写了一个小程序,意在发生segment fault后捕捉SIGSEGV信号,可是程序却一直运行不终止了。为何?
程序如下:
#include<stdio.h>
#include<signal.h>
void sig_cap(int signo, siginfo_t *p_info, void *info) {
printf("caputre signo: %d\n", signo);
printf("sigal number is:%d\n", p_info->si_signo);
printf("pid is %d\n", p_info->si_pid);
}
void sig_reg(int signo) {
struct sigaction sa;
sa.sa_flags = SA_SIGINFO;
sigemptyset(&sa.sa_mask);
sa.sa_sigaction = sig_cap;
sigaction(signo, &sa, NULL);
}
int main(int argc,char *argv[]){
int *p = NULL;
sig_reg(SIGSEGV);
sig_reg(SIGINT);
sig_reg(SIGQUIT);
printf("pid = %d\n", getpid());
*p = 2;
return 0;
}
gcc sigsegv.c -o sigsegv
./sigsegv
运行sigsegv后,程序一直打印,不终止。打印如下:
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
caputre signo: 11
sigal number is:11
pid is 0
|
|