[問題] signal()重覆呼叫問題
以下問題已找到答案了,各位不用回答了
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);
…
}
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 59.124.220.19
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1412215337.A.141.html
※ 編輯: rudyych (59.124.220.19), 10/02/2014 10:04:26
→
10/02 10:16, , 1F
10/02 10:16, 1F
※ 編輯: rudyych (59.124.220.19), 10/02/2014 10:19:15
噓
10/04 03:45, , 2F
10/04 03:45, 2F
→
10/04 11:12, , 3F
10/04 11:12, 3F
→
10/04 11:14, , 4F
10/04 11:14, 4F
→
10/05 01:54, , 5F
10/05 01:54, 5F
→
10/05 01:55, , 6F
10/05 01:55, 6F
→
10/05 01:56, , 7F
10/05 01:56, 7F
推
10/05 21:21, , 8F
10/05 21:21, 8F
→
10/06 12:40, , 9F
10/06 12:40, 9F
→
10/06 12:41, , 10F
10/06 12:41, 10F
→
10/06 12:43, , 11F
10/06 12:43, 11F
→
10/06 12:47, , 12F
10/06 12:47, 12F
→
10/06 12:48, , 13F
10/06 12:48, 13F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章