+ // Fork up to MAX_NUM_CHILDREN processes, then
+ // wait. As soon as a proc completes, fork the next.
+
+ int nextTestCase = 0;
+ int numRunningChildren = 0;
+ while( nextTestCase < numTestCases || numRunningChildren > 0)
+ {
+ if( nextTestCase < numTestCases )
+ {
+ while( numRunningChildren < MAX_NUM_CHILDREN )
+ {
+ int pid = fork();
+ if( pid == 0 ) // Child process
+ {
+ close(STDOUT_FILENO);
+ close(STDERR_FILENO);
+ exit( RunTestCase( tc_array[nextTestCase] ) );
+ }
+ else if(pid == -1)
+ {
+ perror("fork");
+ exit(2);
+ }
+ else // Parent process
+ {
+ TestCase tc(nextTestCase, tc_array[nextTestCase].name);
+ children[pid] = tc;
+ nextTestCase++;
+ numRunningChildren++;
+ }
+ }
+ }
+
+ int status=0;
+ int childPid = waitpid(-1, &status, 0);
+ if( childPid == -1 )
+ {
+ perror("waitpid");
+ exit(2);
+ }
+
+ if( WIFEXITED(status) )
+ {
+ if( childPid > 0 )
+ {
+ int testResult = WEXITSTATUS(status);
+ if( testResult )
+ {
+ printf("Test case %s failed: %d\n", children[childPid].testCaseName, testResult);
+ failedTestCases.push_back(children[childPid].testCase);
+ numFailures++;
+ }
+ else
+ {
+ numPasses++;
+ }
+ numRunningChildren--;
+ }
+ }