]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
drd: Add fork test program
authorBart Van Assche <bvanassche@acm.org>
Sat, 1 Dec 2018 04:49:27 +0000 (20:49 -0800)
committerBart Van Assche <bvanassche@acm.org>
Sat, 1 Dec 2018 18:45:51 +0000 (10:45 -0800)
.gitignore
drd/tests/Makefile.am
drd/tests/fork.c [new file with mode: 0644]

index e88e3b3a4313a7b2e9cd37a702dd6cd54a6e0ac7..a17d63132be59d33fe8e9930a832688084c13629 100644 (file)
 /drd/tests/dlopen_lib.so
 /drd/tests/dlopen_main
 /drd/tests/drd_bitmap_test
+/drd/tests/fork
 /drd/tests/fp_race
 /drd/tests/free_is_write
 /drd/tests/hg01_all_ok
index d09ca5f89a22dfe70ee942cd893ec47afb8c1a89..99dfefa30c6854aad82b2618c8a9fed440213fc2 100644 (file)
@@ -370,6 +370,7 @@ check_PROGRAMS =      \
   concurrent_close    \
   dlopen_main         \
   dlopen_lib.so       \
+  fork                \
   fp_race             \
   free_is_write              \
   hold_lock           \
diff --git a/drd/tests/fork.c b/drd/tests/fork.c
new file mode 100644 (file)
index 0000000..07aaf73
--- /dev/null
@@ -0,0 +1,45 @@
+#include <stdio.h>
+#include <pthread.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+static pthread_t tid[2];
+
+static void *startproc(void *arg)
+{
+  pid_t pid;
+  char *argv[] = { "/bin/ls", "/bin/ls", NULL };
+
+  if ((pid = fork()) == -1) {
+    perror("fork error");
+  } else if (pid == 0) {
+    dup2(2, 1);    // redirect stdout to stderr
+    execv(argv[0], argv); // child
+  }
+
+  return NULL;
+}
+
+int main(int argc, char **argv)
+{
+  // No arguments means serialize the fork() calls. One argument means perform
+  // both fork() calls concurrently.
+  int serialize_fork = argc == 1;
+  int i = 0;
+  int err;
+
+  for (i = 0; i < 2; i++) {
+    err = pthread_create(&tid[i], NULL, &startproc, NULL);
+    if (err != 0)
+      perror("pthread_create()");
+    if (serialize_fork)
+      pthread_join(tid[i], NULL);
+  }
+  if (!serialize_fork) {
+    for (i = 0; i < 2; i++)
+      if (tid[i])
+        pthread_join(tid[i], NULL);
+  }
+  return 0;
+}