【发布时间】:2016-06-13 20:18:32
【问题描述】:
我希望我的父进程在继续之前等待所有子进程完成,我有一个解决方案。
int status;
pid_t pid = 0;
int num = 0;
for (int i = 0; i < NUMBER_OF_PROCESSES; i++)
{
pid = fork();
if (pid == 0)
{
//printf("Hello from Child\n");
sleep(5 - i);
printf("Hello from Child %d\n",i + 1);
num++;
return 0;
}
else if (pid)
{
waitpid(pid, &status, 0);
continue;
}
else
{
printf("Error\n");
exit(1);
}
}
printf("Hello from the process, currentPid : %d, pid : %d\n", getpid(), pid);
return 0;
但似乎我必须等待每个子进程完成之前,有什么办法可以让所有子进程都必须能够并行运行?
【问题讨论】: