linux环境编程-waitpid(僵尸进程回收)

前面一篇文章介绍了 wait的 僵尸进程的回收,现在我们来介绍一下waitpid(与wait类似),那么这个函数 怎么使用呢

先说好  这些东西 真不用记住  留个印象 来查就行了 

waitpid 作用同 wait,可以不用阻塞。并且可以 指定进程ID,对进程进行清理。它的返回值 有些特殊

pid_t  waitpid(pid_t pid,  int *status, int options);   

  • 成功: return 清理掉的子进程ID
  • 失败:   return -1(无子进程)
  • 还有一个  return 0;  子进程 还没有结束 (回收不了) 

来看看参数:

pid:   

  •          > 0   表示回收指定的 子进程
  •          -1    回收任意子进程(相当于wait)
  •          0     回收 当前和 调用 waitpid一个组的所有 子进程
  •         < -1   回收指定进程组内的任意子进程

注意:调用一次 waitpid  或者是 wait 只能回收一个子进程,要回收多个,就应该用循环

options:

参数 option 可以为 0 或下面的 OR 组合:
1.WNOHANG 如果没有任何已经结束的子进程则马上返回,不予以
等待。
2.WUNTRACED 如果子进程进入暂停执行情况则马上返回,但结束
状态不予以理会。
子进程的结束状态返回后存于 status,底下有几个宏可判别结束情
况:
3.WIFEXITEDstatus)如果子进程正常结束则为非 0 值。
4.WEXITSTATUSstatus)取得子进程 exit()返回的结束代码,一
般会先用 WIFEXITED 来判断是否正常结束才能使用此宏。
5.WIFSIGNALEDstatus)如果子进程是因为信号而结束则此宏值为
6.WTERMSIGstatus) 取得子进程因信号而中止的信号代码,一般
会先用 WIFSIGNALED 来判断后才使用此宏。
7.WIFSTOPPEDstatus) 如果子进程处于暂停执行情况则此宏值为
真。一般只有使用 WUNTRACED 时才会有此情况。
8.WSTOPSIGstatus) 取得引发子进程暂停的信号代码,一般会先
WIFSTOPPED 来判断后才使用此宏。
 

代码Demo

#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <unistd.h>


using namespace std;

#define FORK_NUM 5

int
main(int argc, char* argv[])
{
        pid_t pid, rmpid;
        int i;

        for(i=0; i<FORK_NUM; i++){
                pid = fork();
                if(pid == 0){
                        break;
                }else if(pid > 0){
                        printf("creat %dth son \n", i+1);
                        if(i == 2){
                                rmpid = pid;
                        }
                }else{
                        printf("creat %dth son error\n", i+1);
                }
        }

        sleep(i+1);

        if(i==FORK_NUM){
                int status;
                printf("I am parent\n");
        #if 0   // wait one son progress
                if(waitpid(rmpid, &status, 0) == -1){// only wait 3th son progress
                        perror("waipid error");
                        exit(1);
                }
                if(WIFEXITED(status)!=0 && WIFSIGNALED(status)==0){ 
                        printf("son progress normal exit\n");
                        printf("son progress exit status : %d \n", WEXITSTATUS(status));
                }else{// 0   
   printf("son expretion exit,signal ID:%d \n",  WTERMSIG(status));
                }
                printf("the %dth is waitpided by me\n", i+1);
                while(1)sleep(1); 
        #else  // wait all of son progress
                do{
                        rmpid = waitpid(-1, &status, WNOHANG);
                        if(rmpid == 0){
                                sleep(1);
                                continue;
                        }
                        else if(rmpid == -1)break;
                        else{
                                i--;
                                printf("has %d son progress wll to be wait\n", i);
                        }

                }while(i > 0);

                while(1) sleep(1);

        #endif
        }else{
                printf("I am child %d \n", i+1);
        }


        return 0;
}

     
 

猜你喜欢

转载自blog.csdn.net/qq_44065088/article/details/108690543