为什么信号处理程序去无限循环? - SIGSEGV [英] Why signal handler goes to infinite loop? - SIGSEGV

查看:180
本文介绍了为什么信号处理程序去无限循环? - SIGSEGV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

下面是code。
请帮我。

 在此输入code
 9无效SIGSEGV_handler(INT信号)
10 {
11的printf(分段错抓.... \\ n);
12的printf(实例变量价值:I =%d个\\ n \\ n,I);
13}
16
17 INT的main()
18 {
19字符* mallocPtr,* callocPtr,* reallocPtr,* memalignPtr,* vallocPtr;
20结构的sigaction SA;
21
22 sa.sa_handler = SIGSEGV_handler;
23的sigaction(SIGSEGV,&安培; SA,NULL);
24
37
38的printf(分割故障处理程序将对于i = 3,4,5和6 \\ n进入了)
39
40
41,用于(ⅰ= 0; I&下; 7;我+ +)
42 {
43的printf(I =%d个\\ N,I);
44
45 mallocPtr =(字符*)malloc的(3);
46的printf(malloc的地址:%x \\ n,mallocPtr);
47的strcpy(mallocPtrHhvhgvghsvxhvshxv);
48看跌期权(mallocPtr);


解决方案

SIGSEGV 默认行为是终止进程。但你安装一个处理程序,并覆盖此:

  / *不执行任何修复什么是错的断层
 *指令。
 * /
无效SIGSEGV_handler(INT信号)
{
    的printf(分段错抓.... \\ n);
    的printf(实例变量价值:I =%d个\\ n \\ n,I);
}

因此​​,对于每一个触发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. 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. 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.

这篇关于为什么信号处理程序去无限循环? - SIGSEGV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆