blob: 5d938387684bcece855b836a0f218b78732b72d3 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * Copyright (c) International Business Machines Corp., 2001
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
Wanlong Gao4548c6c2012-10-19 18:03:36 +080016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Wanlong Gao4492fc42012-09-18 17:14:01 +080017 *
18 *
plars865695b2001-08-27 22:15:12 +000019 * NAME
Wanlong Gao4492fc42012-09-18 17:14:01 +080020 * fork11.c
plars865695b2001-08-27 22:15:12 +000021 *
22 * DESCRIPTION
23 * Test that parent gets a pid from each child when doing wait
24 *
25 * ALGORITHM
26 * Fork NUMFORKS children that do nothing.
27 *
28 * USAGE
Wanlong Gao4492fc42012-09-18 17:14:01 +080029 * fork11
plars865695b2001-08-27 22:15:12 +000030 *
31 * HISTORY
32 * 07/2001 Ported by Wayne Boyer
33 *
34 * RESTRICTIONS
Wanlong Gao4492fc42012-09-18 17:14:01 +080035 * None
plars865695b2001-08-27 22:15:12 +000036 */
37
plars74948ad2002-11-14 16:16:14 +000038#include <sys/types.h>
39#include <sys/wait.h>
plars865695b2001-08-27 22:15:12 +000040#include <stdio.h>
41#include <errno.h>
42#include "test.h"
plars865695b2001-08-27 22:15:12 +000043
nstrazfa31d552002-05-14 16:50:06 +000044char *TCID = "fork11";
plars865695b2001-08-27 22:15:12 +000045int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000046
Wanlong Gao4492fc42012-09-18 17:14:01 +080047static void setup(void);
48static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000049
50#define NUMFORKS 100
51
plars74948ad2002-11-14 16:16:14 +000052int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000053{
54 int i, pid, cpid, status;
subrata_modak56207ce2009-03-23 13:35:39 +000055 int fail = 0;
Wanlong Gao4492fc42012-09-18 17:14:01 +080056 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020057 const char *msg;
plars865695b2001-08-27 22:15:12 +000058
Wanlong Gao4492fc42012-09-18 17:14:01 +080059 msg = parse_opts(ac, av, NULL, NULL);
60 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080061 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000062
plars865695b2001-08-27 22:15:12 +000063 setup();
64
plars865695b2001-08-27 22:15:12 +000065 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080066 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000067
subrata_modak56207ce2009-03-23 13:35:39 +000068 for (i = 0; i < NUMFORKS; i++) {
Wanlong Gao4492fc42012-09-18 17:14:01 +080069 pid = fork();
70 if (pid == 0)
plars865695b2001-08-27 22:15:12 +000071 exit(0);
plars865695b2001-08-27 22:15:12 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 if (pid > 0) { /* parent */
plars865695b2001-08-27 22:15:12 +000074 cpid = wait(&status);
Wanlong Gao4492fc42012-09-18 17:14:01 +080075 if (cpid != pid)
plars2e129e12004-10-01 19:15:58 +000076 fail++;
plars865695b2001-08-27 22:15:12 +000077 } else {
plars2e129e12004-10-01 19:15:58 +000078 fail++;
plars865695b2001-08-27 22:15:12 +000079 break;
80 }
81 }
Wanlong Gao4492fc42012-09-18 17:14:01 +080082 if (fail)
plars2e129e12004-10-01 19:15:58 +000083 tst_resm(TFAIL, "fork failed %d times", fail);
Wanlong Gao4492fc42012-09-18 17:14:01 +080084 else
plars2e129e12004-10-01 19:15:58 +000085 tst_resm(TPASS, "fork test passed, %d processes", i);
plars865695b2001-08-27 22:15:12 +000086 }
plars865695b2001-08-27 22:15:12 +000087
Wanlong Gao4492fc42012-09-18 17:14:01 +080088 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -080089 tst_exit();
plars865695b2001-08-27 22:15:12 +000090}
91
Mike Frysingerc57fba52014-04-09 18:56:30 -040092static void setup(void)
plars865695b2001-08-27 22:15:12 +000093{
plars865695b2001-08-27 22:15:12 +000094 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +000095 TEST_PAUSE;
96}
97
Mike Frysingerc57fba52014-04-09 18:56:30 -040098static void cleanup(void)
plars865695b2001-08-27 22:15:12 +000099{
Wanlong Gao4492fc42012-09-18 17:14:01 +0800100}