--- /dev/null
+/****************************************************************
+ *
+ * Program ForkTest.c:
+ * Program to test process creation
+ *
+ * Author: Simone Piccardi
+ * Sep. 2001
+ *
+ * Usage: forktest -h give all info's
+ *
+ * $Id: ForkTest.c,v 1.1 2001/09/09 17:39:15 piccardi Exp $
+ *
+ ****************************************************************/
+/*
+ * Include needed headers
+ */
+#include <errno.h> /* error definitions and routines */
+#include <stdlib.h> /* C standard library */
+#include <unistd.h> /* unix standard library */
+#include <stdio.h> /* standard I/O library */
+#include <string.h> /* string functions */
+
+/* Help printing routine */
+void usage(void);
+
+int main(int argc, char *argv[])
+{
+/*
+ * Variables definition
+ */
+ int i;
+ int nchild;
+ pid_t pid;
+ /*
+ * Input section: decode command line parameters
+ * Use getopt function
+ */
+ opterr = 0; /* don't want writing to stderr */
+ while ( (i = getopt(argc, argv, "h")) != -1) {
+ switch (i) {
+ /*
+ * Handling options
+ */
+ case 'h':
+ printf("Wrong -h option use\n");
+ usage();
+ return(0);
+ break;
+ case '?': /* unrecognized options */
+ printf("Unrecognized options -%c\n",optopt);
+ usage();
+ default: /* should not reached */
+ usage();
+ }
+ }
+ /* ***********************************************************
+ *
+ * Options processing completed
+ *
+ * Main code beginning
+ *
+ * ***********************************************************/
+ /* There must be remaing parameters */
+ if (optind == argc) {
+ usage();
+ }
+ nchild = atoi(argv[optind]);
+ printf("Test for forking %d child\n", nchild);
+ /* loop to fork children */
+ for (i=0; i<nchild; i++) {
+ if ( (pid = fork()) < 0) {
+ printf("Error on %d child creation, %s\n", i, strerror(errno));
+ }
+ if (pid == 0) { /* child */
+ printf("Child %d successfully executing\n", i++);
+ sleep(2);
+ printf("Child %d exiting\n", i);
+ exit(0);
+ } else { /* parent */
+ printf("Spawned %d child, pid %d \n", i, pid);
+ }
+ }
+ /* normal exit */
+ return 0;
+}
+/*
+ * routine to print usage info and exit
+ */
+void usage(void) {
+ printf("Program forktest: fork a given number of child \n");
+ printf("Usage:\n");
+ printf(" forktest [-h] child to fork \n");
+ printf(" -h print this help\n");
+ exit(1);
+}