1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 John George - Ported
4*49cdfc7eSAndroid Build Coastguard Worker * 04/2002 wjhuie sigset cleanups
5*49cdfc7eSAndroid Build Coastguard Worker *
6*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
7*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
8*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
9*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
10*49cdfc7eSAndroid Build Coastguard Worker *
11*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
12*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
13*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
15*49cdfc7eSAndroid Build Coastguard Worker *
16*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
17*49cdfc7eSAndroid Build Coastguard Worker * along with this program.
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * Tests to see if pids returned from fork and waitpid are same
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Fork 8 kids, 2 will immediately exit, 2 will sleep, 2 will be compute bound
24*49cdfc7eSAndroid Build Coastguard Worker * and 2 will fork/reap a child for 50 times.
25*49cdfc7eSAndroid Build Coastguard Worker */
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker #include "waitpid_common.h"
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker static void do_compute(void);
30*49cdfc7eSAndroid Build Coastguard Worker static void do_fork(void);
31*49cdfc7eSAndroid Build Coastguard Worker static void do_sleep(void);
32*49cdfc7eSAndroid Build Coastguard Worker
do_child_1(void)33*49cdfc7eSAndroid Build Coastguard Worker static void do_child_1(void)
34*49cdfc7eSAndroid Build Coastguard Worker {
35*49cdfc7eSAndroid Build Coastguard Worker pid_t pid;
36*49cdfc7eSAndroid Build Coastguard Worker int i;
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < MAXKIDS; i++) {
39*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
40*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
41*49cdfc7eSAndroid Build Coastguard Worker if (i == 0 || i == 1)
42*49cdfc7eSAndroid Build Coastguard Worker do_exit(0);
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker if (i == 2 || i == 3)
45*49cdfc7eSAndroid Build Coastguard Worker do_compute();
46*49cdfc7eSAndroid Build Coastguard Worker
47*49cdfc7eSAndroid Build Coastguard Worker if (i == 4 || i == 5)
48*49cdfc7eSAndroid Build Coastguard Worker do_fork();
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker if (i == 6 || i == 7)
51*49cdfc7eSAndroid Build Coastguard Worker do_sleep();
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker
54*49cdfc7eSAndroid Build Coastguard Worker fork_kid_pid[i] = pid;
55*49cdfc7eSAndroid Build Coastguard Worker }
56*49cdfc7eSAndroid Build Coastguard Worker
57*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAKE2(0, MAXKIDS);
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker if (TST_TRACE(reap_children(0, 0, fork_kid_pid, MAXKIDS)))
60*49cdfc7eSAndroid Build Coastguard Worker return;
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Test PASSED");
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
do_compute(void)65*49cdfc7eSAndroid Build Coastguard Worker static void do_compute(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker int i;
68*49cdfc7eSAndroid Build Coastguard Worker
69*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
70*49cdfc7eSAndroid Build Coastguard Worker
71*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
72*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
73*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
74*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
75*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
76*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
77*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
78*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
79*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
80*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 100000; i++) ;
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker exit(3);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
do_fork(void)85*49cdfc7eSAndroid Build Coastguard Worker static void do_fork(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker pid_t fork_pid;
88*49cdfc7eSAndroid Build Coastguard Worker int i;
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
91*49cdfc7eSAndroid Build Coastguard Worker
92*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < 50; i++) {
93*49cdfc7eSAndroid Build Coastguard Worker fork_pid = SAFE_FORK();
94*49cdfc7eSAndroid Build Coastguard Worker if (fork_pid == 0)
95*49cdfc7eSAndroid Build Coastguard Worker exit(3);
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker if (TST_TRACE(reap_children(fork_pid, 0, &fork_pid, 1)))
98*49cdfc7eSAndroid Build Coastguard Worker break;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker exit(3);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
do_sleep(void)104*49cdfc7eSAndroid Build Coastguard Worker static void do_sleep(void)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker TST_CHECKPOINT_WAIT(0);
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
109*49cdfc7eSAndroid Build Coastguard Worker sleep(1);
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker exit(3);
112*49cdfc7eSAndroid Build Coastguard Worker }
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
115*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
116*49cdfc7eSAndroid Build Coastguard Worker .needs_checkpoints = 1,
117*49cdfc7eSAndroid Build Coastguard Worker .setup = waitpid_setup,
118*49cdfc7eSAndroid Build Coastguard Worker .cleanup = waitpid_cleanup,
119*49cdfc7eSAndroid Build Coastguard Worker .test_all = waitpid_test,
120*49cdfc7eSAndroid Build Coastguard Worker };
121