由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C signal SIGFPE 问题
相关主题
Linux 里让一个程序运行30秒,然后就KILL,然后一段时间后再开始运行GCJ2009
关于signal handlerHow to use volatile in c++?
HELP:这个死锁是怎么发生的?这个function pointer最后的那个int是什么意思?
关于在c++ member function里用signal( )紧急求助—寻C++ tutor
java & signal process有人发现最近valgrind有问题么?
printf("%s\n", NULL)的结果Any examples for implementing user-level threads library in C?
About volatile in CSIGSEGV,Segmentation fault in Cygwin
请问如何给sigalrm_handler传递参数does the system guarantee this? (转载)
相关话题的讨论汇总
话题: signal话题: handler话题: sigfpe话题: sig话题: received
进入Programming版参与讨论
1 (共1页)
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.

1 (共1页)
进入Programming版参与讨论
相关主题
does the system guarantee this? (转载)java & signal process
如何区分read page fault 和 write page faultprintf("%s\n", NULL)的结果
被一个sigsegv exception 折腾死了About volatile in C
help on pthreads.....请问如何给sigalrm_handler传递参数
Linux 里让一个程序运行30秒,然后就KILL,然后一段时间后再开始运行GCJ2009
关于signal handlerHow to use volatile in c++?
HELP:这个死锁是怎么发生的?这个function pointer最后的那个int是什么意思?
关于在c++ member function里用signal( )紧急求助—寻C++ tutor
相关话题的讨论汇总
话题: signal话题: handler话题: sigfpe话题: sig话题: received