473,249 Members | 1,822 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,249 software developers and data experts.

Signal Call

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
Nov 14 '05 #1
7 1673

"Amit Sharma" <sh*******@gmail.com> 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
Nov 14 '05 #2
>> "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

Nov 14 '05 #3
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."
Nov 14 '05 #4
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

Nov 14 '05 #5
> 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

Nov 14 '05 #6
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 <ma********************@ians.uni-stuttgart.de> wrote in message news:<cj**********@infosun2.rus.uni-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

Nov 14 '05 #7

"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
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gary Robinson | last post by:
In some code we're writing we're making an assumption, and I'd like to confirm that the assumption is valid. Suppose a signal arrives while a file is being written, and the signal handler...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
4
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I...
11
by: Jackie | last post by:
Hi everyone, I'd like to know when and how signals are used (e.g. SIGFPE, SIGABRT, SIGTERM, SIGSEGV, SIGINT)? Thank you so much.
4
by: manochavishal | last post by:
Why would a signal handler cannot in general call standard library functions. I have read whats is said in Standard but needs an explaination on that as i couldn't uunderstand it. Thanx in...
18
by: Sven | last post by:
Hi, I found a strange behaviour when using the time() function from time.h. Sometimes when it is called, it does not show the correct time in seconds, but an initial value. This time seem to be...
5
by: david | last post by:
I'm developing a program that runs using an asyncore loop. Right now I can adequately terminate it using Control-C, but as things get refined I need a better way to stop it. I've developed...
9
by: thagor2008 | last post by:
Is the behaviour of throwing exceptions in a unix signal handler defined? eg: void sighandler(int sig) { ... do something throw myobj; }
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.