Home | History | Annotate | Download | only in fork
      1 /*
      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
     16  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
     17  *
     18  * NAME
     19  *	fork06.c
     20  *
     21  * DESCRIPTION
     22  *	Test that a process can fork children a large number of
     23  *	times in succession
     24  *
     25  * ALGORITHM
     26  *	Attempt to fork a child that exits immediately
     27  *	Repeat it many times(1000), counting the number of successes and
     28  *	failures
     29  *
     30  * USAGE
     31  *	fork06
     32  *
     33  * HISTORY
     34  *	07/2001 Ported by Wayne Boyer
     35  *
     36  * RESTRICTIONS
     37  *	None
     38  */
     39 
     40 #include <sys/types.h>
     41 #include <sys/wait.h>
     42 #include <stdio.h>
     43 #include "test.h"
     44 
     45 char *TCID = "fork06";
     46 int TST_TOTAL = 1;
     47 
     48 static void setup(void);
     49 static void cleanup(void);
     50 
     51 #define NUMFORKS 1000
     52 
     53 int main(int ac, char **av)
     54 {
     55 	int i, pid, status, childpid, succeed, fail;
     56 
     57 	int lc;
     58 
     59 	tst_parse_opts(ac, av, NULL, NULL);
     60 
     61 	setup();
     62 
     63 	for (lc = 0; TEST_LOOPING(lc); lc++) {
     64 		tst_count = 0;
     65 		succeed = 0;
     66 		fail = 0;
     67 
     68 		for (i = 0; i < NUMFORKS; i++) {
     69 			pid = fork();
     70 			if (pid == -1) {
     71 				fail++;
     72 				continue;
     73 			}
     74 
     75 			if (pid == 0)
     76 				_exit(0);
     77 
     78 			childpid = wait(&status);
     79 			if (pid != childpid)
     80 				tst_resm(TFAIL, "pid from wait %d", childpid);
     81 			succeed++;
     82 		}
     83 
     84 		tst_resm(TINFO, "tries %d", i);
     85 		tst_resm(TINFO, "successes %d", succeed);
     86 		tst_resm(TINFO, "failures %d", fail);
     87 
     88 		if ((wait(&status)) == -1)
     89 			tst_resm(TINFO, "There were no children to wait for");
     90 		else
     91 			tst_resm(TINFO, "There were children left");
     92 	}
     93 
     94 	cleanup();
     95 	tst_exit();
     96 }
     97 
     98 static void setup(void)
     99 {
    100 	tst_sig(FORK, DEF_HANDLER, cleanup);
    101 	TEST_PAUSE;
    102 }
    103 
    104 static void cleanup(void)
    105 {
    106 }
    107