drd: Add fork test program
[valgrind.git] / drd / tests / fork.c
blob07aaf73f74a896fe989ad16787e521379022dec8
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
7 static pthread_t tid[2];
9 static void *startproc(void *arg)
11 pid_t pid;
12 char *argv[] = { "/bin/ls", "/bin/ls", NULL };
14 if ((pid = fork()) == -1) {
15 perror("fork error");
16 } else if (pid == 0) {
17 dup2(2, 1); // redirect stdout to stderr
18 execv(argv[0], argv); // child
21 return NULL;
24 int main(int argc, char **argv)
26 // No arguments means serialize the fork() calls. One argument means perform
27 // both fork() calls concurrently.
28 int serialize_fork = argc == 1;
29 int i = 0;
30 int err;
32 for (i = 0; i < 2; i++) {
33 err = pthread_create(&tid[i], NULL, &startproc, NULL);
34 if (err != 0)
35 perror("pthread_create()");
36 if (serialize_fork)
37 pthread_join(tid[i], NULL);
39 if (!serialize_fork) {
40 for (i = 0; i < 2; i++)
41 if (tid[i])
42 pthread_join(tid[i], NULL);
44 return 0;