#include #include #include #define MAXLINE 132 int main(void) { int n, fd[2]; pid_t pid; char line[MAXLINE]; if (pipe(fd) < 0) { perror("pipe error"); return(1); } if ((pid = fork()) < 0) perror("fork failed"); else if (pid > 0) { /* parent */ close(fd[0]); fprintf(stderr, "parent %d: writing to pipe\n", getpid()); write(fd[1], "hello world\n", 12); } else { /* child */ close(fd[1]); fprintf(stderr, "child %d: reading from pipe\n", getpid()); n = read(fd[0], line, MAXLINE); write(1, line, n); } return(0); }