/* fork2.c - Create 2 child processes. */ #include #include main(int argc, char **argv) { char *catarg[3], *factorarg[3]; int i, return_value; /* Here are the arguments to pass to child process. */ catarg[0] = "cat"; catarg[1] = "f1.txt"; catarg[2] = NULL; factorarg[0] = "factor"; factorarg[1] = "5040"; factorarg[2] = NULL; /* Print out Process ID. Used to prove which process is talking. */ printf("I am process %d.\n", getpid()); /* Try to fork and create a new process address space, then * in the child: attempt to exec the 'cat' program/command. */ if (fork() == 0) { printf (" Cat process %d\n", getpid()); execv("/bin/cat", catarg); } else { /* The parent: if we successfully called cat, we are going to "wait" * until it finishes and then only move on from there. */ printf (" (parent) pid = %d\n", getpid(), i); // Experiment: Take out this call to wait to see funny behavior. wait(&i); } if (fork() == 0) { printf(" Factor process %d\n", getpid()); execv("/usr/bin/factor", factorarg); } else { printf(" (parent) pid = %d\n", getpid()); wait(&i); } }