Udai Kiran wrote:
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++.