Skip to contentSkip to conversation

Query on SIGFPE handling

DISCUSSION Jagadeesh Bhaskar P 3 participants 10 messages Nov 22, 2004 3 messages not in the archive

lore
Share & tools
lore
#0 lore raw

Hi,

I wrote a the small program, to c how signals can be caught by customized routines.

/*********** start of code ********/

#include <stdio.h>
#include <signal.h>

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,

Signature · 13 lines
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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#1 lore raw
Quoted 31 lines · Jagadeesh Bhaskar P

Hi,

I wrote a the small program, to c how signals can be caught by customized routines.

/*********** start of code ********/

#include <stdio.h>
#include <signal.h>

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,

Quoted 9 lines

--
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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

#2 lore raw
Quoted 18 lines · Manish Regmi
Quoted 10 lines

/*********** start of code ********/

#include <stdio.h>
#include <signal.h>

void fe(void){
       printf("floating pt exception:\n");
}
Quoted 8 lines
int main(void){
       signal(SIGFPE, (void *)fe);
       printf("%f\n", (1/0));
       return 0;
}

/********** end of code *************/

Quoted 7 lines

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?

Quoted 4 lines

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

Signature · 9 lines
With regards,

Jagadeesh Bhaskar P

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#3 lore raw

Hi Manish,

As suggested, I rewrote the C program using sigaction, as follows:

/****** start of code **********/

#include <stdio.h>
#include <signal.h>

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!!

Signature · 13 lines
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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#5 lore raw
In reply to a message not in the archive
Quoted 6 lines · Yogesh Bute

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

Signature · 9 lines
With regards,

Jagadeesh Bhaskar P

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#12 lore raw

Re: further query signal handling!

In reply to a message not in the archive
Quoted 10 lines · Jagadeesh Bhaskar P
Quoted 4 lines

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?

Quoted 8 lines

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

Signature · 6 lines
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#6 lore raw
In reply to Jagadeesh Bhaskar P
Quoted 10 lines

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

Signature · 6 lines
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#8 lore raw
In reply to Jagadeesh Bhaskar P
Quoted 7 lines · Jagadeesh Bhaskar P
Quoted 6 lines · Yogesh Bute

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.

Signature · 6 lines
Manish Regmi
-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#7 lore raw
In reply to Manish Regmi
Quoted 1 line · Manish Regmi

see man kill, raise

Thank you. I got that!!

Signature · 9 lines
With regards,

Jagadeesh Bhaskar P

-
To unsubscribe from this list: send the line "unsubscribe linux-newbie" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs
#9 lore raw
In reply to Jagadeesh Bhaskar P

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.

Quoted 46 lines · Jagadeesh Bhaskar P

Hi Manish,

As suggested, I rewrote the C program using sigaction, as follows:

/****** start of code **********/

#include <stdio.h>
#include <signal.h>

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 [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs

Signature · 10 lines
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 [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.linux-learn.org/faqs