信号处理问题 [英] signal handler question

查看:117
本文介绍了信号处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

Iam遇到信号处理程序问题。当特定信号收到
后,在调用信号处理程序后执行

将返回何处?将返回到我们注册信号的位置?

以下代码正在接收递归信号。


2 #include< stdio.h>

3 #include< stdlib.h>

4 #include< unistd.h>



6 void handle_sigsegv(int sig)

7 {

8 printf(抓住SIGSEGV !! \ n);

9信号( SIGSEGV,handle_sigsegv);

10睡觉(1);

11返回;

12}

13

14 void sig_segv(){

15 int * p = NULL;

16 * p = 10;

17}

18

19 int main()

20 {

21 int * p = NULL;

22信号(SIGSEGV,handle_sigsegv);

23 sig_segv();

24 printf(在segfault \ n之后);

25返回0;

26}


请点亮一下。

解决方案

" Udai Ki跑" < s。********* @ gmail.com写信息

news:ab ******************** ************** @ c30g2000 hsa.googlegroups.com ...


大家好

我遇到信号处理程序问题。当特定信号收到
后,在调用信号处理程序后执行

将返回何处?将返回到我们注册信号的位置?

以下代码正在接收递归信号。


2 #include< stdio.h>

3 #include< stdlib.h>

4 #include< unistd.h>



6 void handle_sigsegv(int sig)

7 {

8 printf(抓住SIGSEGV !! \ n);

9信号( SIGSEGV,handle_sigsegv);

10睡觉(1);

11返回;

12}

13

14 void sig_segv(){

15 int * p = NULL;

16 * p = 10;

17}

18

19 int main()

20 {

21 int * p = NULL;

22信号(SIGSEGV,handle_sigsegv);

23 sig_segv();

24 printf(在segfault \ n之后);

25返回0;

26}


请点亮一下。



不确定你要做什么。我看到你正在将新的

信号处理程序重置为信号处理程序中的新信号处理程序。也许你想要这样的东西:


#include< stdio.h>

#include< stdlib.h>

#include< errno.h>

#include< signal.h>

#include< assert.h>


void handle_sigsegv(int sig)

{

/ *请注意,我并没有真正处理任何事情* /

printf(" Caught SIGSEGV !! \ n");

断言(0);

返回;

}


void sig_segv(){

int * p = NULL;

* p = 10;

}


int main()

{

int * p = NULL;

信号(SIGSEGV,handle_sigsegv);

sig_segv();

printf("在segfault \ n"之后);

返回0 ;

}


在任何情况下,如果你收到分段违规,那就是需要修复
的错误。


-

通过 http://www.teranews.com


11月21,9:53 am,Udai Kiran< s.udayki ... @ gmail.comwrote:


大家好

Iam有信号处理程序的麻烦。当特定信号收到
后,在调用信号处理程序后执行

将返回何处?会回到我们注册信号的地步吗?



你无法预测。它类似于一个中断

处理程序并在任何时候生成异常时触发。


以下代码接收递归信号。


2 #include< stdio.h>

3 #include< stdlib.h>

4 #include< unistd。 h>

5

6 void handle_sigsegv(int sig)

7 {

8 printf(" Caught SIGSEGV !! \ n");

9信号(SIGSEGV,handle_sigsegv);

10睡眠(1);

11返回;

12}

13

14 void sig_segv(){

15 int * p = NULL;

16 * p = 10;

17}

18

19 int main()

20 {

21 int * p = NULL;

22信号(SIGSEGV,handle_sigsegv);

23 sig_segv();

24 printf(在segfault \ n之后);

25返回0;

26}


请点亮一下。


Udai Kiran< s。********* @ gmail.comwrites:


大家好

Iam遇到信号处理程序问题。当特定信号收到
后,在调用信号处理程序后执行

将返回何处?将返回到我们注册信号的位置?

以下代码正在接收递归信号。


2 #include< stdio.h>



缺少#include< signal.h--是第1行?


3 #include < stdlib.h>

4 #include< unistd.h>

5

6 void handle_sigsegv(int sig)

7 {

8 printf(抓住SIGSEGV !! \ n);

9信号(SIGSEGV,handle_sigsegv);

10睡觉(1);

11返回;



处理SIGSEGV返回的行为未定义。那个事实再次被提升的事实肯定包括在内!


12}

13

14 void sig_segv(){

15 int * p = NULL;

16 * p = 10;

17 }

18

19 int main()

20 {

21 int * p = NULL;

22信号(SIGSEGV,handle_sigsegv);

23 sig_segv();

24 printf(在segfault \ n之后);

25返回0;

26}



-

Ben。


Hi all
Iam having trouble with signal handler. Where will the execution
return after invoking a signal handler when that particular signal is
recieved? will return to the point where we register the signal?
The following code is recieveing recursive signals.

2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void handle_sigsegv(int sig)
7 {
8 printf("Caught SIGSEGV!!\n");
9 signal(SIGSEGV,handle_sigsegv);
10 sleep(1);
11 return;
12 }
13
14 void sig_segv(){
15 int *p = NULL;
16 *p = 10;
17 }
18
19 int main()
20 {
21 int *p = NULL;
22 signal(SIGSEGV,handle_sigsegv);
23 sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }

Please throw some light.

解决方案

"Udai Kiran" <s.*********@gmail.comwrote in message
news:ab**********************************@c30g2000 hsa.googlegroups.com...

Hi all
Iam having trouble with signal handler. Where will the execution
return after invoking a signal handler when that particular signal is
recieved? will return to the point where we register the signal?
The following code is recieveing recursive signals.

2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void handle_sigsegv(int sig)
7 {
8 printf("Caught SIGSEGV!!\n");
9 signal(SIGSEGV,handle_sigsegv);
10 sleep(1);
11 return;
12 }
13
14 void sig_segv(){
15 int *p = NULL;
16 *p = 10;
17 }
18
19 int main()
20 {
21 int *p = NULL;
22 signal(SIGSEGV,handle_sigsegv);
23 sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }

Please throw some light.

Not sure what you are trying to do. I see that you are resetting your new
signal handler to your new signal handler in your signal handler. Maybe you
want something like:

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>

void handle_sigsegv(int sig)
{
/* Notice that I don''t really handle anything */
printf("Caught SIGSEGV!!\n");
assert(0);
return;
}

void sig_segv(){
int *p = NULL;
*p = 10;
}

int main()
{
int *p = NULL;
signal(SIGSEGV,handle_sigsegv);
sig_segv();
printf("after segfault \n");
return 0;
}

In any case, if you are getting a segmentation violation it is a bug that
has to be fixed.

--
Posted via a free Usenet account from http://www.teranews.com


On Nov 21, 9:53 am, Udai Kiran <s.udayki...@gmail.comwrote:

Hi all
Iam having trouble with signal handler. Where will the execution
return after invoking a signal handler when that particular signal is
recieved? will return to the point where we register the signal?

You cannot predict. It is analogous to an interrupt
handler and fire any time an exception generated.

The following code is recieveing recursive signals.

2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void handle_sigsegv(int sig)
7 {
8 printf("Caught SIGSEGV!!\n");
9 signal(SIGSEGV,handle_sigsegv);
10 sleep(1);
11 return;
12 }
13
14 void sig_segv(){
15 int *p = NULL;
16 *p = 10;
17 }
18
19 int main()
20 {
21 int *p = NULL;
22 signal(SIGSEGV,handle_sigsegv);
23 sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }

Please throw some light.


Udai Kiran <s.*********@gmail.comwrites:

Hi all
Iam having trouble with signal handler. Where will the execution
return after invoking a signal handler when that particular signal is
recieved? will return to the point where we register the signal?
The following code is recieveing recursive signals.

2 #include <stdio.h>

Missing #include <signal.h-- was that line 1?

3 #include <stdlib.h>
4 #include <unistd.h>
5
6 void handle_sigsegv(int sig)
7 {
8 printf("Caught SIGSEGV!!\n");
9 signal(SIGSEGV,handle_sigsegv);
10 sleep(1);
11 return;

The behaviour on return from handling SIGSEGV is undefined. That fact
that it gets raised again is certainly included in that!

12 }
13
14 void sig_segv(){
15 int *p = NULL;
16 *p = 10;
17 }
18
19 int main()
20 {
21 int *p = NULL;
22 signal(SIGSEGV,handle_sigsegv);
23 sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }

--
Ben.


这篇关于信号处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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