projects
/
gapil.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Finita fork, inizio terminazione e wait
[gapil.git]
/
sources
/
ForkTest.c
diff --git
a/sources/ForkTest.c
b/sources/ForkTest.c
index 09035d5a66b1583d05a41c166c1ae8754864f74f..d57ef7a7436c469f0620ce45c3dfee50d9d31fe1 100644
(file)
--- a/
sources/ForkTest.c
+++ b/
sources/ForkTest.c
@@
-26,7
+26,7
@@
*
* Usage: forktest -h give all info's
*
*
* Usage: forktest -h give all info's
*
- * $Id: ForkTest.c,v 1.
2 2001/09/09 22:45:34
piccardi Exp $
+ * $Id: ForkTest.c,v 1.
4 2001/09/14 22:16:41
piccardi Exp $
*
****************************************************************/
/*
*
****************************************************************/
/*
@@
-46,23
+46,30
@@
int main(int argc, char *argv[])
/*
* Variables definition
*/
/*
* Variables definition
*/
- int i;
- int nchild;
+ int nchild, i;
pid_t pid;
pid_t pid;
+ int wait_child=0;
+ int wait_parent=0;
/*
* Input section: decode command line parameters
* Use getopt function
*/
opterr = 0; /* don't want writing to stderr */
/*
* Input section: decode command line parameters
* Use getopt function
*/
opterr = 0; /* don't want writing to stderr */
- while ( (i = getopt(argc, argv, "h")) != -1) {
+ while ( (i = getopt(argc, argv, "h
p:c:
")) != -1) {
switch (i) {
/*
* Handling options
*/
switch (i) {
/*
* Handling options
*/
- case 'h':
+ case 'h':
/* help option */
printf("Wrong -h option use\n");
usage();
printf("Wrong -h option use\n");
usage();
- return(0);
+ return -1;
+ break;
+ case 'c': /* take wait time for childen */
+ wait_child=strtol(optarg, NULL, 10); /* convert input */
+ break;
+ case 'p': /* take wait time for childen */
+ wait_parent=strtol(optarg, NULL, 10); /* convert input */
break;
case '?': /* unrecognized options */
printf("Unrecognized options -%c\n",optopt);
break;
case '?': /* unrecognized options */
printf("Unrecognized options -%c\n",optopt);
@@
-86,16
+93,20
@@
int main(int argc, char *argv[])
printf("Test for forking %d child\n", nchild);
/* loop to fork children */
for (i=0; i<nchild; i++) {
printf("Test for forking %d child\n", nchild);
/* loop to fork children */
for (i=0; i<nchild; i++) {
- if ( (pid = fork()) < 0) {
+ if ( (pid = fork()) < 0) {
+ /* on error exit */
printf("Error on %d child creation, %s\n", i+1, strerror(errno));
printf("Error on %d child creation, %s\n", i+1, strerror(errno));
+ exit(-1);
}
if (pid == 0) { /* child */
printf("Child %d successfully executing\n", ++i);
}
if (pid == 0) { /* child */
printf("Child %d successfully executing\n", ++i);
-
sleep(2
);
+
if (wait_child) sleep(wait_child
);
printf("Child %d exiting\n", i);
exit(0);
} else { /* parent */
printf("Spawned %d child, pid %d \n", i+1, pid);
printf("Child %d exiting\n", i);
exit(0);
} else { /* parent */
printf("Spawned %d child, pid %d \n", i+1, pid);
+ if (wait_parent) sleep(wait_parent);
+ printf("Go to next child \n");
}
}
/* normal exit */
}
}
/* normal exit */