Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Signal Call

Reply
Thread Tools

Signal Call

 
 
Amit Sharma
Guest
Posts: n/a
 
      10-01-2004
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
 
Reply With Quote
 
 
 
 
Merrill & Michele
Guest
Posts: n/a
 
      10-01-2004

"Amit Sharma" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      10-01-2004
>> "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

 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      10-01-2004

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


 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      10-01-2004
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?threa...s.uwaterloo.ca
(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

 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      10-01-2004
> 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

 
Reply With Quote
 
Amit Sharma
Guest
Posts: n/a
 
      10-02-2004
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 <(E-Mail Removed)-stuttgart.de> wrote in message news:<cjkcjb$ue0$(E-Mail Removed)-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?threa...s.uwaterloo.ca
> (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

 
Reply With Quote
 
Merrill & Michele
Guest
Posts: n/a
 
      10-02-2004

"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?threa...s.uwaterloo.ca
> > (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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Target of signal assignment is not a signal" Nicolas Moreau VHDL 9 07-25-2007 04:21 PM
Re: How to make an internal signal embedded deep in hierarchy to a gloal output signal Weng Tianxiang VHDL 2 01-30-2007 12:58 PM
Aside from delta cycles and/or resolution functions, how can the effective value of a signal differ from a driving signal of its? Colin Paul Gloster VHDL 0 01-11-2007 01:31 PM
threading.Thread vs. signal.signal Jack Orenstein Python 0 09-17-2005 11:24 PM
Async-signal safe functions in signal handlers (unix) Michael Pronath Python 1 01-03-2005 01:10 PM



Advertisments