Signal Call

A

Amit Sharma

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
 
M

Merrill & Michele

Amit Sharma said:
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
 
M

Michael Mair

"Division by zero: Floating point exception" for every input
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
 
M

Merrill & Michele

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

Michael Mair

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 said:
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/[email protected]
(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
 
M

Michael Mair

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
 
A

Amit Sharma

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 said:
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 said:
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/[email protected]
(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
 
M

Merrill & Michele

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

I have enough questions about signal.h and setjmp.h to start a new thread.
MPJ
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top