简体   繁体   English

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

[英]Why signal handler goes to infinite loop? - SIGSEGV

Any idea why the signal handler goes to infinite loop? 知道为什么信号处理程序会进入无限循环吗?

Here is the code. 这是代码。 Please help me. 请帮我。

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);

The default action for SIGSEGV is to terminate your process. SIGSEGV的默认操作是终止您的进程。 But you install a handler and override this: 但是你安装了一个处理程序并覆盖它:

/* 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);
}

So for every instruction that triggers a sigsegv, this handler is called and the instruction is restarted . 因此,对于每个触发sigsegv的指令,都会调用此处理程序并重新启动指令 But your handler did nothing to fix what was wrong in the first place with the faulting instruction. 但是你的处理程序没有做任何事情来解决错误指令的第一个问题。

In conclusion, when the instruction is restarted, it will fault again. 总之,当指令重新启动时,它将再次发生故障。 And again, and again and... you get the idea. 又一次又一次......你明白了。

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

The behavior of a process is undefined after it returns normally from a signal-catching function for a SIGBUS, SIGFPE, SIGILL, or SIGSEGV signal that was not generated by kill(), sigqueue(), or raise(). 在正常从信号捕获函数返回不是由kill(),sigqueue()或raise()生成的SIGBUS,SIGFPE,SIGILL或SIGSEGV信号之后,进程的行为是未定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM