OSDN Git Service

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