/*
* thread.cpp
* Copyright (C) 2020 root <root@tw.com>
*
* Distributed under terms of the MIT license.
*/
#include <iostream>
#include<unistd.h>
using namespace std;
int
main()
{
pid_t pid;
cout<<"父进程"<<endl;
for(int i = 0; i < 5; ++i)
{
while((pid = fork()) == -1);
if(pid == 0) break; // 防止子进程作为父进程导致指数增长创建进程
}
if(pid == 0)
{
cout<<"i am child,pid = "<<getpid()<<"my parent is:"<<getppid()<<endl;
}
else
{
sleep(1);
cout<<"i am parent, pid= "<<getpid()<<endl;
}
cout<<"both have!"<<endl;
return 0;
}