#include #include #include #include int main() { pid_t pid; int x = 3; pid = fork(); /* fork another process */ if (pid < 0) /* Forking error! */ { exit(-1); } else if (0 == pid) /* Child process */ { // Child process sleeps -- does nothing -- for 2 seconds. sleep(2); // Child prints its value of x. printf("Child x = %d\n", x); exit(0); } else /* pid > 0: Parent process */ { // Parent changes value of x. Because this is *after* the fork(), // the child process will retain its unchanged value of x. x = 4; printf("Parent x = %d\n", x); wait(NULL); exit(0); } }