标签:fork lib include ppi wait 结果 print clu 子进程
创建如图的进程:
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<wait.h>
int main()
{
int pid=fork();//创建子进程
if(pid==0)
{
printf("D:%d\t %d\n",getppid(),getpid());return 0;//子进程结束
}
int pid2=fork();//创建子进程
if(pid2==0)
{
printf("C:%d\t %d\n",getppid(),getpid());waitpid(pid2,NULL,0);return 0;//子进程结束
}
int pid3=fork();
if(pid3==0)//创建子进程
{
printf("B:%d\t %d\n",getppid(),getpid());return 0;//子进程结束
}
printf("父进程id 当前进程id\n");
printf("A:%d\t %d\n",getppid(),getpid());
waitpid(pid,NULL,0);waitpid(pid2,NULL,0);waitpid(pid3,NULL,0);//等待子进程结束;
return 0;
}
结果如下:
父进程id 当前进程id A:3195 6051 D:6051 6052 C:6051 6053 B:6051 6054
创建如图的进程:
#include<stdio.h>
#include<unistd.h>
#include<wait.h>
#include<stdlib.h>
int main()
{
int pid=fork();int pid3;
if(pid==0)
{
printf("C:%d\t %d\n",getppid(),getpid());return 0;
}
int pid2=fork();
if(pid2==0)
{
pid3=fork();
if (pid3==0)
{
printf("D:%d\t %d\n",getppid(),getpid());return 0;
}
else
{
printf("B:%d\t %d\n",getppid(),getpid());return 0;
}
}
printf("父进程id 当前进程id\n");
printf("A:%d\t %d\n",getppid(),getpid());
waitpid(pid,NULL,0);waitpid(pid2,NULL,0);waitpid(pid3,NULL,0);
return 0;
}
结果如下:
父进程id 当前进程id A:3195 6071 C:6071 6072 B:6071 6073 D:6073 6074
标签:fork lib include ppi wait 结果 print clu 子进程
原文地址:https://www.cnblogs.com/it8343/p/9236563.html