none/tests/bug234814.c: sa_handler take an int as argument
[valgrind.git] / drd / tests / fork.c
blob0f934ca3c7dc2ee5e03365b6acd00a081a140aae
1 #include <stdio.h>
2 #include <pthread.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <sys/wait.h>
8 #define NUM_THREADS 2
10 static pthread_t tid[NUM_THREADS];
11 static pid_t pids[NUM_THREADS];
13 static void *startproc(void *arg)
15 pid_t pid;
16 char *argv[] = { "/bin/ls", "/bin/ls", NULL };
18 if ((pid = fork()) == -1) {
19 perror("fork error");
20 } else if (pid == 0) {
21 dup2(2, 1); // redirect stdout to stderr
22 execv(argv[0], argv); // child
23 } else {
24 *((pid_t*)arg) = pid;
27 return NULL;
30 int main(int argc, char **argv)
32 // No arguments means serialize the fork() calls. One argument means perform
33 // both fork() calls concurrently.
34 int serialize_fork = argc == 1;
35 int i = 0;
36 int err;
38 for (i = 0; i < NUM_THREADS; i++) {
39 err = pthread_create(&tid[i], NULL, &startproc, &pids[i]);
40 if (err != 0)
41 perror("pthread_create()");
42 if (serialize_fork)
43 pthread_join(tid[i], NULL);
45 if (!serialize_fork) {
46 for (i = 0; i < NUM_THREADS; i++)
47 if (tid[i])
48 pthread_join(tid[i], NULL);
50 for (i = 0; i < NUM_THREADS; i++)
51 waitpid(pids[i], &err, 0);
53 return 0;