1 /****************************************************************
4 * Program to test process creation
6 * Author: Simone Piccardi
9 * Usage: forktest -h give all info's
11 * $Id: ForkTest.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $
13 ****************************************************************/
15 * Include needed headers
17 #include <errno.h> /* error definitions and routines */
18 #include <stdlib.h> /* C standard library */
19 #include <unistd.h> /* unix standard library */
20 #include <stdio.h> /* standard I/O library */
21 #include <string.h> /* string functions */
23 /* Help printing routine */
26 int main(int argc, char *argv[])
29 * Variables definition
35 * Input section: decode command line parameters
38 opterr = 0; /* don't want writing to stderr */
39 while ( (i = getopt(argc, argv, "h")) != -1) {
45 printf("Wrong -h option use\n");
49 case '?': /* unrecognized options */
50 printf("Unrecognized options -%c\n",optopt);
52 default: /* should not reached */
56 /* ***********************************************************
58 * Options processing completed
62 * ***********************************************************/
63 /* There must be remaing parameters */
67 nchild = atoi(argv[optind]);
68 printf("Test for forking %d child\n", nchild);
69 /* loop to fork children */
70 for (i=0; i<nchild; i++) {
71 if ( (pid = fork()) < 0) {
72 printf("Error on %d child creation, %s\n", i, strerror(errno));
74 if (pid == 0) { /* child */
75 printf("Child %d successfully executing\n", i++);
77 printf("Child %d exiting\n", i);
80 printf("Spawned %d child, pid %d \n", i, pid);
87 * routine to print usage info and exit
90 printf("Program forktest: fork a given number of child \n");
92 printf(" forktest [-h] child to fork \n");
93 printf(" -h print this help\n");