X-Git-Url: https://gapil.gnulinux.it/gitweb/?a=blobdiff_plain;f=sources%2FForkTest.c;fp=sources%2FForkTest.c;h=5401e319be20b79d721225d8bd340cafbe060f62;hb=35a74444a2fac699a43daefd96369aa984f91870;hp=d57ef7a7436c469f0620ce45c3dfee50d9d31fe1;hpb=c6bb1ae340cad082718e43163b9595608ed123e1;p=gapil.git diff --git a/sources/ForkTest.c b/sources/ForkTest.c index d57ef7a..5401e31 100644 --- a/sources/ForkTest.c +++ b/sources/ForkTest.c @@ -26,7 +26,7 @@ * * Usage: forktest -h give all info's * - * $Id: ForkTest.c,v 1.4 2001/09/14 22:16:41 piccardi Exp $ + * $Id: ForkTest.c,v 1.5 2001/09/19 17:10:49 piccardi Exp $ * ****************************************************************/ /* @@ -48,14 +48,15 @@ int main(int argc, char *argv[]) */ int nchild, i; pid_t pid; - int wait_child=0; - int wait_parent=0; + int wait_child = 0; + int wait_parent = 0; + int wait_end = 0; /* * Input section: decode command line parameters * Use getopt function */ opterr = 0; /* don't want writing to stderr */ - while ( (i = getopt(argc, argv, "hp:c:")) != -1) { + while ( (i = getopt(argc, argv, "hp:c:e:")) != -1) { switch (i) { /* * Handling options @@ -66,10 +67,13 @@ int main(int argc, char *argv[]) return -1; break; case 'c': /* take wait time for childen */ - wait_child=strtol(optarg, NULL, 10); /* convert input */ + wait_child = strtol(optarg, NULL, 10); /* convert input */ break; case 'p': /* take wait time for childen */ - wait_parent=strtol(optarg, NULL, 10); /* convert input */ + wait_parent = strtol(optarg, NULL, 10); /* convert input */ + break; + case 'e': /* take wait before parent exit */ + wait_end = strtol(optarg, NULL, 10); /* convert input */ break; case '?': /* unrecognized options */ printf("Unrecognized options -%c\n",optopt); @@ -101,7 +105,7 @@ int main(int argc, char *argv[]) if (pid == 0) { /* child */ printf("Child %d successfully executing\n", ++i); if (wait_child) sleep(wait_child); - printf("Child %d exiting\n", i); + printf("Child %d, parent %d, exiting\n", i, getppid()); exit(0); } else { /* parent */ printf("Spawned %d child, pid %d \n", i+1, pid); @@ -110,6 +114,7 @@ int main(int argc, char *argv[]) } } /* normal exit */ + if (wait_end) sleep(wait_end); return 0; } /* @@ -118,7 +123,11 @@ int main(int argc, char *argv[]) void usage(void) { printf("Program forktest: fork a given number of child \n"); printf("Usage:\n"); - printf(" forktest [-h] child to fork \n"); + printf(" forktest [-h] [-p sec] [-c sec] [-e sec] child to fork \n"); printf(" -h print this help\n"); + printf(" -p sec wait sec seconds before next fork\n"); + printf(" -c sec wait sec seconds before child termination\n"); + printf(" -e sec wait sec seconds before parent return\n"); + exit(1); }