Aggiornamenti + alcune sockopt di IP
[gapil.git] / listati / fork_test.c
1 #include <errno.h>       /* error definitions and routines */ 
2 #include <stdlib.h>      /* C standard library */
3 #include <unistd.h>      /* unix standard library */
4 #include <stdio.h>       /* standard I/O library */
5 #include <string.h>      /* string functions */
6
7 /* Help printing routine */
8 void usage(void);
9
10 int main(int argc, char *argv[])
11 {
12 /* 
13  * Variables definition  
14  */
15     int nchild, i;
16     pid_t pid;
17     int wait_child  = 0;
18     int wait_parent = 0;
19     int wait_end    = 0;
20     ...        /* handling options */
21     nchild = atoi(argv[optind]);
22     printf("Test for forking %d child\n", nchild);
23     /* loop to fork children */
24     for (i=0; i<nchild; i++) {
25         if ( (pid = fork()) < 0) { 
26             /* on error exit */ 
27             printf("Error on %d child creation, %s\n", i+1, strerror(errno));
28             exit(-1); 
29         }
30         if (pid == 0) {   /* child */
31             printf("Child %d successfully executing\n", ++i);
32             if (wait_child) sleep(wait_child);
33             printf("Child %d, parent %d, exiting\n", i, getppid());
34             exit(0);
35         } else {          /* parent */
36             printf("Spawned %d child, pid %d \n", i+1, pid);
37             if (wait_parent) sleep(wait_parent);
38             printf("Go to next child \n");
39         }
40     }
41     /* normal exit */
42     if (wait_end) sleep(wait_end);
43     return 0;
44 }