为什么信号处理程序去无限循环?

编程入门 行业动态 更新时间:2024-10-18 10:30:38
本文介绍了为什么信号处理程序去无限循环? - SIGSEGV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

下面是code。请帮我。

在此输入code 9无效SIGSEGV_handler(INT信号)10 {11的printf(分段错抓.... \\ n);12的printf(实例变量价值:I =%d个\\ n \\ n,I);13}1617 INT的main()18 {19字符* mallocPtr,* callocPtr,* reallocPtr,* memalignPtr,* vallocPtr;20结构的sigaction SA;2122 sa.sa_handler = SIGSEGV_handler;23的sigaction(SIGSEGV,&安培; SA,NULL);243738的printf(分割故障处理程序将对于i = 3,4,5和6 \\ n进入了)394041,用于(ⅰ= 0; I&下; 7;我+ +)42 {43的printf(I =%d个\\ N,I);4445 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.

更多推荐

为什么信号处理程序去无限循环?

本文发布于:2023-10-09 05:53:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474885.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:信号处理   程序

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!