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 */
7 /* Help printing routine */
10 int main(int argc, char *argv[])
13 * Variables definition
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) {
27 printf("Error on %d child creation, %s\n", i+1, strerror(errno));
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());
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");
42 if (wait_end) sleep(wait_end);