I have written a program that handles signal for Floating point exception and I am using Ubuntu 10.4.
Here's my source code :
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <setjmp.h>
sigjmp_buf mark;
void GeneralHandler(int signo)
{
switch(signo)
{
case SIGFPE:
printf("\nERROR : Invalid Arithmetic operation.\n");
siglongjmp(mark, signo);
break;
}
exit(signo);
}
int main(void)
{
int i = 0,value = 0, ans = 0;
struct sigaction act;
act.sa_handler = GeneralHandler;
sigaction(SIGFPE, &act, NULL);
for(i = 0; i < 10; i++)
{
if(sigsetjmp(mark, 1)) continue;
printf("Value : ");
scanf("%d" ,&value);
ans = 5 / value;
printf("%d / %d = %d\n", 5, value, ans);
}
}
I am using siglongjmp and sigsetjmp methods for jumping from the handler method to the main method inside for loop.
It works fine for the first time and displays ERROR : Invalid Arithmetic operation. and then displays Floating point exception for second time and then exits.
Program output :
searock@searock-desktop:~/C$ ./signal_continue
Value : 0
ERROR : Invalid Arithmetic operation.
Value :0
Floating point exception
searock@searock-desktop:~/C$
I am not sure what's wrong in my program? Why doesn't it show ERROR : Invalid Arithmetic operation. for the second time? Can someone point me in a right direction?