知道为什么信号处理程序进入无限循环吗?
这是代码。请帮我。
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是终止您的进程。但是,您可以安装处理程序并覆盖此方法:
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的指令,都会调用此处理程序, 然后重新启动指令 。但是您的处理程序一开始并没有采取任何措施来纠正错误指令的错误。
总之,当指令重新启动时,它将再次出错。一遍又一遍,…你明白了。