OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [uClibc/] [test/] [unistd/] [fork.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1325 phoenix
/* 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
void child_handler(int sig)
36
{
37
        fprintf(stderr, "I got a SIGCHLD\n");
38
}
39
 
40
int main(void)
41
{
42
        pid_t pid1, pid2, pid3;
43
        int status, result, wpid;
44
 
45
        signal(SIGCHLD, child_handler);
46
 
47
        if ((pid1 = fork()) == 0) {
48
                fprintf(stderr, "The child process sleeps 2 seconds...\n");
49
                sleep(4);
50
                fprintf(stderr, "Child exiting.\n");
51
                exit(-1);
52
        }
53
        if ((pid2 = fork()) == 0) {
54
                fprintf(stderr, "The child process sleeps 3 seconds...\n");
55
                sleep(3);
56
                fprintf(stderr, "Child exiting.\n");
57
                exit(-1);
58
        }
59
        if ((pid3 = fork()) == 0) {
60
                fprintf(stderr, "The child process sleeps 4 seconds...\n");
61
                sleep(2);
62
                fprintf(stderr, "Child exiting.\n");
63
                exit(-1);
64
        }
65
 
66
        fprintf(stderr, "Parent: waiting for the child to die.\n");
67
        status = 0;
68
        while (1) {
69
                wpid = waitpid(pid1, &result, WNOHANG);
70
                if (wpid == pid1)
71
                        status |= GOT1;
72
 
73
                wpid = waitpid(pid2, &result, WNOHANG);
74
                if (wpid == pid2)
75
                        status |= GOT2;
76
 
77
                wpid = waitpid(pid3, &result, WNOHANG);
78
                if (wpid == pid3)
79
                        status |= GOT3;
80
 
81
                if (status == (GOT1 | GOT2 | GOT3))
82
                        break;
83
        }
84
 
85
        fprintf(stderr, "Child process exited.\nGoodbye.\n");
86
        return EXIT_SUCCESS;
87
}
88
 
89
/*
90
Local Variables:
91
c-file-style: "linux"
92
c-basic-offset: 4
93
tab-width: 4
94
End:
95
*/

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.