]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/unistd/fork.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / unistd / fork.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * fork test for uClibc
4  * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
5  *
6  * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
7  */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <signal.h>
13 #include <sys/wait.h>
14
15 #define GOT1    (1 << 1)
16 #define GOT2    (1 << 2)
17 #define GOT3    (1 << 3)
18
19 #ifdef __ARCH_USE_MMU__
20
21 static void child_handler(int sig)
22 {
23         fprintf(stderr, "I got a SIGCHLD\n");
24 }
25
26 int main(void)
27 {
28         pid_t pid1, pid2, pid3;
29         int status, result, wpid;
30
31         signal(SIGCHLD, child_handler);
32
33         if ((pid1 = fork()) == 0) {
34                 fprintf(stderr, "The child process sleeps 2 seconds...\n");
35                 sleep(4);
36                 fprintf(stderr, "Child exiting.\n");
37                 exit(-1);
38         }
39         if ((pid2 = fork()) == 0) {
40                 fprintf(stderr, "The child process sleeps 3 seconds...\n");
41                 sleep(3);
42                 fprintf(stderr, "Child exiting.\n");
43                 exit(-1);
44         }
45         if ((pid3 = fork()) == 0) {
46                 fprintf(stderr, "The child process sleeps 4 seconds...\n");
47                 sleep(2);
48                 fprintf(stderr, "Child exiting.\n");
49                 exit(-1);
50         }
51
52         fprintf(stderr, "Parent: waiting for the child to die.\n");
53         status = 0;
54         while (1) {
55                 wpid = waitpid(pid1, &result, WNOHANG);
56                 if (wpid == pid1)
57                         status |= GOT1;
58
59                 wpid = waitpid(pid2, &result, WNOHANG);
60                 if (wpid == pid2)
61                         status |= GOT2;
62
63                 wpid = waitpid(pid3, &result, WNOHANG);
64                 if (wpid == pid3)
65                         status |= GOT3;
66
67                 if (status == (GOT1 | GOT2 | GOT3))
68                         break;
69         }
70
71         fprintf(stderr, "Child process exited.\nGoodbye.\n");
72         return EXIT_SUCCESS;
73 }
74
75 #else
76
77 int main(void)
78 {
79         printf("Skipping test on non-mmu host!\n");
80         return EXIT_SUCCESS;
81 }
82
83 #endif
84
85 /*
86 Local Variables:
87 c-file-style: "linux"
88 c-basic-offset: 4
89 tab-width: 4
90 End:
91 */