473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

signal handler question

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.
Nov 21 '07 #1
10 2101
"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

Nov 21 '07 #2
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.
Nov 21 '07 #3
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.
Nov 21 '07 #4
On Nov 21, 10:47 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
Udai Kiran <s.udayki...@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.
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.
Nov 21 '07 #5
Udai Kiran wrote:
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.
Nov 21 '07 #6
Udai Kiran wrote, On 21/11/07 07:06:

<snip>
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.
--
Flash Gordon
Nov 21 '07 #7
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 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.
Nov 21 '07 #8
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++.

Nov 21 '07 #9
Udai Kiran wrote:
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."
--
Tor <bw****@wvtqvm.vw | tr i-za-h a-z>
Nov 21 '07 #10
On Tue, 20 Nov 2007 20:53:39 -0800 (PST), Udai Kiran
<s.*********@gmail.comwrote in comp.lang.c:
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
Nov 22 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Robinson | last post by:
In some code we're writing we're making an assumption, and I'd like to confirm that the assumption is valid. Suppose a signal arrives while a file is being written, and the signal handler...
3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
3
by: LeTubs | last post by:
Hi I'm not sure if this is correct ...place to post but here goes This is what i'm trying to do, I want to write a signal / alarm handler ( I don't know which hence the posting here, as once I...
11
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
5
by: Sontu | last post by:
Hi all, Consider the following code: #include<signal.h> #include<stdio.h> #include<sys/mman.h> void handler(int sig) {
7
by: Free Bird | last post by:
Hello all, I have two questions about signal handlers. First of all, is it possible to specify a signal handler for all non-specified signals? Since ISO C defines only six signals but POSIX...
9
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.