我在 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,操作系统是 QNX。我也想用整数值测试 SIGFPE。早期的相同代码也没有为浮点生成 SIGFPE,但后来我添加了 fp_exception_mask(),它工作正常。整数运算有这样的掩码吗?