2

ゼロ除算例外を処理するために、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() を追加しましたが、正常に動作しています。整数演算用のそのようなマスクはありますか?

4

0 に答える 0