浮点异常核心转储 [英] Floating Point Exception Core Dump

查看:133
本文介绍了浮点异常核心转储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linux信号的新手,请帮忙. 在Linux 2.6 gcc中运行时,以下代码会获取核心转储.

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

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

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

问题:
1.由于已安装过程信号屏蔽,是否不应阻塞第40行volatile int z = x/y;生成的"SIGFPGE"?
2.如果未阻塞,则因为已经安装了信号处理程序,信号处理程序是否应该捕获"SIGFPE"而不是内核转储?
3.如果我注释掉第40 volatile int z = x/y;行,而改用第42 raise(SIGFPE);行,则一切正常. x/0和在此处引发SIGFPE有什么区别?

The questions:
1. Since a process signal mask is installed, shouldn't the "SIGFPGE" generated by line 40 volatile int 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 40volatile int 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?

这是代码:

    #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; //line 40

       //raise(SIGFPE); //line 42

       printf("point 1000\n");

       return 0;
    }

推荐答案

在信号被阻止时由硬件陷阱引起的任何SIGFPE都会导致未定义的行为:

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

如果在阻止时生成了SIGFPE,SIGILL,SIGSEGV或SIGBUS信号中的任何一个,则结果是不确定的,除非该信号是由kill()函数,sigqueue()函数或raise()生成的)功能.

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.

(摘自 sigprocmask规范)

这篇关于浮点异常核心转储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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