进程串
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
int main(){
int p1,p2,p3,p4;
while((p1=fork())==-1);
if(p1==0){
printf("child %d parent %d\n",getpid(),getppid());
while((p2=fork())==-1);
if(p2==0){
// child process
printf("child %d parent %d\n",getpid(),getppid());
while((p3=fork())==-1);
if(p3==0){
printf("child %d parent %d\n",getpid(),getppid());
while((p4=fork())==-1);
}
}
}
//printf("child %d parent %d\n",getpid(),gitppid());
return 0;
}
~
~
进程树
#include <stdio.h>
#include <unistd.h>
int main(){
int p1,p2,p3,p4,p5,p6;
while((p4=fork())<0);
while((p1=fork())<0);
if(p1==0){
printf("child %d parent %d\n",getpid(),getppid());
while((p2=fork())<0);
if(p2==0){
printf("child %d parent %d\n",getpid(),getppid());
while((p3=fork())<0);
if(p3==0){
printf("child %d parent %d\n",getpid(),getppid());
}
}
}
// 如果不是else 则发现会子进程执行
else if(p4==0){
printf("child %d parent %d\n",getpid(),getppid());
while((p5=fork())<0);
if(p5==0){
printf("child %d parent %d\n",getpid(),getppid());
}
while((p6=fork())<0);
if(p5==0){
printf("child %d parent %d\n",getpid(),getppid());
}
}
return 0;
}