执行默认信号处理程序 [英] executing default signal handler
问题描述
我已经写在这里我已经注册信号处理程序的数量在Linux中不同信号的应用程序。
过程接收到该信号后,控制被转移到我已经登记的信号处理程序。在此信号处理我做了一些工作,我需要做的,然后我想叫即 SIF_DFL
默认信号处理程序或 SIG_IGN
。
然而, SIG_DFL
和 SIG_ING
都是宏分别扩大到数值0和1,这是无效的函数的地址。
I have written an application where i have registered number of signal handler for different signals in linux .
After process receives the signal the control is transferred to the signal handler i had registered. In this signal handler i do some work which i need to do, and then i would like to call the default signal hander i.e SIF_DFL
or SIG_IGN
.
However, SIG_DFL
and SIG_ING
are both macros which expand to numeric values 0 and 1 respectively, which are invalid function addresses.
有什么办法,我可以调用默认的操作即 SIG_DFL
或 SIG_IGN
?
IS there any way i can call default actions i.e SIG_DFL
or SIG_IGN
?
为了达到效果 SIG_DFL
或 SIG_ING
我分别调用exit(1),什么也不做, 。但是,对于像 SIGSEGV信号
我也想有核心转储。
一般来说,我会想我的默认行为是一样的 SIG_DFL
,而忽略行为相同 SIG_IGN
,顺便操作系统会做 。
In order to achieve the effect of SIG_DFL
or SIG_ING
i call exit(1) and do nothing , respectively . But for signals like SIGSEGV
i also would like to have core dump .
In general i would want to my default behavior to be same as SIG_DFL
and ignore behavior same SIG_IGN
, the way Operating system would do .
推荐答案
的GNU C库参考手册有一整章解释有关的信号处理一切。
The GNU C Library Reference Manual has a whole chapter explaining everything about signal handling.
您始终在您安装自己的处理程序(请参阅信号联机手册()
或<$ C $得到previously设置信号处理器(函数指针) C>的sigaction())。
You always get the previously set signal handler (a function pointer) when you install your own handler (see manpages for signal()
or sigaction()
).
previous_handler = signal(SIGINT, myhandler);
一般的规则是,你总是可以()再次将信号重置为previous处理程序和加薪。
The general rule is, that you can always reset to the previous handler and raise()
the signal again.
void myhandler(int sig) {
/* own stuff .. */
signal(sig, previous_handler);
raise(sig);
/* when it returns here .. set our signal handler again */
signal(sig, myhandler);
}
有一个的一般规则的缺点:其分别对应的信号硬件异常通常被分配到某个指令导致异常。所以,当你再次上调的信号,相关的指令是不一样的最初。这可以,但不应该损害其他信号处理程序。
There is one disadvantage of the general rule: Hardware exceptions which are mapped to signals are usually assigned to a certain instruction which caused the exception. So, when you raise a signal again, the associated instruction is not the same as originally. This can but should not harm other signal handlers.
另一个劣势是,每次上调信号导致了大量的处理时间。以prevent过度使用的提高()
您可以使用下列选项:
Another disadvantage is, that each raised signal causes a lot of processing time. To prevent excessive use of raise()
you can use the following alternatives:
-
在
SIG_DFL
函数指针指向的情况下,解决0
(这显然是不合法的地址)。因此,您必须再次信号复位处理程序和加薪()
。
In case of
SIG_DFL
the function pointer points to address0
(which is obviously no valid address). Thus, you have to reset the handler andraise()
the signal again.
if (previous_handler == SIG_DFL)
{
signal(sig, SIG_DFL);
raise(sig);
signal(sig, myhandler);
}
SIG_IGN
的价值 1
(也无效地址)。在这里,你可以返回(什么都不做)。
SIG_IGN
has value 1
(also an invalid address). Here you can just return (do nothing).
else if (previous_handler == SIG_IGN)
{
return;
}
否则(既不 SIG_IGN
也不 SIG_DFL
)你收到一个有效的函数指针和您能直接调用句柄,
Otherwise (neither SIG_IGN
nor SIG_DFL
) you have received a valid function pointer and you can call the handler directly,
else
{
previous_handler(sig);
}
当然,你必须要考虑不同的API,以及(见信号联机手册()
和的sigaction()
)。
Of course, you have to consider the different APIs as well (see manpages for signal()
and sigaction()
).
这篇关于执行默认信号处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!