父子进程
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
int main(void)
{
int i=0,j=0,k=0,n=0;;
pid_t pid[5];
pid_t pid_d[5];
pid_t pid_d_cpy[5];
pid_t pid_cpy[5];
pid_t pi;
int *status;
printf("pid=%d ppid=%d\n",getpid(),getppid());
_go:
pid[j]=fork();
if(pid[j]==0)
{
printf("i=%d pid=%d ppid=%d\n",i,getpid(),getppid());
_DOT:
pid_d[k]=fork();
if(pid_d[k]==0)
{
printf("i=%d pid=%d ppid=%d\n",i,getpid(),getppid());
sleep(1);
}
else if(pid_d[k]>0)
{
k++;
if(k<5)
{
goto _DOT;
}
for(n=0;n<k;n++)
{
if(pid_d[n]>pid_d[n+1])
{
pi=pid_d[n];
pid_d[n]=pid_d[n+1];
pid_d[n+1]=pi;
}
}
waitpid(pid_d[0],status , 0);
waitpid(pid_d[1],status , 0);
waitpid(pid_d[2],status , 0);
waitpid(pid_d[3],status , 0);
waitpid(pid_d[4],status , 0);
}
}
else if(pid[j]>0)
{
i++;
j++;
if(i<5)
{
goto _go;
}
for(n=0;n<i;n++)
{
if(pid[n]>pid[n+1])
{
pi=pid[n];
pid[n]=pid[n+1];
pid[n+1]=pi;
}
}
waitpid(pid[0],status , 0);
waitpid(pid[1],status , 0);
waitpid(pid[2],status , 0);
waitpid(pid[3],status , 0);
waitpid(pid[4],status , 0);
}
printf("pid=%d will exit\n",getpid());
return 0;
}
标签:status,include,pid,父子,printf,进程,pi,waitpid
From: https://www.cnblogs.com/yesiming/p/18028110