Versione finale del client ECHO su TCP, con esempio di uso della funzione
[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.10 2003/05/02 09:55:13 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 #include "Gapil.h"
42 #include "macros.h"
43
44 /* Help printing routine */
45 void usage(void);
46
47 int main(int argc, char *argv[])
48 {
49 /* 
50  * Variables definition  
51  */
52     int nchild, i;
53     pid_t pid;
54     int wait_child  = 0;
55     int wait_parent = 0;
56     int wait_end    = 0;
57     /*
58      * Input section: decode command line parameters 
59      * Use getopt function
60      */
61     opterr = 0;  /* don't want writing to stderr */
62     while ( (i = getopt(argc, argv, "hsp:c:e:")) != -1) {
63         switch (i) {
64         /* 
65          * Handling options 
66          */ 
67         case 'h':   /* help option */
68             printf("Wrong -h option use\n");
69             usage();
70             return -1;
71             break;
72         case 'c':   /* take wait time for childen */
73             wait_child = strtol(optarg, NULL, 10);    /* convert input */
74             break;
75         case 'p':   /* take wait time for childen */
76             wait_parent = strtol(optarg, NULL, 10);   /* convert input */
77             break;
78         case 'e':   /* take wait before parent exit */
79             wait_end = strtol(optarg, NULL, 10);      /* convert input */
80             break;
81         case 's':
82             Signal(SIGCHLD, HandSigCHLD);
83             break;
84         case '?':   /* unrecognized options */
85             printf("Unrecognized options -%c\n",optopt);
86             usage();
87         default:    /* should not reached */
88             usage();
89         }
90     }
91     /* ***********************************************************
92      * 
93      *           Options processing completed
94      *
95      *                Main code beginning
96      * 
97      * ***********************************************************/
98     /* There must be remaing parameters */
99     if (optind == argc) {
100         usage();
101     }
102     nchild = atoi(argv[optind]);
103     printf("Process %d: forking %d child\n", getpid(), nchild);
104     /* loop to fork children */
105     for (i=0; i<nchild; i++) {
106         if ( (pid = fork()) < 0) { 
107             /* on error exit */ 
108             printf("Error on %d child creation, %s\n", i+1, strerror(errno));
109             exit(-1); 
110         }
111         if (pid == 0) {   /* child */
112             printf("Child %d successfully executing\n", ++i);
113             if (wait_child) sleep(wait_child);
114             printf("Child %d, parent %d, exiting\n", i, getppid());
115             exit(0);
116         } else {          /* parent */
117             printf("Spawned %d child, pid %d \n", i+1, pid);
118             if (wait_parent) sleep(wait_parent);
119             printf("Go to next child \n");
120         }
121     }
122     /* normal exit */
123     if (wait_end) sleep(wait_end);
124     return 0;
125 }
126 /*
127  * routine to print usage info and exit
128  */
129 void usage(void) {
130     printf("Program forktest: fork a given number of child \n");
131     printf("Usage:\n");
132     printf("  forktest [-h] [-p sec] [-c sec] [-e sec] child to fork \n");
133     printf("  -h           print this help\n");
134     printf("  -s           install signal handler\n");
135     printf("  -p sec       wait sec seconds before next fork\n");
136     printf("  -c sec       wait sec seconds before child termination\n");
137     printf("  -e sec       wait sec seconds before parent return\n");
138     
139     exit(1);
140 }