#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
int n = 5;
int i = 0;
printf("Begin ......\n");
pid_t pid=0;
for(;i<n;++i)
{
pid = fork();
if(pid == 0)
{
//son
printf("I am child pid=%d,ppid=%d\n",getpid(),getppid());
break;
}else if(pid > 0)
{
// printf("I am father,pid=%d,ppid=%d\n",getpid(),getppid());
}
}
sleep(i);
if(i < 5)
{
printf("I am child ,will exit,pid=%d,ppid=%d\n",getpid,getppid());
}else{
//father
printf("I am parent,will out pid=%d,ppid=%d\n",getpid(),getppid());
}
while(1)
{
sleep(1);
}
return 0;
}
linux c++(进程相关的函数 & 第二篇 子进程控制顺序)
原文:https://www.cnblogs.com/lodger47/p/14729308.html