• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Prayer

            在一般中尋求卓越
            posts - 1256, comments - 190, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            10.7 SIGCLD semantics

            Posted on 2009-04-14 15:40 Prayer 閱讀(876) 評論(0)  編輯 收藏 引用 所屬分類: LINUX/UNIX/AIX

            10.7 SIGCLD semantics

            SIGCLD是在system v的系列版本中使用的一個信號。SIGCHLDBSD系列版本中使用的信號,他們的意義有所不同,其中SIGCLD應該被禁用。其原因如下:

            () system SIGCLD

            SVR4是從system v發展出來的,它使用SIGCLD的方法(如下都以signalsigset函數作為signal安裝函數)

            1.被推薦的使用SIGCLD的方法

            設置SIGCLDSIG_IGN,這樣生成的孩子process在退出后不會生成zombia

            2.不被推薦的使用方法

            就是默認的對SIGCLD的處理方法,即SIG_DFL,子進程結束后不處理,如果父親wait的話,會等所有的孩子進程結束后才返回-1errno=ECHLD。這不是推薦的。

             

            可見在System v系列里,對SIGCLD的使用推薦使用SIG_IGN

             

            3system vSIGCLD的一個被詬病的地方

            就是當用signal安裝SIGCLDhandler的時候,他會立馬檢查當前是否有孩子進程可以被waited,如果有,就立馬調用handler這會造成stack overflow,例如:我們用signal安裝handler的時候,經常在signal handler里面首先將handler reinstall, 好,當有一個子進程結束了,發送了SIGCLD信號,該handler被調用,reinstall handler, 立馬檢查是否有可以waited的進程,有,立馬調用handler, ….如此循環,不久死掉了么.

            如下就是例子代碼:

            Figure 10.6. System V SIGCLD handler that doesn't work

            #include      "apue.h"

            #include      <sys/wait.h>

             

            static void sig_cld(int);

             

            int

            main()

            {

                pid_t   pid;

             

                if (signal(SIGCLD, sig_cld) == SIG_ERR)

                    perror("signal error");

                if ((pid = fork()) < 0) {

                    perror("fork error");

                } else if (pid == 0) {      /* child */

                    sleep(2);

                    _exit(0);

                }

                pause();    /* parent */

                exit(0);

            }

             

            static void

            sig_cld(int signo)   /* interrupts pause() */

            {

                pid_t   pid;

                int     status;

             

                printf("SIGCLD received\n");

                if (signal(SIGCLD, sig_cld) == SIG_ERR) /* reestablish handler */

                    perror("signal error");

                if ((pid = wait(&status)) < 0)      /* fetch child status */

                    perror("wait error");

                printf("pid = %d\n", pid);

            }

             

            可見system v系列不能給SIGCLD安裝handler,那么就只能采用推薦的方法SIG_IGN了。使其子進程壓根不產生zombia.

             

            (二)SIGCHLD

            linux里,用signal安裝SIGCHLD,且將handler置為SIG_IGN,也會達到是子進程退出后不產生zombia的好處。而默認的我們不處理該信號,就會產生zombiaBSD系列就是這樣。

            如下是摘自linux suse man page的一段話:

            POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN.  POSIX.1-2001 allows this  possibility, so that ignoring SIGCHLD can be used to prevent the creation of zombies (see wait(2)).  Nevertheless, the historical BSD and System V behaviors for ignoring SIGCHLD differ, so that  the  only  completely  portable  method  of ensuring that terminated children do not become zombies is to catch the SIGCHLD signal and perform a wait(2) or similar.

            鑒于不同平臺的對SIGCHLD的設置成SIG_IGN的意義的不同,唯一的更portable的避免zombia的方法是在SIGCHLDhandler里面調用wait

            (三)使不產生zombia孩子的另一個方法

            使用sigaction并設置SA_NOCLDWAIT標記

             

            (四)在包括linux在內的一些平臺,SIGCLD被宏定為SIGCHLD,放心使用

             

            久久久久97国产精华液好用吗| 精品国产乱码久久久久软件| 久久久久久久尹人综合网亚洲| 久久99精品国产99久久| 国产国产成人久久精品| 久久精品女人天堂AV麻| 亚洲国产精品综合久久一线| 麻豆成人久久精品二区三区免费 | 久久久久久久久久免免费精品| 久久男人中文字幕资源站| 久久亚洲精品成人av无码网站| 久久久艹| 久久久久免费精品国产| 伊人久久大香线蕉综合Av| 国产精品热久久无码av| 国产精品久久久久AV福利动漫| 久久婷婷五月综合成人D啪| 国产亚洲综合久久系列| 久久久久久久91精品免费观看| 97久久精品人人澡人人爽| 日韩久久久久久中文人妻 | 欧美亚洲另类久久综合| 97精品伊人久久大香线蕉| 国产精品综合久久第一页| 久久精品国产亚洲AV香蕉| 亚洲中文字幕无码久久精品1 | 久久综合亚洲欧美成人| 久久久久亚洲国产| 久久精品成人影院| 国产高潮国产高潮久久久91| 久久国产精品99精品国产987| 色诱久久久久综合网ywww | 亚洲欧洲中文日韩久久AV乱码| 国产99久久久国产精品~~牛| 久久狠狠色狠狠色综合| 97久久精品无码一区二区天美| 久久精品天天中文字幕人妻 | 久久久精品久久久久影院| 中文字幕无码av激情不卡久久| 欧美亚洲另类久久综合婷婷| 亚洲精品无码专区久久同性男|