以下问题已找到答案了,各位不用回答了
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);
…
}