OSDN Git Service

e71462443b3676849036df7b77b0f8c002fec369
[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,2001 by Erik Andersen <andersen@uclibc.org>
7  * Written by Erik Andersen <andersen@uclibc.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Library General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <signal.h>
29 #include <sys/wait.h>
30
31 #define GOT1    (1 << 1)
32 #define GOT2    (1 << 2)
33 #define GOT3    (1 << 3)
34
35 #ifdef __ARCH_USE_MMU__
36
37 void child_handler(int sig)
38 {
39         fprintf(stderr, "I got a SIGCHLD\n");
40 }
41
42 int main(void) 
43 {
44         pid_t pid1, pid2, pid3;
45         int status, result, wpid;
46
47         signal(SIGCHLD, child_handler);
48
49         if ((pid1 = fork()) == 0) {
50                 fprintf(stderr, "The child process sleeps 2 seconds...\n");
51                 sleep(4);
52                 fprintf(stderr, "Child exiting.\n");
53                 exit(-1);
54         }
55         if ((pid2 = fork()) == 0) {
56                 fprintf(stderr, "The child process sleeps 3 seconds...\n");
57                 sleep(3);
58                 fprintf(stderr, "Child exiting.\n");
59                 exit(-1);
60         }
61         if ((pid3 = fork()) == 0) {
62                 fprintf(stderr, "The child process sleeps 4 seconds...\n");
63                 sleep(2);
64                 fprintf(stderr, "Child exiting.\n");
65                 exit(-1);
66         }
67
68         fprintf(stderr, "Parent: waiting for the child to die.\n");
69         status = 0;
70         while (1) {
71                 wpid = waitpid(pid1, &result, WNOHANG);
72                 if (wpid == pid1)
73                         status |= GOT1;
74
75                 wpid = waitpid(pid2, &result, WNOHANG);
76                 if (wpid == pid2)
77                         status |= GOT2;
78
79                 wpid = waitpid(pid3, &result, WNOHANG);
80                 if (wpid == pid3)
81                         status |= GOT3;
82
83                 if (status == (GOT1 | GOT2 | GOT3))
84                         break;
85         }
86
87         fprintf(stderr, "Child process exited.\nGoodbye.\n");
88         return EXIT_SUCCESS;
89 }
90
91 #else
92
93 int main(void)
94 {
95         printf("Skipping test on non-mmu host!\n");
96         return EXIT_SUCCESS;
97 }
98
99 #endif
100
101 /*
102 Local Variables:
103 c-file-style: "linux"
104 c-basic-offset: 4
105 tab-width: 4
106 End:
107 */