浮点异常核心转储
作者:互联网
我是Linux信号的新手,请帮忙.
在Linux 2.6 gcc中运行时,以下代码会获取核心转储.
$./每年
浮点异常(核心已转储)
问题:
1.由于已安装过程信号屏蔽,因此不应由第40行的volatile int z = x / y生成的“ SIGFPGE”;被封锁?
2.如果未阻塞,则由于已经安装了信号处理程序,信号处理程序是否应该捕获“ SIGFPE”而不是内核转储?
3.如果我注释掉第40行volatile int z = x / y ;,并使用第42行raise(SIGFPE);相反,一切都会按我的预期进行. x / 0和在此处提高SIGFPE有什么区别?
这是代码:
#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都会导致未定义的行为:
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.
标签:c-3,linux,signals,sigfpe 来源: https://codeday.me/bug/20191010/1887102.html