#include /* Symbolic Constants */ #include /* Primitive System Data Types */ #include /* Errors */ #include /* Input/Output */ #include /* Wait for Process Termination */ #include /* General Utilities */ int global; pid_t childpid; /* variable to store the child's pid */ int status; /* parent process: child's exit status */ void childfoo() { global = 100; printf("CHILD: I am the child process! PID: %d\n", getpid()); printf("CHILD: My Value of global : %d\n", global); exit(0); } int main() { /* only 1 int variable is needed because each process would have its own instance of the variable here, 2 int variables are used for clarity */ /* now create new process */ childpid = fork(); if (childpid >= 0) /* fork succeeded */ { if (childpid == 0) /* fork() returns 0 to the child process */ { childfoo(); } else /* fork() returns new pid to the parent process */ { wait(&status); /* wait for child to exit, and store its status */ printf("PARENT: I am the parent process %d\n", getpid()); printf("PARENT: My Value of global : %d\n", global); exit(0); /* parent exits */ } } else /* fork returns -1 on failure */ { perror("fork"); /* display error message */ exit(0); } }