亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 5525 | 回復: 5
打印 上一主題 下一主題

[Linux] [結貼]signal()注冊的信號處理函數(shù)里面,不需要再次注冊信號處理函數(shù)嗎? [復制鏈接]

論壇徽章:
1
2015元宵節(jié)徽章
日期:2015-03-06 15:53:22
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-04-28 18:49 |只看該作者 |倒序瀏覽
本帖最后由 sentto2 于 2015-04-29 09:31 編輯

網(wǎng)上說,linux下面的signal函數(shù),其實現(xiàn)機制是,在調用信號處理函數(shù)之前就會自動把信號處理函數(shù)重置,也就是信號處理函數(shù)只調用一遍。
如果需要多次都有效的話,信號處理函數(shù)里面要再次用signal()注冊自身。

但是我寫代碼測試了一下,發(fā)現(xiàn)不用在信號處理函數(shù)里面再去注冊自己啊,代碼如下:

  1. #include<signal.h>
  2. #include<stdio.h>
  3. void f(int sig)
  4. {
  5.   printf("input=%d\n",sig);
  6. }
  7. int main()
  8. {
  9.   signal(SIGHUP,f);
  10.   int i = getchar();
  11.   puts("hello world");
  12.   return 0;
  13. }
復制代碼
編譯運行g++ ss.cpp&&./a.out
然后在另一個終端里面去不停的發(fā)信號:

  1. $ ps -ef|grep a.out
  2. x        27498 27421  0 23:14 pts/3    00:00:00 ./a.out
  3. x        27501 26574  0 23:15 pts/2    00:00:00 grep --color=auto a.out
  4. [x@localhost ~]$ kill -1 27498
  5. [x@localhost ~]$ kill -1 27498
復制代碼
那么運行a.out的終端就會不停的輸出:

  1. $ g++ ss.cpp&&./a.out
  2. input=1
  3. input=1
復制代碼
這是為什么呢? 不是說signal()注冊的函數(shù)只是一次有效的嗎?

求職 : 機器學習
論壇徽章:
79
2015年亞洲杯紀念徽章
日期:2015-05-06 19:18:572015七夕節(jié)徽章
日期:2015-08-21 11:06:172015亞冠之阿爾納斯爾
日期:2015-09-07 09:30:232015亞冠之薩濟拖拉機
日期:2015-10-21 08:26:3915-16賽季CBA聯(lián)賽之浙江
日期:2015-12-30 09:59:1815-16賽季CBA聯(lián)賽之浙江
日期:2016-01-10 12:35:21技術圖書徽章
日期:2016-01-15 11:07:2015-16賽季CBA聯(lián)賽之新疆
日期:2016-02-24 13:46:0215-16賽季CBA聯(lián)賽之吉林
日期:2016-06-26 01:07:172015-2016NBA季后賽紀念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16賽季CBA聯(lián)賽之浙江
日期:2017-07-18 13:41:54
2 [報告]
發(fā)表于 2015-04-28 19:15 |只看該作者
signal注冊一次,就可以了啊
  1. #include<signal.h>
  2. #include<stdio.h>
  3. void f(int sig)
  4. {
  5.     printf("input=%d\n",sig);
  6. }
  7. int main()
  8. {
  9.     signal(SIGINT,f);
  10.     while(1)
  11.     {   
  12.         printf("a\n");
  13.         sleep(3);
  14.    
  15.     }   
  16.     return 0;
  17. }
復制代碼
你試試這個,每次發(fā)生信號,都會觸發(fā)這個函數(shù),不需要再信號處理函數(shù)中重新注冊

論壇徽章:
1
2015元宵節(jié)徽章
日期:2015-03-06 15:53:22
3 [報告]
發(fā)表于 2015-04-28 21:11 |只看該作者
zsszss0000 發(fā)表于 2015-04-28 19:15
signal注冊一次,就可以了啊你試試這個,每次發(fā)生信號,都會觸發(fā)這個函數(shù),不需要再信號處理函數(shù)中重新注冊


謝謝,我的問題就是這樣,你說的不是我想要的: 因為網(wǎng)上總說signal函數(shù)注冊的處理函數(shù),里面必須再次注冊自己才能保證每次都被信號觸發(fā)。換句話說,signal()函數(shù)調用是不是會恢復默認的信號處理函數(shù)呢?

謝謝。

求職 : 機器學習
論壇徽章:
79
2015年亞洲杯紀念徽章
日期:2015-05-06 19:18:572015七夕節(jié)徽章
日期:2015-08-21 11:06:172015亞冠之阿爾納斯爾
日期:2015-09-07 09:30:232015亞冠之薩濟拖拉機
日期:2015-10-21 08:26:3915-16賽季CBA聯(lián)賽之浙江
日期:2015-12-30 09:59:1815-16賽季CBA聯(lián)賽之浙江
日期:2016-01-10 12:35:21技術圖書徽章
日期:2016-01-15 11:07:2015-16賽季CBA聯(lián)賽之新疆
日期:2016-02-24 13:46:0215-16賽季CBA聯(lián)賽之吉林
日期:2016-06-26 01:07:172015-2016NBA季后賽紀念章
日期:2016-06-28 17:44:45黑曼巴
日期:2016-06-28 17:44:4515-16賽季CBA聯(lián)賽之浙江
日期:2017-07-18 13:41:54
4 [報告]
發(fā)表于 2015-04-28 21:56 |只看該作者
signal的handler不會恢復默認的處理函數(shù)回復 3# sentto2


   

論壇徽章:
2
2015年亞洲杯之烏茲別克斯坦
日期:2015-04-15 15:43:482015亞冠之迪拜阿赫利
日期:2015-06-30 20:36:46
5 [報告]
發(fā)表于 2015-04-29 17:45 |只看該作者
參考man signal
  1.        In  the  original UNIX systems, when a handler that was established using signal() was invoked by the delivery of a signal, the disposition
  2.        of the signal would be reset to SIG_DFL, and the system did not block delivery of further instances of the signal.  System V also  provides
  3.        these  semantics  for  signal().   This  was bad because the signal might be delivered again before the handler had a chance to reestablish
  4.        itself.  Furthermore, rapid deliveries of the same signal could result in recursive invocations of the handler.

  5.        BSD improved on this situation by changing the semantics of signal handling (but, unfortunately, silently changed the semantics when estab‐
  6.        lishing  a handler with signal()).  On BSD, when a signal handler is invoked, the signal disposition is not reset, and further instances of
  7.        the signal are blocked from being delivered while the handler is executing.

  8.        The situation on Linux is as follows:

  9.        * The kernel's signal() system call provides System V semantics.

  10.        * By default, in glibc 2 and later, the signal() wrapper function does not invoke the kernel system call.  Instead, it  calls  sigaction(2)
  11.          using  flags  that supply BSD semantics.  This default behavior is provided as long as the _BSD_SOURCE feature test macro is defined.  By
  12.          default, _BSD_SOURCE is defined; it is also implicitly defined if one defines _GNU_SOURCE, and can of course be explicitly defined.

  13.          On glibc 2 and later, if the _BSD_SOURCE feature test macro is not defined, then signal() provides  System  V  semantics.   (The  default
  14.          implicit definition of _BSD_SOURCE is not provided if one invokes gcc(1) in one of its standard modes (-std=xxx or -ansi) or defines var‐
  15.          ious other feature test macros such as _POSIX_SOURCE, _XOPEN_SOURCE, or _SVID_SOURCE; see feature_test_macros(7).)

  16.        * The signal() function in Linux libc4 and libc5 provide System V semantics.  If one on a libc5 system includes <bsd/signal.h>  instead  of
  17.          <signal.h>, then signal() provides BSD semantics.
復制代碼

論壇徽章:
1
2015元宵節(jié)徽章
日期:2015-03-06 15:53:22
6 [報告]
發(fā)表于 2015-04-29 18:42 |只看該作者
bfdhczw 發(fā)表于 2015-04-29 17:45
參考man signal


說的很好,默認編譯,signal()調用就是POSIX行為,等同于調用sigaction,會阻塞,沒有disposition.
如果編譯時加了額外的選項例如-std=c99,那么就是SysV的行為,調用內核的signal。

結貼!
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP