xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/fork/fork_procs.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2023 SUSE LLC Andrea Cervesato <[email protected]>
4  */
5 
6 /*\
7  *[Description]
8  *
9  * This test spawns multiple processes using fork() and it checks if wait()
10  * returns the right PID once they end up.
11  */
12 
13 #include <stdlib.h>
14 #include "tst_test.h"
15 
16 static char *str_numforks;
17 static int numforks = 1000;
18 
run(void)19 static void run(void)
20 {
21 	pid_t pid;
22 	int status;
23 	int counter = 0;
24 
25 	tst_res(TINFO, "Forking %d processes", numforks);
26 
27 	for (int i = 0; i < numforks; i++) {
28 		pid = SAFE_FORK();
29 		if (!pid)
30 			exit(0);
31 
32 		if (SAFE_WAIT(&status) == pid)
33 			counter++;
34 	}
35 
36 	TST_EXP_EXPR(numforks == counter,
37 		"%d processes ended successfully",
38 		counter);
39 }
40 
setup(void)41 static void setup(void)
42 {
43 	if (tst_parse_int(str_numforks, &numforks, 1, INT_MAX))
44 		tst_brk(TBROK, "wrong number of forks '%s'", str_numforks);
45 }
46 
47 static struct tst_test test = {
48 	.test_all = run,
49 	.setup = setup,
50 	.forks_child = 1,
51 	.options = (struct tst_option[]) {
52 		{ "n:", &str_numforks, "Number of processes to spawn" },
53 		{},
54 	},
55 };
56