我正在循环中创建多个子进程。每个孩子都会做这件事,他们中的任何一个人都可以先结束。(不确定是否相关,但:每个孩子都有一个孙子)
当一个子进程完成后,我如何等待任何子进程终止并停止其他进程?
for(i=0; i<numberOfChildren; i++)
{
pid = fork();
if(pid < 0)
{
fprintf(stderr, "Error: fork Failed\n");
return EXIT_FAILURE;
}
/* Child Process */
if(pid == 0)
{
/* Do your thing */
}
/* Parent process */
else
{
childrenPid[i]=pid;
}
}