signal handler question

U

Udai Kiran

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.
 
D

Dann Corbit

Udai Kiran said:
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.
 
K

ksashtekar

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.
 
B

Ben Bacarisse

Udai Kiran said:
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 said:
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!
 
U

Udai Kiran

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

yes signal.h was there in first line. let me post a new version.


1 #include <signal.h>
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 sleep(1);
10 }
11
12 int sig_segv(){
13 int *p = NULL;
14 *p = 10;
15 return 0;
16 }
17
18 int main()
19 {
20 int ret_val = 0;
21 signal(SIGSEGV,handle_sigsegv);
22 printf("before segfault \n");
23 ret_val = sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }


the thing I was looking for is if a segfault occure in sig_segv why is
it comming back to the same point of execution and then causing
segfault again.
 
S

santosh

yes signal.h was there in first line. let me post a new version.
1 #include <signal.h>
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 sleep(1);
10 }
11
12 int sig_segv(){
13 int *p = NULL;
14 *p = 10;
15 return 0;
16 }
17
18 int main()
19 {
20 int ret_val = 0;
21 signal(SIGSEGV,handle_sigsegv);
22 printf("before segfault \n");
23 ret_val = sig_segv();
24 printf("after segfault \n");
25 return 0;
26 }
the thing I was looking for is if a segfault occure in sig_segv why is
it comming back to the same point of execution and then causing
segfault again.

What's the output? The behaviour of a Standard C program after a
reception of SIGFPE, SIGSEGV or SIGILL is Undefined, i.e., any
behaviour is possible and allowed.

Generally in multitasking systems like Linux and Windows programs that
write to memory that they do not own are terminated, since such
programs can pose a significant security risk if allowed to handle the
signal and continue.

Note also that the behaviour is Undefined if you call any Standard
library function other than abort() or _Exit() from within a signal
handler.
 
F

Flash Gordon

Udai Kiran wrote, On 21/11/07 07:06:

the thing I was looking for is if a segfault occure in sig_segv why is
it comming back to the same point of execution and then causing
segfault again.

Because it is allowed to. The only thing you can do with any degree of
safety on a segfault is attempt to tidy up and the terminate the
program. I say attempt, because if tidying up includes writing to or
closing files *that* could cause another segfault.

If you want to know more about the handling of a specific system you
need to ask where that system is topical. I would suggest
comp.unix.programmer in this case, but they will also probably tell you
that you cannot change this behaviour.
 
M

Mark Bluemel

Udai said:
Hi all
Iam having trouble with signal handler.

Not totally surprising - they're not easy.

I think the behaviour tends to be system-specific - if you're running on
a Unix-like system, you could do well to get hold of W Richard Stevens'
book "Advanced Programming in the Unix Environment" which discusses
signal handling extensively and clearly; I can't comment on other
platforms.
 
J

James Kuyper

Udai said:
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 code inside a signal handler suffers from a number of serious
restrictions. Section 7.14.1.1p3 says:

"... Then the equivalent of (*func)(sig); is executed. If and when the
function returns, if the value of sig is SIGFPE, SIGILL, SIGSEGV, or any
other implementation-defined value corresponding to a computational
exception, the behavior is undefined; otherwise the program will resume
execution at the point it was interrupted."

There are only three ways for a program which enters a signal handler
for any one of those signals to have behavior defined by the C standard:
by calling abort(), or calling _Exit(), or by never exiting the signal
handler. The first two are pretty drastic; the third is pretty pointless.
 
M

Martin Ambuhl

Udai said:
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.

[OP's code is at EOM]

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> /* mha: note that this is non-standard,
and any questions associated with it
are off-topic in comp.lang.c */
#include <signal.h> /* mha: added, needed for SIGSEGV, and
without it signal is assumed to
return an int */

void handle_sigsegv(int sig)
{
signal(SIGSEGV, SIG_IGN); /* mha: see discussion of signal()
below. */
printf("Caught SIGSEGV!!\n");
sleep(1); /* mha: note that this is non-standard,
and any questions associated with it
are off-topic in comp.lang.c */
signal(SIGSEGV, handle_sigsegv); /* mha: Moved. You don't want
to do this until you have
completed handling SIGSEGV;
in fact, you might to have
signal(SIGSEGV, SIG_IGN) at
the beginning of the
handler. The most common
approach is for the
equivalent of
signal(SIGSEGV, SIG_DFL) to
be executed on entrance to
the signal handler, but this
may not be what you want and
whether it is done is
implementation-defined. */
return;
}

void sig_segv()
{
raise(SIGSEGV); /* mha: a surer way of raising SIGSEGV
than the attempted invalid access in
the original code */
}

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

[EOM: OP's original code]
If you actually want anyone's help, don't munge your code with
extraneous text, like those line numbers.
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.

'throw' is C++.
 
T

Tor Rustad

Udai said:
the thing I was looking for is if a segfault occure in sig_segv why is
it comming back to the same point of execution and then causing
segfault again.

"Behavior, upon use of a nonportable or erroneous program construct or
of erroneous data, or of indeterminately valued objects, for which this
International Standard imposes no requirements."
 
J

Jack Klein

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

Calling printf() in a signal handler leads to totally undefined
behavior, aside from the fact that the result of handling SIGSEGV is
undefined in itself.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top