#include #include #include int main() { int pipe_ends[2]; if (pipe(pipe_ends) == -1) perror("pipe"), exit(-1); int child = fork(); if (child == -1) perror("fork"), exit(-1); if (child == 0) { char msg[] = { "Hi\n" }; close(pipe_ends[0]); write(pipe_ends[1], msg, sizeof msg); printf("Child done\n"); } else { char bread, pipe_buf[128]; close(pipe_ends[1]); sleep(1); printf("Child said "); // ensure that stdout buffer is flushed before // calling write(1, ) fflush(stdout); while ((bread = read(pipe_ends[0], pipe_buf, sizeof pipe_buf)) > 0) write(1, pipe_buf, bread); } }