#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main() {
pid_t pid;
int status;
printf("Parent process (PID: %d) is about to fork...\n", getpid());
pid = fork();
if (pid < 0) {
// Fork failed
perror("fork");
exit(1);
} else if (pid == 0) {
// Child process
printf("Child process (PID: %d) is about to execute /bin/echo...\n", getpid());
char *args[] = {"/bin/echo", "Hello, World!", NULL};
execv("/bin/echo", args);
// If execv returns, it must have failed
perror("execv");
exit(1);
} else {
// Parent process
printf("Parent process is waiting for child (PID: %d) to complete...\n", pid);
pid_t wait_result = waitpid(pid, &status, 0);
if (wait_result == -1) {
perror("waitpid");
exit(1);
}
if (WIFEXITED(status)) {
printf("Child process exited with status %d\n", WEXITSTATUS(status));
} else {
printf("Child process did not exit normally\n");
}
}
return 0;
}