[问题] signal()重复呼叫问题

楼主: rudyych (熊熊)   2014-10-02 10:02:13
以下问题已找到答案了,各位不用回答了
Note that if a given signal was previously set to be ignored,
this code avoids altering that setting.
This is because non-job-control shells often ignore certain signals
when starting children, and it is important for the children to respect this.
开发平台(Platform): GCC
额外使用到的函数库(Library Used): NO
问题(Question):
我在很多地方都会看到signal()被重复呼叫,有点不解。
比如说我在GNU C Library的24.3.1节,看到如下的范例。
(原文网址: http://goo.gl/Kz7yhf)
如果我没有理解错误,signal的return是原本的默认Handler,所以,
if (signal (SIGINT, termination_handler) == SIG_IGN)
signal (SIGINT, SIG_IGN);
表示如果SIGINT的默认处理方式是忽略(SIG_IGN),则再将
SIGINT出现时的Hander设成SIG_IGN,即恢复成忽略。
那这样做,signal (SIGINT, termination_handler)不就变成没做用了吗?
那还费时去写termination_hander()做什么?
它为什么要这么做?
程式码(Code):
#include <signal.h>
void termination_handler (int signum) {
struct temp_file *p;
for (p = temp_file_list; p; p = p->next) unlink (p->name);
}
int main (void) {

if (signal (SIGINT, termination_handler) == SIG_IGN)
signal (SIGINT, SIG_IGN);
if (signal (SIGHUP, termination_handler) == SIG_IGN)
signal (SIGHUP, SIG_IGN);
if (signal (SIGTERM, termination_handler) == SIG_IGN)
signal (SIGTERM, SIG_IGN);

}
楼主: rudyych (熊熊)   2014-10-02 10:16:00
啊... 这个我懂了。可是我无法自删本文。
作者: loveme00835 (发箍)   2014-10-04 03:45:00
问完删文怕丢脸吗?
楼主: rudyych (熊熊)   2014-10-04 11:12:00
我自问自答没麻烦到大家。想自删是不想浪费大家时间看我的问题。不过你这样就嘘我就很没有礼貌。
作者: uranusjr (←這人是超級笨蛋)   2014-10-05 01:54:00
真不想浪费人时间就把你自己的解答放上来, 让下一个人能直接看到答案。你这样满足自己需求之后想断别人路, 被人质疑还在那边争, 才真的浪费时间(还顺便浪费别人时间)
作者: KoenigseggG (地表最速)   2014-10-05 21:21:00
他板主这样嘘用意不同吧@@
楼主: rudyych (熊熊)   2014-10-06 12:40:00
U大,为了怕大家费时看我问题,我的确有把我的回答放在最上面。就是因为他是板主,我才觉得他不可以这样就嘘我。其实这篇保留,对我也我所谓。我真的只是怕浪费大家时间。造成大家困扰,真的很抱歉。

Links booklink

Contact Us: admin [ a t ] ucptt.com