so true

          心懷未來,開創未來!
          隨筆 - 160, 文章 - 0, 評論 - 40, 引用 - 0

          導航

          <2016年2月>
          31123456
          78910111213
          14151617181920
          21222324252627
          282912345
          6789101112

          常用鏈接

          留言簿(8)

          隨筆分類(157)

          隨筆檔案(158)

          牛人博客

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          signal process

          信號學習:
          1。簡單點用signal(主要就1個api,signal(SIGINT, SignalHandler)和signal(SIGINT, SIG_IGN)就是全部了);
          2。復雜點用sigaction(可以設置在handling期間,允許/不允許哪些信號打斷自己);
          3。當前線程handling過程中,當前線程不會因為相同signal再次進入handler,但當前線程可以被別的signal打斷重新進入handler,別的線程可以進入handler;
          例如下面這個例子,只有一個主線程,先被SIGINT打斷進入handler,雖然又被SIGTERM打斷再次進入handler:
          #0  0x00007f7cc55dc200 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
          #1  0x00007f7cc55dc090 in __sleep (seconds=<value optimized out>) at ../sysdeps/unix/sysv/linux/sleep.c:138
          #2  0x0000000000468300 in SignalHandler (sig=15) at src/dispatcher2_server.cpp:78
          #3  <signal handler called>
          #4  0x00007f7cc55dc200 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
          #5  0x00007f7cc55dc090 in __sleep (seconds=<value optimized out>) at ../sysdeps/unix/sysv/linux/sleep.c:138
          #6  0x0000000000468300 in SignalHandler (sig=2) at src/dispatcher2_server.cpp:78
          #7  <signal handler called>
          #8  0x00007f7cc55dc200 in __nanosleep_nocancel () at ../sysdeps/unix/syscall-template.S:82
          #9  0x00007f7cc55dc090 in __sleep (seconds=<value optimized out>) at ../sysdeps/unix/sysv/linux/sleep.c:138
          #10 0x000000000046844a in main (argc=1, argv=0x7fff048454a8) at src/dispatcher2_server.cpp:102
          demo:
          static void SignalHandler(int sig) {
              if (SIGTERM == sig || SIGINT == sig) {
                  struct sigaction sa;
                  memset(&sa, 0, sizeof(sa));
                  sa.sa_handler = SIG_IGN;
                  sigaction(SIGINT, &sa, NULL); // keep other threads from entering this handler
                  sigaction(SIGTERM, &sa, NULL); // keep other threads from entering this handler
                  
                  //...
              }
          }
          struct sigaction sa;
          memset(&sa, 0, sizeof(sa));
          sa.sa_handler = SignalHandler;
          sigfillset(&sa.sa_mask); // block every signal during handling
          sigaction(SIGINT, &sa, NULL);
          sigaction(SIGTERM, &sa, NULL);


          介紹:http://www.alexonlinux.com/signal-handling-in-linux
          demo:http://aspyct.org/blog/2012/08/25/unix-signal-handling-example/


          posted on 2016-02-17 11:30 so true 閱讀(395) 評論(0)  編輯  收藏 所屬分類: C&C++Linux

          主站蜘蛛池模板: 砀山县| 东阿县| 四子王旗| 五峰| 乐东| 凤凰县| 峨边| 郓城县| 西华县| 昆明市| 灌云县| 城口县| 阜平县| 扶余县| 西林县| 金溪县| 靖宇县| 曲水县| 兖州市| 元朗区| 南木林县| 麻栗坡县| 同德县| 湖北省| 淳安县| 洪雅县| 阳泉市| 罗平县| 平遥县| 松滋市| 堆龙德庆县| 新兴县| 仲巴县| 饶河县| 上林县| 英德市| 奇台县| 沭阳县| 新民市| 登封市| 连城县|