linux-sysprog · advanced · ~15 min

Fork+exec /bin/echo

`execvp` semantics: image replacement; the only way back is via the exit status.

Challenge

Implement int run_echo(const char *msg) that fork+exec's /bin/echo msg and waits for it, returning the child's exit status (0 on success). The child must call execvp("echo", argv) so that the parent can wait for it and capture its exit code. The parent does no I/O — the harness redirects the child's stdout to a pipe before calling.

Starter code

#include <unistd.h>
#include <sys/wait.h>

int run_echo(const char *msg) {
    /* TODO */
    return -1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.