相关疑难解决方法(0)

如何等待fork()调用的所有子进程完成?

我正在分配许多进程,我想测量完成整个任务所需的时间,即分叉完成所有进程的时间.请告知如何让父进程等到所有子进程终止?我想确保在合适的时刻停止计时器.

这是我使用的代码:

#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

            // The End
            exit(0);
        } …
Run Code Online (Sandbox Code Playgroud)

c++ linux parallel-processing gcc

18
推荐指数
2
解决办法
6万
查看次数

标签 统计

c++ ×1

gcc ×1

linux ×1

parallel-processing ×1