为什么信号处理程序进入无限循环? – SIGSEGV

任何想法为什么信号处理程序进入无限循环?

这是代码。 请帮帮我。

enter code here 9 void SIGSEGV_handler(int signal) 10 { 11 printf("Segmentation fault caught....\n"); 12 printf("Value of instance variable: i = %d\n\n", i); 13 } 16 17 int main() 18 { 19 char *mallocPtr, *callocPtr, *reallocPtr, *memalignPtr, *vallocPtr; 20 struct sigaction sa; 21 22 sa.sa_handler=SIGSEGV_handler; 23 sigaction(SIGSEGV, &sa, NULL); 24 37 38 printf("The segmentation fault handler will be entered for i = 3, 4, 5 and 6\n"); 39 40 41 for(i=0; i<7; i++) 42 { 43 printf("i = %d\n",i); 44 45 mallocPtr=(char*)malloc(3); 46 printf("Malloc address : %x\n",mallocPtr); 47 strcpy(mallocPtr, "Hhvhgvghsvxhvshxv"); 48 puts(mallocPtr); 

SIGSEGV的默认操作是终止你的进程。 但是你安装一个处理程序并覆盖它:

 /* Does nothing to "fix" what was wrong with the faulting * instruction. */ void SIGSEGV_handler(int signal) { printf("Segmentation fault caught....\n"); printf("Value of instance variable: i = %d\n\n", i); } 

因此,对于触发sigsegv的每条指令,调用此处理程序并重新启动指令 。 但是你的处理程序并没有做任何事情来解决错误指令的问题。

总之,当指令重新启动时,会再次发生故障。 再一次,然后……你明白了。

http://pubs.opengroup.org/onlinepubs/009604599/functions/xsh_chap02_04.html#tag_02_04

SIGBUS,SIGFPE,SIGILL或SIGSEGV信号的信号捕获函数正常返回,而不是由kill(),sigqueue()或raise()生成。