]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
make outputs of drd/tests/fork* deterministic
authorPetar Jovanovic <mips32r2@gmail.com>
Wed, 12 Dec 2018 17:45:34 +0000 (17:45 +0000)
committerPetar Jovanovic <mips32r2@gmail.com>
Wed, 12 Dec 2018 17:53:43 +0000 (17:53 +0000)
Wait for children to finish before terminating the main process.

This fixes occasional failures of the following tests:

drd/tests/fork-parallel                  (stderr)
drd/tests/fork-serial                    (stderr)

drd/tests/fork.c

index 07aaf73f74a896fe989ad16787e521379022dec8..0f934ca3c7dc2ee5e03365b6acd00a081a140aae 100644 (file)
@@ -3,8 +3,12 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/wait.h>
 
-static pthread_t tid[2];
+#define NUM_THREADS 2
+
+static pthread_t tid[NUM_THREADS];
+static pid_t pids[NUM_THREADS];
 
 static void *startproc(void *arg)
 {
@@ -16,6 +20,8 @@ static void *startproc(void *arg)
   } else if (pid == 0) {
     dup2(2, 1);    // redirect stdout to stderr
     execv(argv[0], argv); // child
+  } else {
+    *((pid_t*)arg) = pid;
   }
 
   return NULL;
@@ -29,17 +35,20 @@ int main(int argc, char **argv)
   int i = 0;
   int err;
 
-  for (i = 0; i < 2; i++) {
-    err = pthread_create(&tid[i], NULL, &startproc, NULL);
+  for (i = 0; i < NUM_THREADS; i++) {
+    err = pthread_create(&tid[i], NULL, &startproc, &pids[i]);
     if (err != 0)
       perror("pthread_create()");
     if (serialize_fork)
       pthread_join(tid[i], NULL);
   }
   if (!serialize_fork) {
-    for (i = 0; i < 2; i++)
+    for (i = 0; i < NUM_THREADS; i++)
       if (tid[i])
         pthread_join(tid[i], NULL);
   }
+  for (i = 0; i < NUM_THREADS; i++)
+    waitpid(pids[i], &err, 0);
+
   return 0;
 }