#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid;
int i=0;
for(i=0;i<5;i++)
{
pid = fork();
if(pid == -1)
{
perror("fork error:");
exit(1);
}
else if(pid == 0)
{
break; //子进程跳出循环
}
}
sleep(i);
if(i<5)
{
printf("I am child%d,pid=%d,ppid=%d \n",i+1,getpid(),getppid());
}
else
{
printf("I am parent,pid=%d,ppid=%d \n",getpid(),getppid());
}
return 0;
}