c++ - How to wait until all child processes called by fork() complete? -


i forking number of processes , want measure how long takes complete whole task, when processes forked completed. please advise how make parent process wait until child processes terminated? want make sure stop timer @ right moment.

here code use:

#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 executed child process              // magic here              // end             exit(0);         }         else if (pid[indexofprocess] < 0)    // failed fork         {             cerr << "failed fork" << endl;             exit(1);         }         else                         // parent         {             // if(indexofprocess==pcount-1) , loop 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 , " << lapsed.tv_usec    << " usec"<< endl << endl;          }      }//for  }//main 

i'd move after line "else //parent" down, outside loop. after loop of forks, loop waitpid, stop clock , rest:

for (int = 0; < pidcount; ++i) {     int status;     while (-1 == waitpid(pids[i], &status, 0));     if (!wifexited(status) || wexitstatus(status) != 0) {         cerr << "process " << << " (pid " << pids[i] << ") failed" << endl;         exit(1);     } }  gettimeofday (&second, &tzp); //stop time 

i've assumed if child process fails exit status of 0, didn't complete work, , therefore test has failed produce valid timing data. if child processes supposed killed signals, or exit non-0 return statuses, you'll have change error check accordingly.

an alternative using 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);         }     } } 

this 1 doesn't tell process in sequence failed, if care can add code in pids array , index.


Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -