在父母继续前进之前,如何等待所有孩子终止? [英] How to wait for all the children to terminate before the parent moves on?
问题描述
我正在做一些并行编程(多处理),我需要父代:
I'm doing some parallel programming (multiprocessing) and I need the parent to:
1)Fork
几个孩子
2)创建完所有子项之后,只需等待所有子项终止
2) AFTER all the children have been created, simply WAIT for all of them to terminate
3)在所有孩子都被解雇之后,做一些其他工作.
3) After all the children are terminated, do some other work.
这是我尝试过的:
int main(int argc, char **argv[])
{
int j, i;
pid_t children[3];
int pid;
// Fork the processes
for(j = 0; j < 3; j++){
if((children[j] = fork()) == 0){
// Child process
for(i = 0; i < 2; i++){
printf("child %d printing: %d\n", j, i);
}
}else {
// In parent now
while (pid = waitpid(-1, NULL, 0)) {
if (errno == ECHILD) {
break;
}
}
printf("all children terminated. in parent now\n");
}
}
return 0;
}
没有给出正确的输出.甚至在所有孩子都死之前,多次打印所有孩子都已终止.现在在父母身边".另外,对于每个过程,我应该只看到2个输出,但是我看到了更多.任何帮助将不胜感激.
Doesn't give the correct output. "all children terminated. in parent now" gets printed out several times, even before all the children are dead. Also, for each process, I should see only 2 outputs, but I see more. Any help would be appreicated.
推荐答案
这是否是您想要实现的目标?我只是为每个孩子设置一个简单的计数,但要增加延迟以提高并行化可见性.
Is this more what you are trying to achieve? I've just set a simple count for each child with a delay to increase the parallelisation visibility.
#include <sys/wait.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
int main(int argc, char **argv[])
{
int i, j, k;
int pid;
// Fork the processes
for(j = 0; j < 3; j++)
{
if (fork() == 0)
{
printf("starting child %d\n", j);
// Child process - do your child stuff
for (i = 0; i < 5; ++i)
{
for (k = 0; k < 10000000; ++k);
printf("child %d printing: %d\n", j, i);
}
printf("child %d ending\n", j);
// then just quit
exit(0);
}
}
j = 1;
while (wait(NULL) > 0)
{
printf("%d child completed\n", j++);
}
// In parent now
printf("all children terminated. in parent now\n");
return 0;
}
我得到的输出是
starting child 0
starting child 1
child 0 printing: 0
starting child 2
child 1 printing: 0
child 0 printing: 1
child 2 printing: 0
child 1 printing: 1
child 0 printing: 2
child 0 printing: 3
child 2 printing: 1
child 1 printing: 2
child 1 printing: 3
child 0 printing: 4
child 0 ending
child 2 printing: 2
1 child completed
child 1 printing: 4
child 1 ending
2 child completed
child 2 printing: 3
child 2 printing: 4
child 2 ending
3 child completed
all children terminated. in parent now
肯
这篇关于在父母继续前进之前,如何等待所有孩子终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!