[問題] signal()重覆呼叫問題

看板C_and_CPP (C/C++)作者 (熊熊)時間11年前 (2014/10/02 10:02), 11年前編輯推噓0(1111)
留言13則, 4人參與, 最新討論串1/1
以下問題已找到答案了,各位不用回答了 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
U大,為了怕大家費時看我問題,我的確有把我的回答放在
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
文章代碼(AID): #1KBB8f51 (C_and_CPP)
文章代碼(AID): #1KBB8f51 (C_and_CPP)