我在QNX中实现了以下代码来处理除以零的异常。
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 ;
}
当变量的类型是float或double时,此代码生成SIGFPE,但是当将变量类型更改为int或long时,此代码不会生成SIGFPE。我使用的是gcc编译器,处理器是POWERPC MPC8347,OS是QNX。 我也想用整数值测试SIGFPE。 早期相同的代码也没有为float生成SIGFPE,但后来我添加了fp_exception_mask(),它工作正常。整数运算有没有这样的掩码?