#include #include #include #include #include /* * A simple demo of using fork, exec, and waitpid. */ int main() { printf("program started\n"); pid_t pid = fork(); // make a child process if (pid < 0) { printf("fork failed\n"); } else if (pid == 0) { // child process printf("this is the child process\n"); // run some other program if (execlp("/bin/pwd", "/bin/pwd", NULL) < 0) { printf("exec failed\n"); } // will never reach here } else { // parent process printf("this is the parent process (child pid is %d)\n", pid); int child_status; if (waitpid(pid, &child_status, 0) < 0) { printf("waitpid failed\n"); exit(1); } printf("child process is reaped\n"); } printf("program ending\n"); return 0; }