#include #include #include int main(int argc, char **argv) { int pipe_ends [2]; if (pipe(pipe_ends) == -1) perror("pipe"), exit(1); pid_t child = fork(); if (child == -1) perror("fork"), exit(1); if (child == 0) { // the child char msg [] = { "Hi\n" }; close(pipe_ends[0]); /* redirect stdout to the pipe! */ dup2(pipe_ends[1], 1); close(pipe_ends[1]); execlp("/bin/date", "date", NULL); } else { // the parent char pipe_buf[128]; close(pipe_ends[1]); size_t n; while ((n = read(pipe_ends[0], pipe_buf, sizeof pipe_buf)) > 0) if (write(1, pipe_buf, n) != n) perror("write"); } }