#include #include #include #include #include #include int pipe_fd[2]; void do_child(void) { char *p = "Hello, dad or mam!\n"; printf("This is child.\n"); close(pipe_fd[0]); while (*p != '\0') { if (write(pipe_fd[1], p, 1) < 0) { perror("write"); exit(1); } p++; } } void do_parent(void) { char c; int count, status; printf("This is parent.\n"); close(pipe_fd[1]); while ((count = read(pipe_fd[0], &c, 1)) > 0) { putchar(c); } if (count < 0) { perror("read"); exit(1); } if (wait(&status) < 0) { perror("wait"); exit(1); } } int main(void) { int child; if (pipe(pipe_fd) < 0) { perror("pipe"); exit(1); } if ((child = fork()) < 0) { perror("fork"); exit(1); } if (child) { do_parent(); } else { do_child(); } return 0; }