Query on SIGFPE handling
am 22.11.2004 07:26:12 von Jagadeesh Bhaskar P
Hi,
I wrote a the small program, to c how signals can be caught by
customized routines.
/*********** start of code ********/
#include
#include
void fe(void){
printf("floating pt exception:\n");
}
int main(void){
signal(SIGFPE, (void *)fe);
printf("%f\n", (1/0));
return 0;
}
/********** end of code *************/
It goes on catching the signal infinitely, and if i didnt do the
customization of that signal handling, it comes only once and then
exits.
What is the reason? Isnt it supposed to generate a signal once per
event?
Someone please do reply,
--
With regards,
Jagadeesh Bhaskar P
R&D Engineer
HCL Infosystems Ltd
Pondicherry
INDIA
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 08:00:01 von manish regmi
On Mon, 22 Nov 2004 11:56:12 +0530, Jagadeesh Bhaskar P
wrote:
> Hi,
>
> I wrote a the small program, to c how signals can be caught by
> customized routines.
>
> /*********** start of code ********/
>
> #include
> #include
>
> void fe(void){
> printf("floating pt exception:\n");
> }
>
> int main(void){
> signal(SIGFPE, (void *)fe);
> printf("%f\n", (1/0));
> return 0;
> }
>
> /********** end of code *************/
>
> It goes on catching the signal infinitely, and if i didnt do the
> customization of that signal handling, it comes only once and then
> exits.
>
> What is the reason? Isnt it supposed to generate a signal once per
> event?
>
> Someone please do reply,
>
> --
> With regards,
>
> Jagadeesh Bhaskar P
> R&D Engineer
> HCL Infosystems Ltd
> Pondicherry
> INDIA
>
According to the history of UNIX, signal caught by signal function is
unreliable. You need to reload handler on each signal.
ie,
void fe(void){
printf("floating pt exception:\n");
signal(SIGFPE, (void *)fe);
}
It is a good idea to use sigaction(). It is a reliable function doing
the same thing .
see man sigaction
Regards manish
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 08:15:55 von Jagadeesh Bhaskar P
On Mon, 2004-11-22 at 12:30, Manish Regmi wrote:
>
> > /*********** start of code ********/
> >
> > #include
> > #include
> >
> > void fe(void){
> > printf("floating pt exception:\n");
> > }
> >
> > int main(void){
> > signal(SIGFPE, (void *)fe);
> > printf("%f\n", (1/0));
> > return 0;
> > }
> >
> > /********** end of code *************/
> >
> According to the history of UNIX, signal caught by signal function is
> unreliable. You need to reload handler on each signal.
> ie,
> void fe(void){
> printf("floating pt exception:\n");
> signal(SIGFPE, (void *)fe);
> }
I changed the code like above. But no change came. Is this caused by the
signal being generated many times, or is it just the problem with the
signal() function?
>
> It is a good idea to use sigaction(). It is a reliable function doing
> the same thing .
> see man sigaction
Im seeing into that also
--
With regards,
Jagadeesh Bhaskar P
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 09:24:36 von Jagadeesh Bhaskar P
Hi Manish,
As suggested, I rewrote the C program using sigaction, as follows:
/****** start of code **********/
#include
#include
void fe(int x){
printf("floating pt exception:\n");
}
int main(void){
struct sigaction p;
p.sa_handler = fe;
sigaction(SIGFPE, &p, NULL);
printf("%f\n", (1/0));
return 0;
}
/********* end of code *******/
But then again the signal is being caught by the program infinitely. Why
is that happening, if last time it was a problem with the signal()
function.
Please do help!!
--
With regards,
Jagadeesh Bhaskar P
R&D Engineer
HCL Infosystems Ltd
Pondicherry
INDIA
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 10:05:24 von Jagadeesh Bhaskar P
On Mon, 2004-11-22 at 14:27, Yogesh Bute wrote:
> hi,
> u need to inform the shell that the signal is handled - so u need to
> SIG_IGN the singal ie. signal (signum, SIG_IGN) once u have done with
> handling the signal
> or u could send SIG_DFL, so that default singal handler will handle the
> signal, after your specific signal handler code.
How can I generate and send a signal from a function of mine? Is it
possible from a userlevel C program, using some functions like
signal()??
Please do help
--
With regards,
Jagadeesh Bhaskar P
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: further query signal handling!
am 22.11.2004 10:09:28 von manish regmi
On Mon, 22 Nov 2004 14:27:40 +0530, Jagadeesh Bhaskar P
wrote:
> > You are generating a divide by zero exception (on i386) which is a
> > fault (it means the instruction is restartable). So what happens is
> > you catch an exception and print and return. The same code is
> > restarted again. so processor gets exception again and again.
>
> If the fault was not attached to a function written by me, and leaving
> it to be handled by the kernel, there was no problem. How can that
> happen? Shouldnt the kernel restart the instruction in that case also.
> Why didnt that happen?
>
> --
> With regards,
>
> Jagadeesh Bhaskar P
> R&D Engineer
> HCL Infosystems Ltd
> Pondicherry
> INDIA
In UNIX and like, the exceptions generate UNIX signals. IEEE POSIX
have defined what to do on generation of signals. The signal may have
default action, run user 's signal handler. For SIGFPE signal, The
default action is Abnormal termination of the process. But when you
set your handler, it does not terminate but runs your handler. After
returning from your handler it again tries to execute the faulting
instruction and signal is generate again and so on.
BTW:
For UNIX programming, Advanced UNIX programming by richard stevens is
the Best book (i have ever seen).
Regards Manish
--
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 10:19:43 von manish regmi
> How can I generate and send a signal from a function of mine? Is it
> possible from a userlevel C program, using some functions like
> signal()??
>
> Please do help
> --
> With regards,
>
> Jagadeesh Bhaskar P
>
yes,
see man kill, raise
regards manish
--
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 10:26:10 von manish regmi
On Mon, 22 Nov 2004 14:35:24 +0530, Jagadeesh Bhaskar P
wrote:
> On Mon, 2004-11-22 at 14:27, Yogesh Bute wrote:
> > hi,
> > u need to inform the shell that the signal is handled - so u need to
> > SIG_IGN the singal ie. signal (signum, SIG_IGN) once u have done with
> > handling the signal
> > or u could send SIG_DFL, so that default singal handler will handle the
> > signal, after your specific signal handler code.
Doing that will probably put you in infinite loop.
Infact, the is no way to go further unless you resolve the cause of
the exception.
For playing with Signals,
you can generate SIGUSR1 kill(pid, SIGUSR1) and catch.
--
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 10:26:50 von Jagadeesh Bhaskar P
On Mon, 2004-11-22 at 14:49, Manish Regmi wrote:
> see man kill, raise
Thank you. I got that!!
--
With regards,
Jagadeesh Bhaskar P
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
Re: Query on SIGFPE handling
am 22.11.2004 12:15:33 von joy merwin monteiro
The basic mistake is that the variable values have not changed.
1/0 will generate a FPE no matter what you do.
use variables like a=0;b=1;
and if b/a generates an FPE,
change its value.(bit hazy how to implement this... google around)
the reason for giving you a signal handler
is obviously to help you correct the error,
not commit it over and over again!
reagrds,
Joy.M.Monteiro
PS: Such questions will be better answered on linux-cprogramming list....
try it.
On Mon, 22 Nov 2004 13:54:36 +0530, Jagadeesh Bhaskar P
wrote:
> Hi Manish,
>
> As suggested, I rewrote the C program using sigaction, as follows:
>
> /****** start of code **********/
>
> #include
> #include
>
> void fe(int x){
> printf("floating pt exception:\n");
> }
>
> int main(void){
> struct sigaction p;
> p.sa_handler = fe;
> sigaction(SIGFPE, &p, NULL);
> printf("%f\n", (1/0));
> return 0;
> }
>
> /********* end of code *******/
>
> But then again the signal is being caught by the program infinitely. Why
> is that happening, if last time it was a problem with the signal()
> function.
>
> Please do help!!
>
>
>
> --
> With regards,
>
> Jagadeesh Bhaskar P
> R&D Engineer
> HCL Infosystems Ltd
> Pondicherry
> INDIA
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.linux-learn.org/faqs
>
--
people always turn away,
from the eyes of a stranger...
Afraid to know
what lies behind the stare.......
--QueensRyche
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs