• <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>
            BLUESKY
            一步一個腳印向前走

            今天看了unix網絡編程中的關于wait和waitpid的區別,它采用的驗證例子是客戶/服務器的連接問題
             
            1.當子進程結束的時候,父進程會收到SIGCHLD通知
              2.進程可以調用wait/waitpid等待此Signal
                a.當所有子進程都在執行的時候,會block
                b.當某個子進程中止,成為zombie的時候,立刻返回
                c.如果沒有任何子進程,則返回錯誤

            以前曾經學過這兩函數的使用,但是沒有什么例子可以驗證,今天巧遇網絡編程,就把這兩個函數重新溫習一下:
            pid_t wait(int *stat_loc);
            pid_t waitpid(pid_t pid, int *stat_loc, int options);



            上面就是兩個函數的聲明
            stat_loc返回的是子進程的終止狀態,是個整型值,可以通過一些宏定義來檢測終止的狀態,暫時不介紹了
            The pid argument specifies a set of child processes for which status is requested.
            The waitpid() function shall only return the status of a child process from this set:
              If pid is equal to (pid_t)-1, status is requested for any child process. In this respect, waitpid() is then equivalent to wait().
              If pid is greater than 0, it specifies the process ID of a single child process for which status is requested.
              If pid is 0, status is requested for any child process whose process group ID is equal to that of the calling process.
              If pid is less than (pid_t)-1, status is requested for any child process whose process group ID is equal to the absolute value of pid.

            The options argument is constructed from the bitwise-inclusive OR of zero or more of the following flags, defined in the <sys/wait.h> header:
                [WCONTINUED]
                The waitpid() function shall report the status of any continued child process specified by pid whose status has not been reported
                since it continued from a job control stop.
                [WNOHANG]
                The waitpid() function shall not suspend execution of the calling thread if status is not immediately available for one of the child processes specified by pid
                [WUNTRACED]
                The status of any child processes specified by pid that are stopped, and whose status has not yet been reported since they stopped,
                shall also be reported to the requesting process

                下面我們來具體看看wait和waitpid在網絡編程中使用的區別:

            void sig_chld(int signo)
            {
              pid_t pid;
              
            int stat;
              pid 
            = wait(&stat);
              printf(
            "child %d terminated \n",pid);
              
            return;
            }

            void sig_chld(int signo)
            {
              pid_t pid;
              
            int stat;
              
            while((pid = waitpid(-1,&stat,WNOHANG))>0)
              printf(
            "child %d terminated \n",pid);
              
            return;
            }


              上面是兩段不同的信號處理函數,它們的結果可能相差很多的,下面我們就來看看有哪些區別:
              首先我們看看用wait函數產生的效果:
              1、下面的輸出是在啟動客戶端和服務器端程序后的ps輸出
              備注:在客戶端程序中,我們連續產生5個服務器連接進程,所以一共6個waitsrv進程



              2、下面是在客戶端輸入ctrl+d后的服務器端輸出和ps查詢后的結果





              我們從上面可以看出利用wait的一個致命的缺點就是只能回收一個子進程,其他的進程由于沒有得到回收而變成僵尸進程

              下面我們來看看利用waitpid函數的輸出結果:
               1、啟動服務器端和客戶端后的ps輸出



              2、在客戶端輸入ctrl+d后的服務器端輸出和ps的結果





               我們可以發現所有服務器的子進程在接受到客戶端輸入的EOF后,都被回收了!
             
               由此,我們可以發現,為了減少僵尸進程的產生,我們可以采用waitpid而減少wait的使用,雖然僵尸進程可以由init回收
               以上的例子程序是采用的unix網絡編程中的例子,在此代碼就不再詳細列出了!
            posted on 2007-09-04 09:21 LG 閱讀(2086) 評論(0)  編輯 收藏 引用 所屬分類: UnixProgram

            <2007年6月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            相冊

            最新評論

            国内精品久久久久影院薰衣草 | 无码AV波多野结衣久久| 亚洲国产成人久久综合区| 日批日出水久久亚洲精品tv| 久久精品国产99久久久古代| 97久久久久人妻精品专区| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久综合亚洲鲁鲁五月天| 久久午夜无码鲁丝片| 国产亚洲精久久久久久无码AV| 久久精品青青草原伊人| 中文字幕久久欲求不满| 亚洲中文久久精品无码| 久久一区二区三区免费| 久久66热人妻偷产精品9| 精品国产日韩久久亚洲| 国产精品亚洲美女久久久| 久久综合噜噜激激的五月天| 女同久久| 久久久久亚洲AV无码专区桃色| 国产亚洲精久久久久久无码| 久久久久久久波多野结衣高潮| 久久天天躁狠狠躁夜夜av浪潮 | 久久久亚洲欧洲日产国码是AV | 久久国产精品久久精品国产| 国产偷久久久精品专区| 亚洲精品99久久久久中文字幕| 国产精品热久久毛片| 91久久精品电影| 2020最新久久久视精品爱| 久久午夜电影网| 国产精品久久久久影院色| 1000部精品久久久久久久久| 久久精品午夜一区二区福利| 久久影院综合精品| 蜜臀av性久久久久蜜臀aⅴ麻豆 | AV色综合久久天堂AV色综合在| 久久久av波多野一区二区| 精品综合久久久久久97| 中文国产成人精品久久不卡| 欧美一区二区三区久久综|