OSDN Git Service

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