PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > signal handler question
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
signal handler question

Réponse
 
LinkBack Outils de la discussion
Vieux 21/11/2007, 04h53   #1
Udai Kiran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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.
  Réponse avec citation
Vieux 21/11/2007, 05h15   #2
Dann Corbit
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

"Udai Kiran" <s.udaykiran@gmail.com> wrote in message
news:abbfee28-929a-43dd-800f-08e256008fb9@c30g2000hsa.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

  Réponse avec citation
Vieux 21/11/2007, 05h38   #3
ksashtekar@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

On Nov 21, 9:53 am, Udai Kiran <s.udayki...@gmail.com> 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?


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.


  Réponse avec citation
Vieux 21/11/2007, 05h47   #4
Ben Bacarisse
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

Udai Kiran <s.udaykiran@gmail.com> writes:

> 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.
  Réponse avec citation
Vieux 21/11/2007, 07h06   #5
Udai Kiran
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

On Nov 21, 10:47 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
> Udai Kiran <s.udayki...@gmail.com> writes:
> > 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.
  Réponse avec citation
Vieux 21/11/2007, 08h29   #6
santosh
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

In article
<60d4381d-2a1e-49c2-a527-299b087e50ca@y43g2000hsy.googlegroups.com>,
Udai Kiran <s.udaykiran@gmail.com> wrote on Wednesday 21 Nov 2007 12:36
pm:

> On Nov 21, 10:47 am, Ben Bacarisse <ben.use...@bsb.me.uk> wrote:
>> Udai Kiran <s.udayki...@gmail.com> writes:
>> > 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?


<snip>

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

  Réponse avec citation
Vieux 21/11/2007, 08h45   #7
Flash Gordon
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

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
  Réponse avec citation
Vieux 21/11/2007, 08h57   #8
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

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.
  Réponse avec citation
Vieux 21/11/2007, 12h53   #9
James Kuyper
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

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.
  Réponse avec citation
Vieux 21/11/2007, 20h01   #10
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

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 , 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++.

  Réponse avec citation
Vieux 21/11/2007, 22h26   #11
Tor Rustad
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

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 <bwzcab@wvtqvm.vw | tr i-za-h a-z>
  Réponse avec citation
Vieux 22/11/2007, 02h57   #12
Jack Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: signal handler question

On Tue, 20 Nov 2007 20:53:39 -0800 (PST), Udai Kiran
<s.udaykiran@gmail.com> wrote 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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 21h21.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22447 seconds with 20 queries