j*******e 发帖数: 674 | 1 下面code在Linux2.6,gcc下运行:
$ ./a.out
Floating point exception (core dumped)
问题:
1. 已经设了process signal mask, 40行产生的SIGFPE应该被屏蔽
2. 32行安装了signal handler, 40行产生的SIGFPE应该被capture
为什么运行结果是直接core dump?
如果注释掉 “z=x/y", 改用“raise(SIGFPE)", 运行结果就符合预期。
难道“z=x/y"在这里不是rasie SIGFPE 吗?
=============================
#include
#include
#include
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;
} | t****t 发帖数: 6806 | 2 man signal
According to POSIX, the behaviour of a process is undefined after it
ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by
the kill(2) or the raise(3) functions. Integer division by zero has
undefined result. On some architectures it will generate a SIGFPE signal.
(Also dividing the most negative integer by -1 may generate SIGFPE.)
Ignoring this signal might lead to an endless loop.
【在 j*******e 的大作中提到】 : 下面code在Linux2.6,gcc下运行: : $ ./a.out : Floating point exception (core dumped) : 问题: : 1. 已经设了process signal mask, 40行产生的SIGFPE应该被屏蔽 : 2. 32行安装了signal handler, 40行产生的SIGFPE应该被capture : 为什么运行结果是直接core dump? : 如果注释掉 “z=x/y", 改用“raise(SIGFPE)", 运行结果就符合预期。 : 难道“z=x/y"在这里不是rasie SIGFPE 吗? : =============================
| j*******e 发帖数: 674 | 3 Thanks for the fast response.
I removed the signal mask between line 18 ~23.
So the signal is not ignored, it should be captured by the Signal handler.
Now I run the program, it goes into endless loop, as in your comments. How
come?
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
sig_handler() received signal 8
...
...
it
【在 t****t 的大作中提到】 : man signal : According to POSIX, the behaviour of a process is undefined after it : ignores a SIGFPE, SIGILL, or SIGSEGV signal that was not generated by : the kill(2) or the raise(3) functions. Integer division by zero has : undefined result. On some architectures it will generate a SIGFPE signal. : (Also dividing the most negative integer by -1 may generate SIGFPE.) : Ignoring this signal might lead to an endless loop.
| t****t 发帖数: 6806 | 4 The following is from C standard. | j*******e 发帖数: 674 | 5 Wow. I am impressed.
Thanks for the help.
Is there any book about the Unix signals you can recommend? I read through
the "Advanced Programming in the Unix environment", and believe I got it all
. Then once I really start work on it, all kinds of issue showed up. | t****t 发帖数: 6806 | 6 can't give you any book suggestion. i learned these from experience. as you
said, book usually is not enough, sometimes you have to read manuals word to
word literally. and most importantly, you have to think, what will OS do?
what will compiler do? if i wrote the OS/compiler, what should i do? what
choice do i have? and then compare with manual.
gradually your thoughts will get closer to the environment.
all
【在 j*******e 的大作中提到】 : Wow. I am impressed. : Thanks for the help. : Is there any book about the Unix signals you can recommend? I read through : the "Advanced Programming in the Unix environment", and believe I got it all : . Then once I really start work on it, all kinds of issue showed up.
|
|