xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fork/fork03.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  * Copyright (c) Linux Test Project, 2003-2023
5  * Author: 2001 Ported by Wayne Boyer
6  */
7 
8 /*\
9  *[Description]
10  *
11  * Check that child process can use a large text space and do a large number
12  * of operations. In this situation, check for pid == 0 in child and check
13  * for pid > 0 in parent after wait.
14  */
15 
16 #include <unistd.h>
17 #include <sys/wait.h>
18 #include <stdlib.h>
19 #include "tst_test.h"
20 
verify_fork(void)21 static void verify_fork(void)
22 {
23 	float fl1, fl2;
24 	int pid1, pid2, status, i;
25 
26 	pid1 = SAFE_FORK();
27 	if (!pid1) {
28 		/* child uses some cpu time slices */
29 		for (i = 1; i < 32767; i++) {
30 			fl1 = 0.000001;
31 			fl1 = fl2 = 0.000001;
32 			fl1 = fl1 * 10.0;
33 			fl2 = fl1 / 1.232323;
34 			fl1 = fl2 - fl2;
35 			fl1 = fl2;
36 		}
37 		exit(!!pid1);
38 	}
39 
40 	tst_res(TINFO, "process id in parent of child from fork: %d", pid1);
41 	pid2 = SAFE_WAIT(&status);
42 
43 	if (pid1 != pid2) {
44 		tst_res(TFAIL, "pids don't match: %d vs %d", pid1, pid2);
45 		return;
46 	}
47 
48 	if ((status >> 8) != 0) {
49 		tst_res(TFAIL, "child exited with failure");
50 		return;
51 	}
52 
53 	tst_res(TPASS, "test PASSED");
54 }
55 
56 static struct tst_test test = {
57 	.test_all = verify_fork,
58 	.forks_child = 1,
59 };
60