#define _POSIX_C_SOURCE 200809L
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main(void) {
    /* This example begins with a single-threaded process. */
    pid_t child = fork();
    if (child == -1) { perror("fork"); return EXIT_FAILURE; }
    if (child == 0) {
        execl("/bin/echo", "echo", "child", (char *)NULL);
        perror("execl");
        _exit(127);
    }
    int status;
    pid_t result;
    do { result = waitpid(child, &status, 0); }
    while (result == -1 && errno == EINTR);
    if (result == -1) { perror("waitpid"); return EXIT_FAILURE; }
    if (WIFEXITED(status)) {
        printf("parent: child exit status = %d\n", WEXITSTATUS(status));
        return WEXITSTATUS(status) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
    }
    if (WIFSIGNALED(status))
        fprintf(stderr, "parent: child terminated by signal %d\n", WTERMSIG(status));
    return EXIT_FAILURE;
}
