回收子进程的两种方法

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <sys/wait.h>
void  sigchld(int signum){
    //version 1.导致问题,若同时有三个子进程同时死亡,会丢失信号
    //int r = wait(0);
    //printf("%d process is recycled\n",r);
    //version 2.导致问题,wait等不到子进程会阻塞.影响父进程。
    /*for(;;){
        pid_t pid = wait(0);
        if(pid == -1){
            if(errno != ECHILD){
                perror("wait");
                exit(-1);
            }
            printf("all child process have been recycled\n");
            break;
        }
        printf("%d process is recycled\n",pid);
        }*/
    //vesion 3解决1,2出现的问题
    for(;;){
        pid_t pid = waitpid(-1,0,WNOHANG);
        if(pid == -1){
            if(errno != ECHILD){
                perror("waitpid");
                exit(-1);
            }
            printf("all child process have been recycled\n");
            break;
        }
        if(pid){
            printf("%d process was recycled\n",pid);
        }else{
            break;
        }
    }
    //for(;;)回收所有子进程,waitpid防止子进程不结束时候阻塞的情况.
}
int main(){
//    signal(SIGCHLD,SIG_IGN);//第一种方法,直接忽略SIGCHLD信号
    signal(SIGCHLD,sigchld);//第二种方法,自己写处理函数
    pid_t pid = 0;
    for(int i = 0;i < 3;++i){
        pid = fork();
        if(pid == -1){
            perror("fork");
            exit(-1);
        }
        if(pid == 0){
            if(i == 2)
                sleep(10);
            printf("I am %u child process, I will die\n",getpid());
            return 0;
        }
    }
    /*for(;;){
        int ret = wait(0);
        if(ret == -1){
            if(errno != ECHILD){
                perror("wait");
                exit(-1);
            }
            printf("All child processes have been reclaimed\n");
            return 0;
        }
        printf("%d child process id recycled\n",ret);
    }*/
    while(1){
        printf("waiting...\n");
        sleep(1);
    }
}
 

猜你喜欢

转载自blog.csdn.net/wWX336815/article/details/85229701