#include #include #include #include #include /* Global variable to manipulate */ int globalVariable = 2; int main(int argc, char *argv[]){ /* One to see where we are, one to wait for the child in our parent */ pid_t ws, pid; /* Needed for waitpid() */ int childExitStatus; /* We'll manipulate these too */ int localVariable = 20; char identifier[20]; pid = fork(); /* Child process */ if(pid == 0){ strcpy(identifier,"Child Process: "); globalVariable++; localVariable++; sleep(5); } /* Error condition */ else if(pid < 0){ printf("Failed to fork \n"); return -1; } /* Parent process, pid > 0 */ else{ strcpy(identifier,"Parent Process: "); ws = waitpid(pid,&childExitStatus,WUNTRACED); printf("Child process ended. pid was: %d\n",ws); } /* Code executed by both parent and child. */ printf("%s \n",identifier); printf(" Global variable: %d (address: %p)\n",globalVariable, &globalVariable); printf(" Local variable: %d (address: %p)\n",localVariable, &localVariable); return 0; }