/** * This program forks a separate process using the fork()/exec() system calls. * * Figure 3.10 * * @author Gagne, Galvin, Silberschatz * Operating System Concepts - Seventh Edition * Copyright John Wiley & Sons - 2005. */ #define _GNU_SOURCE #include #include #include #include #include #include #include int main (int argc, char **argv) { int n = 0, *m; pid_t pid; m = (int *) malloc(sizeof(int)); *m = 2; pid = fork (); if (pid < 0) { /* error occurred */ fprintf (stderr, "Fork Failed\n"); exit (-1); /* Get out. */ } else if (pid == 0) { /* child process */ printf ("** I am the child, my pid value: %d: n=%d; *m=%d; m's addr: %x**\n", pid, n, *m, m); n = 24; *m = 31; printf ("** I am the child, my pid value: %d: n=%d; *m=%d; **\n", pid, n, *m); exit(0); } else { /* parent process */ wait (NULL); printf ("Child Complete\n"); /* parent will wait for the child to complete */ n = 45; printf ("** I am the parent; child's %d: n=%d; *m=%d; m's addr: %x **\n", pid, n, *m, m); exit (0); } }