@@ -379,12 +379,12 @@ fork01 fork01
fork03 fork03
fork04 fork04
fork05 fork05
-fork06 fork06
+fork06 fork_procs -n 1000
fork07 fork07
fork08 fork08
fork09 fork09
fork10 fork10
-fork11 fork11
+fork11 fork_procs -n 100
fork13 fork13
fork14 fork14
@@ -2,12 +2,11 @@
/fork03
/fork04
/fork05
-/fork06
/fork07
/fork08
/fork09
/fork10
-/fork11
/fork12
/fork13
/fork14
+/fork_procs
deleted file mode 100644
@@ -1,106 +0,0 @@
-/*
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * NAME
- * fork06.c
- *
- * DESCRIPTION
- * Test that a process can fork children a large number of
- * times in succession
- *
- * ALGORITHM
- * Attempt to fork a child that exits immediately
- * Repeat it many times(1000), counting the number of successes and
- * failures
- *
- * USAGE
- * fork06
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- * None
- */
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <stdio.h>
-#include "test.h"
-
-char *TCID = "fork06";
-int TST_TOTAL = 1;
-
-static void setup(void);
-static void cleanup(void);
-
-#define NUMFORKS 1000
-
-int main(int ac, char **av)
-{
- int i, pid, status, childpid, succeed, fail;
-
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
- succeed = 0;
- fail = 0;
-
- for (i = 0; i < NUMFORKS; i++) {
- pid = fork();
- if (pid == -1) {
- fail++;
- continue;
- }
-
- if (pid == 0)
- _exit(0);
-
- childpid = wait(&status);
- if (pid != childpid)
- tst_resm(TFAIL, "pid from wait %d", childpid);
- succeed++;
- }
-
- tst_resm(TINFO, "tries %d", i);
- tst_resm(TINFO, "successes %d", succeed);
- tst_resm(TINFO, "failures %d", fail);
-
- if ((wait(&status)) == -1)
- tst_resm(TINFO, "There were no children to wait for");
- else
- tst_resm(TINFO, "There were children left");
- }
-
- cleanup();
- tst_exit();
-}
-
-static void setup(void)
-{
- tst_sig(FORK, DEF_HANDLER, cleanup);
- TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
deleted file mode 100644
@@ -1,97 +0,0 @@
-/*
- * Copyright (c) International Business Machines Corp., 2001
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- *
- * NAME
- * fork11.c
- *
- * DESCRIPTION
- * Test that parent gets a pid from each child when doing wait
- *
- * ALGORITHM
- * Fork NUMFORKS children that do nothing.
- *
- * USAGE
- * fork11
- *
- * HISTORY
- * 07/2001 Ported by Wayne Boyer
- *
- * RESTRICTIONS
- * None
- */
-
-#include <sys/types.h>
-#include <sys/wait.h>
-#include <stdio.h>
-#include <errno.h>
-#include "test.h"
-
-char *TCID = "fork11";
-int TST_TOTAL = 1;
-
-static void setup(void);
-static void cleanup(void);
-
-#define NUMFORKS 100
-
-int main(int ac, char **av)
-{
- int i, pid, cpid, status;
- int fail = 0;
- int lc;
-
- tst_parse_opts(ac, av, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- tst_count = 0;
-
- for (i = 0; i < NUMFORKS; i++) {
- pid = fork();
- if (pid == 0)
- exit(0);
-
- if (pid > 0) { /* parent */
- cpid = wait(&status);
- if (cpid != pid)
- fail++;
- } else {
- fail++;
- break;
- }
- }
- if (fail)
- tst_resm(TFAIL, "fork failed %d times", fail);
- else
- tst_resm(TPASS, "fork test passed, %d processes", i);
- }
-
- cleanup();
- tst_exit();
-}
-
-static void setup(void)
-{
- tst_sig(FORK, DEF_HANDLER, cleanup);
- TEST_PAUSE;
-}
-
-static void cleanup(void)
-{
-}
new file mode 100644
@@ -0,0 +1,55 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2023 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
+ */
+
+/*\
+ *[Description]
+ *
+ * This test spawns multiple processes using fork() and it checks if wait()
+ * returns the right PID once they end up.
+ */
+
+#include <stdlib.h>
+#include "tst_test.h"
+
+static char *str_numforks;
+static int numforks = 1000;
+
+static void run(void)
+{
+ pid_t pid;
+ int status;
+ int counter = 0;
+
+ tst_res(TINFO, "Forking %d processes", numforks);
+
+ for (int i = 0; i < numforks; i++) {
+ pid = SAFE_FORK();
+ if (!pid)
+ exit(0);
+
+ if (SAFE_WAIT(&status) == pid)
+ counter++;
+ }
+
+ TST_EXP_EXPR(numforks == counter,
+ "%d processes ended successfully",
+ counter);
+}
+
+static void setup(void)
+{
+ if (tst_parse_int(str_numforks, &numforks, 1, INT_MAX))
+ tst_brk(TBROK, "wrong number of forks '%s'", str_numforks);
+}
+
+static struct tst_test test = {
+ .test_all = run,
+ .setup = setup,
+ .forks_child = 1,
+ .options = (struct tst_option[]) {
+ { "n:", &str_numforks, "Number of processes to spawn" },
+ {},
+ },
+};