知道为什么信号处理程序进入无限循环吗?
下面是代码。请帮帮我。
enter code here
9 void SIGSEGV_handler(int signal)
10 {
11 printf("Segmentation fault caught....n");
12 printf("Value of instance variable: i = %dnn", 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 6n");
39
40
41 for(i=0; i<7; i++)
42 {
43 printf("i = %dn",i);
44
45 mallocPtr=(char*)malloc(3);
46 printf("Malloc address : %xn",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 = %dnn", i);
}
因此,对于触发sigsegv的每个指令,该处理程序被称为,并且该指令被重新启动。但是你的处理程序没有做任何事情来修复错误指令的错误。
综上所述,当指令重新启动时,它会再次出错。一遍又一遍……
http://pubs.opengroup.org/onlinepubs/009604599/functions/xsh_chap02_04.html#tag_02_04
进程从信号捕获函数正常返回SIGBUS、SIGFPE、SIGILL或SIGSEGV信号后,其行为是未定义的,该信号不是由kill()、sigqueue()或raise()生成的。