Reputation: 21
I have Implemented following code in QNX to handle divided by zero exception.
static int st_iThreadID = -1;
static sigjmp_buf fpe_env;
float a = -10, b = 0;
volatile float c = 0;
/**************************************
*
* Signal Handler for Divided by zero exception
*
****************************************/
void SignalHandler(int Signo,siginfo_t* info,void* other)
{
int iThreadID = gettid();`enter code here`
printf( " exception caught for thread \t %d Generated Signal is \t %d \n",iThreadID,Signo);
siglongjmp(fpe_env, info->si_code);
}
/**************************************
*
* Handler Ends
*
****************************************/
int TestDividedByZero()
{
/*local variable initialization*/
int l_iStatus = 0;
sigset_t set;
int code ;
struct sigaction act; /*Sigaction structure to set the action for Signal*/
/*End local variable initialization*/
fp_exception_mask(_FP_EXC_DIVZERO, 1 ); /* Set the exception mask for floating point Exception */
sigfillset( &act.sa_mask);
sigdelset(&set,SIGFPE);
act.sa_flags = SA_SIGINFO;
act.sa_mask = set;
act.sa_sigaction =SignalHandler;
sigaction(SIGFPE, &act, NULL ); /* POSIX: Specify the action associated with a signal SIGFPE */
code = sigsetjmp(fpe_env, 1);
if(code == 0)
{
c = a / b; /*Here generates floating point signal SIGFPE and registered handler will get call */
// printf("value of d is %d \n",d);
}
else
{
puts( "Jumped from Signal handler");
c = 0; // Set result to default value
}
//puts( "End SIGFPE of Testing ");
return l_iStatus ;
}
When the type of variable is float or double , this code generates SIGFPE but when change the variable type to int or long, this code does not generates the SIGFPE. I am using gcc compiler, processor is POWERPC MPC8347 and OS is QNX. I wants to test SIGFPE with integer value as well. earlier same code was not generating SIGFPE for float also but later I added fp_exception_mask(), and it is working fine. Is there any such mask for integer operation?
Upvotes: 2
Views: 348