From 71be91d2ddf065b86a5cd9d4f3bfc3eca3c87a4b Mon Sep 17 00:00:00 2001 From: Petar Jovanovic Date: Wed, 12 Dec 2018 17:45:34 +0000 Subject: [PATCH] make outputs of drd/tests/fork* deterministic 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 | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/drd/tests/fork.c b/drd/tests/fork.c index 07aaf73f74..0f934ca3c7 100644 --- a/drd/tests/fork.c +++ b/drd/tests/fork.c @@ -3,8 +3,12 @@ #include #include #include +#include -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; } -- 2.47.3