如何等待直到所有由fork()调用的子进程完成? [英] How to wait untill all child processes called by fork() complete?

查看:127
本文介绍了如何等待直到所有由fork()调用的子进程完成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我分岔了一些进程,我想测量完成整个任务需要多长时间,即所有分叉的进程完成。请建议如何使父进程等待所有子进程终止?

I am forking a number of processes and I want to measure how long it takes to complete the whole task, that is when all processes forked are completed. Please advise how to make the parent process wait until all child processes are terminated? I want to make sure that I stop the timer at the right moment.

以下是我使用的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <sys/time.h>
#include <sys/wait.h>

using namespace std;

struct timeval first,  second,  lapsed;
struct timezone tzp; 

int main(int argc, char* argv[])// query, file, num. of processes.
{

int pCount = 5; // process count

gettimeofday (&first, &tzp); //start time

pid_t* pID = new pid_t[pCount];

for(int indexOfProcess=0; indexOfProcess<pCount; indexOfProcess++)

{
pID[indexOfProcess]= fork();

if (pID[indexOfProcess] == 0)                // child
{     // code only executed by child process

//magic here

exit(0);// The End
}
else if (pID[indexOfProcess] < 0)            // failed to fork
{
    cerr << "Failed to fork" << endl;
    exit(1);
}
else                         // parent
{
// if(indexOfProcess==pCount-1) and a loop with waitpid??

gettimeofday (&second, &tzp); //stop time
if (first.tv_usec > second.tv_usec)
{  second.tv_usec += 1000000;  second.tv_sec--; }

lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec; 

cout << "Job performed in " <<lapsed.tv_sec << " sec and " << lapsed.tv_usec    << " usec"<< endl << endl;


}

}//for

}//main


推荐答案

我把所有内容移动到else // parent在fork循环之后,对waitpid再做一个for循环,然后停止时钟,并做其余的:

I'd move everything after the line "else //parent" down, outside the for loop. After the loop of forks, do another for loop with waitpid, then stop the clock and do the rest:

for (int i = 0; i < pidCount; ++i) {
    int status;
    while (-1 == waitpid(pids[i], &status, 0));
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
        cerr << "Process " << i << " (pid " << pids[i] << ") failed" << endl;
        exit(1);
    }
}

gettimeofday (&second, &tzp); //stop time

我假设子进程无法正常退出,状态为0,则它没有完成其工作,因此测试未能产生有效的时序数据。显然,如果子进程被假设为被信号杀死,或者退出非0返回状态,则必须相应地更改错误检查。

I've assumed that if the child process fails to exit normally with a status of 0, then it didn't complete its work, and therefore the test has failed to produce valid timing data. Obviously if the child processes are supposed to be killed by signals, or exit non-0 return statuses, then you'll have to change the error check accordingly.

另一种使用wait:

while (true) {
    int status;
    pid_t done = wait(&status);
    if (done == -1) {
        if (errno == ECHILD) break; // no more child processes
    } else {
        if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
            cerr << "pid " << done << " failed" << endl;
            exit(1);
        }
    }
}

你的哪个进程顺序失败,但如果你关心,那么你可以添加代码在pids数组中查找,并获取索引。

This one doesn't tell you which process in sequence failed, but if you care then you can add code to look it up in the pids array and get back the index.

这篇关于如何等待直到所有由fork()调用的子进程完成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆