如何code添加到标准的信号处理? [英] How to add code to the standard signal handler?

查看:113
本文介绍了如何code添加到标准的信号处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux上运行,我需要一些code添加到标准的信号处理C应用程序。当时的想法是建立我的处理程序指针保存到标准之一,并从我的code调用保存的处理程序。不幸的是,信号(),也没有的sigaction()的返回指针的标准处理程序。它们都返回NULL来代替。
是否有这样做的定制处理,并与标准的处理继续不拆自定义处理程序,然后重新发送相同的信号的方法吗?

I have a C application running on Linux where I need to add some code to the standard signal handler. The idea was to setup my handler saving the pointer to the standard one and call the saved handler from my code. Unfortunately, neither signal() nor sigaction() return pointer to the standard handler. Both of them return NULL instead. Is there any way of doing custom handling and continuing with the standard handling without removal of the custom handler and sending the same signal again?

推荐答案

有没有标准信号处理器;相反,还有由内核执行的默认操作时的信号未处理。如果你想要做的事情是接收到的信号时,则推迟到默认操作,你可以做你的信号处理程序结束如下:

There is no "standard signal handler"; instead, there's a default action performed by the kernel when a signal is unhandled. If you want to do something when the signal is received, then defer to the default action, you can do the following at the end of your signal handler:

sigset_t set;
signal(sig, SIG_DFL);
raise(sig);
sigemptyset(&set);
sigaddset(&set, sig);
sigprocmask(SIG_UNBLOCK, &set, 0);

这是假设你使用的sigaction 来安装信号处理程序,并没有指定 SA_NODEFER SA_RESETHAND 标志。它也可以达到你想要使用这些标志和简单地调用筹集什么,但是这有丑陋的比赛条件,如果信号快速连续发表了两次,所以你不应该做的它;改用我建议的方法。

This is assuming you used sigaction to install your signal handler, and did not specify the SA_NODEFER or SA_RESETHAND flags. It's also possible to achieve what you want using those flags and simply calling raise, but this has ugly race conditions if the signal is delivered twice in rapid succession, so you should not do it; instead use the method I suggested.

编辑:其实你不必做任何的信号掩码的东西,因为从信号处理程序返回时,将恢复旧的信号掩码。只是这应该工作:

Actually you don't have to do any of the signal mask stuff, since returning from the signal handler will restore the old signal mask. Just this should work:

signal(sig, SIG_DFL);
raise(sig);
return;

这篇关于如何code添加到标准的信号处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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