plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * 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 Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 17 | * |
| 18 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | * NAME |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 20 | * fork11.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * |
| 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 Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 29 | * fork11 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * |
| 31 | * HISTORY |
| 32 | * 07/2001 Ported by Wayne Boyer |
| 33 | * |
| 34 | * RESTRICTIONS |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 35 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 36 | */ |
| 37 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/wait.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
| 41 | #include <errno.h> |
| 42 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 44 | char *TCID = "fork11"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 47 | static void setup(void); |
| 48 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
| 50 | #define NUMFORKS 100 |
| 51 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 52 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 53 | { |
| 54 | int i, pid, cpid, status; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 55 | int fail = 0; |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 56 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 57 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 59 | msg = parse_opts(ac, av, NULL, NULL); |
| 60 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 61 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | setup(); |
| 64 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 66 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 68 | for (i = 0; i < NUMFORKS; i++) { |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 69 | pid = fork(); |
| 70 | if (pid == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | exit(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 72 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 73 | if (pid > 0) { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | cpid = wait(&status); |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 75 | if (cpid != pid) |
plars | 2e129e1 | 2004-10-01 19:15:58 +0000 | [diff] [blame] | 76 | fail++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | } else { |
plars | 2e129e1 | 2004-10-01 19:15:58 +0000 | [diff] [blame] | 78 | fail++; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | } |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 82 | if (fail) |
plars | 2e129e1 | 2004-10-01 19:15:58 +0000 | [diff] [blame] | 83 | tst_resm(TFAIL, "fork failed %d times", fail); |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 84 | else |
plars | 2e129e1 | 2004-10-01 19:15:58 +0000 | [diff] [blame] | 85 | tst_resm(TPASS, "fork test passed, %d processes", i); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 87 | |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 88 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 89 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 92 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | TEST_PAUSE; |
| 96 | } |
| 97 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 98 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | { |
Wanlong Gao | 4492fc4 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 100 | } |