473,909 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,s ig_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,s ig_s);
printf("Divisio n 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 1708

"Amit Sharma" <sh*******@gmai l.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,s ig_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,s ig_s);
printf("Divisio n 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,S IG_IGN);
sigsetjmp(buf,1 );
signal(SIGFPE,s ig_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("Divisio n by zero:Floating point exception\n");
siglongjmp(buf, 1);
}
Thanks,
Amit


Michael Mair <ma************ ********@ians.u ni-stuttgart.de> wrote in message news:<cj******* ***@infosun2.ru s.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,s ig_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,s ig_s);
printf("Divisio n 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,S IG_IGN);
sigsetjmp(buf,1 );
signal(SIGFPE,s ig_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("Divisio n 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,s ig_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,s ig_s);
printf("Divisio n 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
3976
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 explicitly raises a SystemExit exception. My understanding is that the C-level I/O code will continue and the signal won't be processed until the end of the atomic python interpreter instruction that invoked that C code.
2
2218
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 suggestion on how to deliver a signal to an object is to do it via a global variable and a wrapper function, a technique that is generally a Bad Idea (due to the usage of a global variable). I understand that this ng is dedicated to the discussion of...
3
3529
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.*/ signal(SIGINT,crash); signal(SIGBUS,crash); signal(SIGSEGV,crash); This part works. Those signals cause the "crash.c" module to run and get rid of the lock. Is there a standard way to also print
4
2026
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 know Unixes do it, but what about windows ? does it implements all SIG* signal promptly ? Thanks ! -Eric Boutin
11
2989
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
2106
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 advance
18
3934
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 the time when the program was started the first time. My platform is a DEC machine with Tru64 onboard. A possible explanation could be, that the time() function is called
5
6659
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 another program that executes it as a child process using popen2.Popen4(). I was attempting to use signals to stop it (using os.kill()) but I keep running into a problem where sending the signal causes an infinite loop of printing the message...
9
5853
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; }
0
10037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11348
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10921
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11052
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10540
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9727
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8099
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7249
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4336
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.