wu :: forums (http://www.ocf.berkeley.edu/~wwu/cgi-bin/yabb/YaBB.cgi)
riddles >> cs >> signal handler execution path
(Message started by: morningkiran on Nov 20th, 2007, 9:00pm)

Title: signal handler execution path
Post by morningkiran on Nov 20th, 2007, 9:00pm
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.


Code:
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 }


Please throw some light.

Title: Re:  signal handler execution path
Post by Grimbal on Nov 21st, 2007, 1:36am
I played a bit with it and it seems that it returns to the very instruction that caused the exception.

And some unixes will remove the handler when the signal is raised, which means that after printint the message, your program tries to dereference the null pointer again and this times fails the usual way with a core dump.

If you want to return to anywhere else, you should probably use a long jump (search man longjmp).



Powered by YaBB 1 Gold - SP 1.4!
Forum software copyright © 2000-2004 Yet another Bulletin Board