3 * Copyright (C) 2001 Simone Piccardi
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 /****************************************************************
22 * Program to test process creation
24 * Author: Simone Piccardi
27 * Usage: forktest -h give all info's
29 * $Id: ForkTest.c,v 1.6 2001/09/21 17:10:51 piccardi Exp $
31 ****************************************************************/
33 * Include needed headers
35 #include <errno.h> /* error definitions and routines */
36 #include <stdlib.h> /* C standard library */
37 #include <unistd.h> /* unix standard library */
38 #include <stdio.h> /* standard I/O library */
39 #include <string.h> /* string functions */
41 /* Help printing routine */
44 int main(int argc, char *argv[])
47 * Variables definition
55 * Input section: decode command line parameters
58 opterr = 0; /* don't want writing to stderr */
59 while ( (i = getopt(argc, argv, "hp:c:e:")) != -1) {
64 case 'h': /* help option */
65 printf("Wrong -h option use\n");
69 case 'c': /* take wait time for childen */
70 wait_child = strtol(optarg, NULL, 10); /* convert input */
72 case 'p': /* take wait time for childen */
73 wait_parent = strtol(optarg, NULL, 10); /* convert input */
75 case 'e': /* take wait before parent exit */
76 wait_end = strtol(optarg, NULL, 10); /* convert input */
78 case '?': /* unrecognized options */
79 printf("Unrecognized options -%c\n",optopt);
81 default: /* should not reached */
85 /* ***********************************************************
87 * Options processing completed
91 * ***********************************************************/
92 /* There must be remaing parameters */
96 nchild = atoi(argv[optind]);
97 printf("Process %d: forking %d child\n", getpid(), nchild);
98 /* loop to fork children */
99 for (i=0; i<nchild; i++) {
100 if ( (pid = fork()) < 0) {
102 printf("Error on %d child creation, %s\n", i+1, strerror(errno));
105 if (pid == 0) { /* child */
106 printf("Child %d successfully executing\n", ++i);
107 if (wait_child) sleep(wait_child);
108 printf("Child %d, parent %d, exiting\n", i, getppid());
110 } else { /* parent */
111 printf("Spawned %d child, pid %d \n", i+1, pid);
112 if (wait_parent) sleep(wait_parent);
113 printf("Go to next child \n");
117 if (wait_end) sleep(wait_end);
121 * routine to print usage info and exit
124 printf("Program forktest: fork a given number of child \n");
126 printf(" forktest [-h] [-p sec] [-c sec] [-e sec] child to fork \n");
127 printf(" -h print this help\n");
128 printf(" -p sec wait sec seconds before next fork\n");
129 printf(" -c sec wait sec seconds before child termination\n");
130 printf(" -e sec wait sec seconds before parent return\n");