我想在一位家长中分娩三个孩子的过程。 和下面是我的C++代码:在一位家长中分娩多个孩子
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <iostream>
using namespace std;
int main()
{
pid_t pid;
for (int i = 0; i < 3; i++)
{
pid = fork();
if (pid < 0)
{
cout << "Fork Error";
return -1;
}
else if (pid == 0)
cout << "Child " << getpid() << endl;
else
{
wait(NULL);
cout << "Parent " << getpid() << endl;
return 0;
}
}
}
现在我的输出是:
Child 27463
Child 27464
Child 27465
Parent 27464
Parent 27463
Parent 27462
我应该如何修改我的程序得到像下面这样的输出?
Child 27463
Child 27464
Child 27465
Parent 27462
我的意思是这三个孩子需要属于同一个父母,有人可以给我一些建议吗?
谢谢大家。 :)
是的!这是正确的。非常感谢 : ) – jaychung