10 static pthread_t tid
[NUM_THREADS
];
11 static pid_t pids
[NUM_THREADS
];
13 static void *startproc(void *arg
)
16 char *argv
[] = { "/bin/ls", "/bin/ls", NULL
};
18 if ((pid
= fork()) == -1) {
20 } else if (pid
== 0) {
21 dup2(2, 1); // redirect stdout to stderr
22 execv(argv
[0], argv
); // child
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;
38 for (i
= 0; i
< NUM_THREADS
; i
++) {
39 err
= pthread_create(&tid
[i
], NULL
, &startproc
, &pids
[i
]);
41 perror("pthread_create()");
43 pthread_join(tid
[i
], NULL
);
45 if (!serialize_fork
) {
46 for (i
= 0; i
< NUM_THREADS
; i
++)
48 pthread_join(tid
[i
], NULL
);
50 for (i
= 0; i
< NUM_THREADS
; i
++)
51 waitpid(pids
[i
], &err
, 0);