Aggiunte minimali
[gapil.git] / sources / ForkTest.c
1 /* ForkTest.c
2  * 
3  * Copyright (C) 2001 Simone Piccardi
4  * 
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.
9  * 
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.
14  * 
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.
18  */
19 /****************************************************************
20  *
21  * Program ForkTest.c: 
22  * Program to test process creation
23  *
24  * Author: Simone Piccardi
25  * Sep. 2001
26  *
27  * Usage: forktest -h give all info's
28  *
29  * $Id: ForkTest.c,v 1.3 2001/09/11 21:01:09 piccardi Exp $
30  *
31  ****************************************************************/
32 /* 
33  * Include needed headers
34  */
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 */
40
41 /* Help printing routine */
42 void usage(void);
43
44 int main(int argc, char *argv[])
45 {
46 /* 
47  * Variables definition  
48  */
49     int i;
50     int nchild;
51     pid_t pid;
52     /*
53      * Input section: decode command line parameters 
54      * Use getopt function
55      */
56     opterr = 0;  /* don't want writing to stderr */
57     while ( (i = getopt(argc, argv, "h")) != -1) {
58         switch (i) {
59         /* 
60          * Handling options 
61          */ 
62         case 'h':  
63             printf("Wrong -h option use\n");
64             usage();
65             return(0);
66             break;
67         case '?':   /* unrecognized options */
68             printf("Unrecognized options -%c\n",optopt);
69             usage();
70         default:    /* should not reached */
71             usage();
72         }
73     }
74     /* ***********************************************************
75      * 
76      *           Options processing completed
77      *
78      *                Main code beginning
79      * 
80      * ***********************************************************/
81     /* There must be remaing parameters */
82     if (optind == argc) {
83         usage();
84     }
85     nchild = atoi(argv[optind]);
86     printf("Test for forking %d child\n", nchild);
87     /* loop to fork children */
88     for (i=0; i<nchild; i++) {
89         if ( (pid = fork()) < 0) {
90             printf("Error on %d child creation, %s\n", i+1, strerror(errno));
91         }
92         if (pid == 0) {   /* child */
93             printf("Child %d successfully executing\n", ++i);
94             sleep(3);
95             printf("Child %d exiting\n", i);
96             exit(0);
97         } else {          /* parent */
98             printf("Spawned %d child, pid %d \n", i+1, pid);
99         }
100     }
101     /* normal exit */
102     return 0;
103 }
104 /*
105  * routine to print usage info and exit
106  */
107 void usage(void) {
108     printf("Program forktest: fork a given number of child \n");
109     printf("Usage:\n");
110     printf("  forktest [-h] child to fork \n");
111     printf("  -h           print this help\n");
112     exit(1);
113 }