OSDN Git Service

sync with glibc
[uclinux-h8/uclibc-ng.git] / test / unistd / clone.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * clone 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 <sched.h>
30 #include <sys/wait.h>
31 #include "clone_cruft.h"
32
33 #define GOT1     (1 << 1)
34 #define GOT2     (1 << 2)
35 #define GOT3     (1 << 3)
36 #define ALLGOT   (GOT1|GOT2|GOT3)
37
38 void child_handler(int sig)
39 {
40         printf("I got a SIGCHLD\n");
41 }
42
43 int clone_main(void *arg)
44 {
45         unsigned long input = (unsigned long)arg;
46         int secs = (input / 10) * 4;
47         printf("Clone got %lu, sleeping for %i secs\n", input, secs);
48         sleep(secs);
49         return input + 20;
50 }
51
52 int main(void) 
53 {
54         int clone1, clone2, clone3;
55         char clone1_stack[8192], clone2_stack[8192], clone3_stack[8192];
56         int status, nostatus, result, wpid;
57
58         signal(SIGCHLD, child_handler);
59
60         if ((clone1 = do_clone(clone_main, clone1_stack, 0, (void*)11)) == -1) {
61                 perror("Clone 1 failed");
62                 exit(-1);
63         }
64         if ((clone2 = do_clone(clone_main, clone2_stack, 0, (void*)22)) == -1) {
65                 perror("Clone 2 failed");
66                 exit(-2);
67         }
68         if ((clone3 = do_clone(clone_main, clone3_stack, 0, (void*)33)) == -1) {
69                 perror("Clone 3 failed");
70                 exit(-3);
71         }
72
73         sleep(1);
74         printf("Parent: waiting for the clones to die.\n");
75         nostatus = status = 0;
76         while (1) {
77                 if ((wpid = waitpid(clone1, &result, WNOHANG|__WCLONE)) == -1)
78                         nostatus |= GOT1;
79                 if (wpid == clone1) {
80                         status |= GOT1;
81                         printf("Clone1 gave back %i\n", WEXITSTATUS(result));
82                 }
83
84                 if ((wpid = waitpid(clone2, &result, WNOHANG|__WCLONE)) == -1)
85                         nostatus |= GOT2;
86                 if (wpid == clone2) {
87                         status |= GOT2;
88                         printf("Clone2 gave back %i\n", WEXITSTATUS(result));
89                 }
90
91                 if ((wpid = waitpid(clone3, &result, WNOHANG|__WCLONE)) == -1)
92                         nostatus |= GOT3;
93                 if (wpid == clone3) {
94                         status |= GOT3;
95                         printf("Clone3 gave back %i\n", WEXITSTATUS(result));
96                 }
97
98                 if (status == ALLGOT || nostatus == ALLGOT)
99                         break;
100         }
101
102         if (status == ALLGOT) {
103                 printf("Clones exited.\nGoodbye.\n");
104                 return EXIT_SUCCESS;
105         } else {
106                 perror("Waiting for clones failed");
107                 return EXIT_FAILURE;
108         }
109 }
110
111 /*
112 Local Variables:
113 c-file-style: "linux"
114 c-basic-offset: 4
115 tab-width: 4
116 End:
117 */