SIGFPE信号问题 [英] Signal SIGFPE question

查看:1141
本文介绍了SIGFPE信号问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Linux的新手信号,请大家帮忙。
在Linux中GCC 2.6运行时,以下code获得核心转储。

$ ./a.out结果
浮点异常(核心转储)

该问题:结果
1.由于在安装过程信号掩码,不应该由线40(Z = X / Y)中产生的SIGFPGE受阻?结果
2.如果不堵塞,因为信号处理程序已经安装,应该不是SIGFPE由信号处理程序捕获的,而不是一个核心转储?结果
3.如果我注释掉线40(Z = X / Y),使用42行(提高(SIGFPE))来代替,则一切正常如我所料。什么是X / 0,提高SIGFPE这里的区别?

下面是code:

 的#include<&stdio.h中GT;
    #包括LT&;&stdlib.h中GT;
    #包括LT&;&signal.h中GT;    无效sig_handler(INT正负号)
    {
       的printf(sig_handler()接收信号%d个\\ N,正负号);
    }
    INT主(INT ARGC,CHAR *的argv [])
    {       //设置信号屏蔽,屏蔽所有的信号
       sigset_t设置;
       sigfillset(安培;套);       如果(sigprocmask(SIG_BLOCK,&放大器;集,NULL)℃的)
       {
          PERROR(无法设置sigmask);
          返回-1;
       }       //安装SIGFPE信号处理程序
       结构sigaction的行为;
       act.sa_handler = sig_handler;
       act.sa_mask =集;
       act.sa_flags = 0;
       如果(sigaction的(SIGFPE,&放大器;行为,NULL)℃的)
       {
          PERROR(sigaction的失败);
          出口(-1);
       }       挥发性INT X = 1;
       挥发性INT Y = 0;
       挥发性INT Z = X / Y;       //加薪(SIGFPE);       的printf(点1000 \\ n);       返回0;
    }


解决方案

由于硬件陷阱,而信号被阻断的原因未定义的行为的任何SIGFPE:


  

如果任何SIGFPE的,SIGILL,SIGSEGV,或者当他们被封锁产生SIGBUS信号,结果是不确定的,除非被杀死()函数时,sigqueue()函数,或提高(所产生的信号)的功能。


(从 sigprocmask 规范

I am newbie on the Linux signals, please help. The following code get core dump when run in Linux 2.6 gcc.

$ ./a.out
Floating point exception (core dumped)

The questions:
1. Since a process signal mask is installed, shouldn't the "SIGFPGE" generated by line 40 (z=x/y) be blocked?
2. If it is not blocked, since a signal handler has been installed, shouldn't the "SIGFPE" be captured by the signal handler, instead of a core dump?
3. If I commented out line 40 (z=x/y), and use line 42 (raise(SIGFPE)) instead, then everything works as I expected. What is the difference between x/0 and raise SIGFPE here?

Here is the code:

    #include <stdio.h>
    #include <stdlib.h>
    #include <signal.h>

    void sig_handler(int signum)
    {
       printf("sig_handler() received signal %d\n", signum);
    }


    int main(int argc, char * argv[])
    {

       // setup signal mask, block all signals
       sigset_t set;
       sigfillset(&set);

       if(sigprocmask(SIG_BLOCK, &set, NULL)<0)
       {
          perror("failed to set sigmask");
          return -1;
       }

       // install signal handler for SIGFPE
       struct sigaction act;
       act.sa_handler = sig_handler;
       act.sa_mask = set;
       act.sa_flags = 0;
       if(sigaction( SIGFPE, &act, NULL)<0)
       {
          perror("sigaction failed");
          exit(-1);
       }

       volatile int x =1;
       volatile int y =0;
       volatile int z = x/y;

       //raise(SIGFPE);

       printf("point 1000\n");

       return 0;
    }

解决方案

Any SIGFPE caused by a hardware trap while the signal is blocked causes undefined behavior:

If any of the SIGFPE, SIGILL, SIGSEGV, or SIGBUS signals are generated while they are blocked, the result is undefined, unless the signal was generated by the kill() function, the sigqueue() function, or the raise() function.

(from sigprocmask specification)

这篇关于SIGFPE信号问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆