/* * fork-and-wait.c */ #include #include #include #include int main(int argc, char *argv[]) { pid_t child_pid, exit_pid; int exit_value; child_pid = fork(); if(child_pid == 0) { printf("I am the child, my pid is %d.\n", getpid()); sleep(2); printf("I, the child just woke up.\n"); exit(42); } else { printf("I am the parent and my child's pid is %d.\n", child_pid); printf("Parent is waiting for child to exit...\n"); exit_pid = wait(&exit_value); printf("My child, pid %d, exited with value %d.\n", exit_pid, WEXITSTATUS(exit_value)); } }