signal handler question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Udai Kiran

    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.
  • Dann Corbit

    #2
    Re: signal handler question

    "Udai Kiran" <s.udaykiran@gm ail.comwrote in message
    news:abbfee28-929a-43dd-800f-08e256008fb9@c3 0g2000hsa.googl egroups.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

    Comment

    • ksashtekar@gmail.com

      #3
      Re: signal handler question

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

      Comment

      • Ben Bacarisse

        #4
        Re: signal handler question

        Udai Kiran <s.udaykiran@gm ail.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.

        Comment

        • Udai Kiran

          #5
          Re: signal handler question

          On Nov 21, 10:47 am, Ben Bacarisse <ben.use...@bsb .me.ukwrote:
          Udai Kiran <s.udayki...@gm ail.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.

          Comment

          • Mark Bluemel

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

            Comment

            • Flash Gordon

              #7
              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.progr ammer in this case, but they will also probably tell you
              that you cannot change this behaviour.
              --
              Flash Gordon

              Comment

              • James Kuyper

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

                Comment

                • Martin Ambuhl

                  #9
                  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 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++.

                  Comment

                  • Tor Rustad

                    #10
                    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>

                    Comment

                    • Jack Klein

                      #11
                      Re: signal handler question

                      On Tue, 20 Nov 2007 20:53:39 -0800 (PST), Udai Kiran
                      <s.udaykiran@gm ail.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.l earn.c-c++

                      Comment

                      Working...