Signal Call

Discussion in 'C Programming' started by Amit Sharma, Oct 1, 2004.

  1. Amit Sharma

    Amit Sharma Guest

    Hi,
    I want to write a program, where until we give the value of j as
    non zero, it should go on asking us values of i and j, displaying the
    message "Division by zero: Floating point exception" for every input
    of j as zero. Once we give the value of both i and j as non-zero, it
    displays i/j and terminates.

    This is the sample code i tried:
    #include <signal.h>
    void sig_s(int signo);
    void Divide();
    main( )
    {
    signal(SIGFPE,sig_s);
    Divide();

    }
    void Divide()
    {
    int i,j,k;
    printf("Enter a number\n");
    scanf("%d",&i);
    printf("another number\n");
    scanf("%d",&j);
    if( (i==0) && (j==0))
    {
    printf("Total is %d\n",0);
    exit(0);
    }
    else
    {
    printf("Total is %d\n",i/j);
    }
    }

    void sig_s(int signo)
    {
    signal(SIGFPE,sig_s);
    printf("Division by zero:Floating point exception\n");
    Divide();
    }

    It works fine as you give the denominator as zero and ask for the new
    number but if you give the valid values also (e.g. i=10, j=5) Then
    also it went into the Signal handler function and call the signal
    handler recursively.
    Could you please suggest me what wrong i am doing here ???

    Thanks,
    Amit
     
    Amit Sharma, Oct 1, 2004
    #1
    1. Advertising

  2. "Amit Sharma" <> wrote: "Division by zero: Floating point
    exception" for every input
    > of j as zero. Once we give the value of both i and j as non-zero, ....


    snipped
    You can divide zero among as many persons who want a piece of it. Dividing
    by zero is quite a different thing. MPJ
     
    Merrill & Michele, Oct 1, 2004
    #2
    1. Advertising

  3. Amit Sharma

    Michael Mair Guest

    [OT] (was: Signal Call)

    >> "Division by zero: Floating point exception" for every input
    >>of j as zero. Once we give the value of both i and j as non-zero, ....

    >
    > snipped
    > You can divide zero among as many persons who want a piece of it. Dividing
    > by zero is quite a different thing. MPJ


    <OT>
    True. However, it is better to help the OP _and_ correct his use of
    the language than only help him.
    Everything else is off-topic... and it is a nice gesture to mark it so.

    As you said elsethread that you were new to the usenet I thought
    mentioning that might be helpful.
    It is tempting to answer when you know to say something but it
    is more satisfactory when you actually can help the people. For
    everyone.
    </OT>

    --Michael
     
    Michael Mair, Oct 1, 2004
    #3
  4. Re: [OT] (was: Signal Call)


    > > MPJ wrote
    > > You can divide zero among as many persons who want a piece of it.

    Dividing
    > > by zero is quite a different thing. MPJ

    > "Michael Mair" wrote
    > <OT>
    > True. However, it is better to help the OP _and_ correct his use of
    > the language than only help him.
    > Everything else is off-topic... and it is a nice gesture to mark it so.
    >
    > As you said elsethread that you were new to the usenet I thought
    > mentioning that might be helpful.
    > It is tempting to answer when you know to say something but it
    > is more satisfactory when you actually can help the people. For
    > everyone.
    > </OT>
    >
    > --Michael


    I would be less than thrilled to see a perfectly-valid, ANSI C program
    announce to me that 0/7 were division by zero. The language question I
    didn't address at all, because I'm not sure if the fella is a ring theorist
    who mistyped or a kid who needs to crack open his eighth-grade math book
    before thinking of coding. MPJ

    P.S. I like the word "elsethread."
     
    Merrill & Michele, Oct 1, 2004
    #4
  5. Amit Sharma

    Michael Mair Guest

    Hi there,


    I will first comment on your code which probably will not make
    you happy but I have some "useful" answer below.

    > #include <signal.h>

    #include <stdio.h> /* for printf and scanf */
    #include <stdlib.> /* for exit */
    > void sig_s(int signo);
    > void Divide();
    > main( )

    That is either int main (), int main (void) or
    int main (int argc, char *argv[]).
    > {
    > signal(SIGFPE,sig_s);
    > Divide();

    return 0;
    > }
    > void Divide()
    > {
    > int i,j,k;

    k is unused
    > printf("Enter a number\n");
    > scanf("%d",&i);
    > printf("another number\n");
    > scanf("%d",&j);
    > if( (i==0) && (j==0))
    > {
    > printf("Total is %d\n",0);
    > exit(0);
    > }
    > else
    > {
    > printf("Total is %d\n",i/j);
    > }
    > }
    >
    > void sig_s(int signo)
    > {
    > signal(SIGFPE,sig_s);
    > printf("Division by zero:Floating point exception\n");
    > Divide();
    > }


    Please provide code samples that compile.


    > It works fine as you give the denominator as zero and ask for the new
    > number but if you give the valid values also (e.g. i=10, j=5) Then
    > also it went into the Signal handler function and call the signal
    > handler recursively.
    > Could you please suggest me what wrong i am doing here ???


    The thread
    http://groups.google.de/groups?threadm=bnu4dh$ik5$
    (one URL line)
    will give you enough answers. Dave Vandervies explains the whole
    thing rather nicely.

    In short: The signal does not go away after you called the signal
    handler. Usually, the default signal handler is reinstated (which
    does not happen as you once again put your own handler first).
    So, there is no way that the program resumes normal operation
    whithout you doing non-standard-C things (ask in your OS's
    programmer newsgroup).
    You can use standard conforming signal handlers for basic
    cleanup or hasty output, but it is a Bad Idea to try to get
    back into normal program flow.
    This is only an abridged and sloppy explanation, so please read
    the thread I refer to.


    Cheers
    Michael
     
    Michael Mair, Oct 1, 2004
    #5
  6. Amit Sharma

    Michael Mair Guest

    Re: [OT]

    > I would be less than thrilled to see a perfectly-valid, ANSI C program
    > announce to me that 0/7 were division by zero.


    The program suggested otherwise...

    > The language question I
    > didn't address at all, because I'm not sure if the fella is a ring theorist
    > who mistyped or a kid who needs to crack open his eighth-grade math book
    > before thinking of coding. MPJ


    Hmmm, maybe a little bit more of your reasoning written in your
    answer might have made your motives clearer.


    > P.S. I like the word "elsethread."


    So do I - nice of you to provide me with an opportunity to use it ;-)


    Cheers
    Michael
     
    Michael Mair, Oct 1, 2004
    #6
  7. Amit Sharma

    Amit Sharma Guest

    Thanks for the reply,
    I went through the thread and found the possible
    undefined behaviours by signal, Here is the solution i found seems to
    be perfect using sigsetjmp & siglongjmp.

    #include <signal.h>
    #include <stdio.h>
    #include <setjmp.h>

    void sig_s(int signo);
    jmp_buf buf;

    main( )
    {

    int i,j,k;
    signal(SIGFPE,SIG_IGN);
    sigsetjmp(buf,1);
    signal(SIGFPE,sig_s);
    printf("Enter a number\n"); scanf("%d",&i);
    printf("another number\n"); scanf("%d",&j);
    printf("Total is %d",i/j);
    }

    void sig_s(int signo)
    {
    printf("Division by zero:Floating point exception\n");
    siglongjmp(buf,1);
    }


    Thanks,
    Amit






    Michael Mair <-stuttgart.de> wrote in message news:<cjkcjb$ue0$-stuttgart.de>...
    > Hi there,
    >
    >
    > I will first comment on your code which probably will not make
    > you happy but I have some "useful" answer below.
    >
    > > #include <signal.h>

    > #include <stdio.h> /* for printf and scanf */
    > #include <stdlib.> /* for exit */
    > > void sig_s(int signo);
    > > void Divide();
    > > main( )

    > That is either int main (), int main (void) or
    > int main (int argc, char *argv[]).
    > > {
    > > signal(SIGFPE,sig_s);
    > > Divide();

    > return 0;
    > > }
    > > void Divide()
    > > {
    > > int i,j,k;

    > k is unused
    > > printf("Enter a number\n");
    > > scanf("%d",&i);
    > > printf("another number\n");
    > > scanf("%d",&j);
    > > if( (i==0) && (j==0))
    > > {
    > > printf("Total is %d\n",0);
    > > exit(0);
    > > }
    > > else
    > > {
    > > printf("Total is %d\n",i/j);
    > > }
    > > }
    > >
    > > void sig_s(int signo)
    > > {
    > > signal(SIGFPE,sig_s);
    > > printf("Division by zero:Floating point exception\n");
    > > Divide();
    > > }

    >
    > Please provide code samples that compile.
    >
    >
    > > It works fine as you give the denominator as zero and ask for the new
    > > number but if you give the valid values also (e.g. i=10, j=5) Then
    > > also it went into the Signal handler function and call the signal
    > > handler recursively.
    > > Could you please suggest me what wrong i am doing here ???

    >
    > The thread
    > http://groups.google.de/groups?threadm=bnu4dh$ik5$
    > (one URL line)
    > will give you enough answers. Dave Vandervies explains the whole
    > thing rather nicely.
    >
    > In short: The signal does not go away after you called the signal
    > handler. Usually, the default signal handler is reinstated (which
    > does not happen as you once again put your own handler first).
    > So, there is no way that the program resumes normal operation
    > whithout you doing non-standard-C things (ask in your OS's
    > programmer newsgroup).
    > You can use standard conforming signal handlers for basic
    > cleanup or hasty output, but it is a Bad Idea to try to get
    > back into normal program flow.
    > This is only an abridged and sloppy explanation, so please read
    > the thread I refer to.
    >
    >
    > Cheers
    > Michael
     
    Amit Sharma, Oct 2, 2004
    #7
  8. "Amit Sharma" wrote
    > Thanks for the reply,
    > I went through the thread and found the possible
    > undefined behaviours by signal, Here is the solution i found seems to
    > be perfect using sigsetjmp & siglongjmp.
    >
    > #include <signal.h>
    > #include <stdio.h>
    > #include <setjmp.h>
    >
    > void sig_s(int signo);
    > jmp_buf buf;
    >
    > main( )
    > {
    >
    > int i,j,k;
    > signal(SIGFPE,SIG_IGN);
    > sigsetjmp(buf,1);
    > signal(SIGFPE,sig_s);
    > printf("Enter a number\n"); scanf("%d",&i);
    > printf("another number\n"); scanf("%d",&j);
    > printf("Total is %d",i/j);
    > }
    >
    > void sig_s(int signo)
    > {
    > printf("Division by zero:Floating point exception\n");
    > siglongjmp(buf,1);
    > }
    >
    >
    > Thanks,
    > Amit


    And what did your compiler and linker tell you about this?

    >
    > Michael Mair wrote
    > > I will first comment on your code which probably will not make
    > > you happy but I have some "useful" answer below.
    > >
    > > > #include <signal.h>

    > > #include <stdio.h> /* for printf and scanf */
    > > #include <stdlib.> /* for exit */
    > > > void sig_s(int signo);
    > > > void Divide();
    > > > main( )

    > > That is either int main (), int main (void) or
    > > int main (int argc, char *argv[]).
    > > > {
    > > > signal(SIGFPE,sig_s);
    > > > Divide();

    > > return 0;
    > > > }
    > > > void Divide()
    > > > {
    > > > int i,j,k;

    > > k is unused

    Amit thinks it's perfect. Apparently his compiler has a -perfect switch.
    > > > printf("Enter a number\n");
    > > > scanf("%d",&i);
    > > > printf("another number\n");
    > > > scanf("%d",&j);
    > > > if( (i==0) && (j==0))


    Michael, wir haben uns gewundert ob er zerstreuter Professor oder eines
    Schulbuches noetig sei. Er ist der Letzere. SIE, aber, waren halb blau als
    Sie dachten dass null durch null unter Algebraisten definiert ist.

    > > > {
    > > > printf("Total is %d\n",0);
    > > > exit(0);
    > > > }
    > > > else
    > > > {
    > > > printf("Total is %d\n",i/j);
    > > > }
    > > > }
    > > >
    > > > void sig_s(int signo)
    > > > {
    > > > signal(SIGFPE,sig_s);
    > > > printf("Division by zero:Floating point exception\n");
    > > > Divide();
    > > > }

    > >
    > > Please provide code samples that compile.
    > >
    > >
    > > > It works fine as you give the denominator as zero and ask for the new
    > > > number but if you give the valid values also (e.g. i=10, j=5) Then
    > > > also it went into the Signal handler function and call the signal
    > > > handler recursively.
    > > > Could you please suggest me what wrong i am doing here ???

    > >
    > > The thread
    > >

    http://groups.google.de/groups?threadm=bnu4dh$ik5$
    > > (one URL line)
    > > will give you enough answers. Dave Vandervies explains the whole
    > > thing rather nicely.
    > >
    > > In short: The signal does not go away after you called the signal
    > > handler. Usually, the default signal handler is reinstated (which
    > > does not happen as you once again put your own handler first).
    > > So, there is no way that the program resumes normal operation
    > > whithout you doing non-standard-C things (ask in your OS's
    > > programmer newsgroup).
    > > You can use standard conforming signal handlers for basic
    > > cleanup or hasty output, but it is a Bad Idea to try to get
    > > back into normal program flow.
    > > This is only an abridged and sloppy explanation, so please read
    > > the thread I refer to.
    > >
    > >
    > > Cheers
    > > Michael


    I have enough questions about signal.h and setjmp.h to start a new thread.
    MPJ
     
    Merrill & Michele, Oct 2, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Pronath
    Replies:
    1
    Views:
    1,242
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    504
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    690
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,369
  5. dibacco73
    Replies:
    1
    Views:
    698
    joris
    Feb 12, 2009
Loading...

Share This Page