Linux练习(wait父进程等待子进程)

#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>

int main()
{
        pid_t pid;
        char *m;
        int n;
        pid=fork();
        if(pid==0)
        {
                m="this is child";
                n=5;
        }
        else
        {
                m="this is father";
                n=3;    
        }

        for(;n>0;n--)
        {

                puts(m);
                sleep(1);
        }
        if(pid!=0)
        {
            pid_t child_pid;
            child_pid=wait(NULL);
            
            printf("child has finished: %d\n",child_pid);
        
        }
        exit(0);

}
原文地址:https://www.cnblogs.com/tiandsp/p/2707378.html