#include #include #include #include #include #include int main (int argc, char** argv) { int status; pid_t child_pid; child_pid = fork(); switch (child_pid) { // fork error case -1: perror("fork"); return EXIT_FAILURE; // child code case 0: { printf("Child here. My pid is %d. Parent's pid is %d\n", getpid(), getppid()); sleep(3); // sleep for a while break; } // parent code default: { printf("Parent here. My pid is %d. Child's pid is %d\n", getpid(), child_pid); // wait for child to end execution pid_t waited_pid = waitpid(child_pid, &status, 0); if (waited_pid < 0) { perror("waitpid"); return EXIT_FAILURE; } printf("Parent here. Succesfully waited for child %d\n", waited_pid); } } return 0; }