• <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

            常用鏈接

            相冊

            最新評論

            91精品国产高清91久久久久久| 久久久久亚洲精品男人的天堂| 久久免费视频一区| 国产精品久久久天天影视香蕉| 久久久国产乱子伦精品作者| 少妇久久久久久被弄高潮| 婷婷国产天堂久久综合五月| 久久久久久久综合日本| 久久e热在这里只有国产中文精品99 | 情人伊人久久综合亚洲| 国产精品青草久久久久婷婷 | 青青青青久久精品国产| 久久99精品久久久久久水蜜桃 | 香港aa三级久久三级| 久久se精品一区精品二区| 人妻精品久久久久中文字幕一冢本| 亚洲va久久久噜噜噜久久天堂 | 99久久人妻无码精品系列蜜桃| 久久久久久人妻无码| 久久无码av三级| 久久综合精品国产一区二区三区| 免费精品久久天干天干| 亚洲精品无码久久千人斩| 精品熟女少妇a∨免费久久| 青青草原综合久久大伊人精品| 国产精品青草久久久久福利99 | 人妻无码精品久久亚瑟影视 | 久久久久久国产精品免费免费| 区久久AAA片69亚洲| 91精品国产91久久综合| 欧美午夜精品久久久久久浪潮| 久久婷婷人人澡人人爽人人爱| 97超级碰碰碰久久久久| 中文成人久久久久影院免费观看| 久久久久久毛片免费播放| 亚洲精品WWW久久久久久| 国产成人综合久久综合| 久久人人爽人人爽人人片AV不| 久久久久久久尹人综合网亚洲| 热99RE久久精品这里都是精品免费 | 精品无码久久久久久国产|