#include #include #include #include int main(void) { int n, fd[2]; pid_t pid; char line[64]; if (pipe(fd) < 0) { perror("pipe error"); exit(1); } if ( (pid = fork()) < 0) { perror("fork error"); exit(1); } else if (pid > 0) { /* parent */ snprintf(line, 64, "Hello child! I'm your parent pid %d!\n", getpid()); close(fd[0]); write(fd[1], line, strlen(line)); } else { /* child */ close(fd[1]); n = read(fd[0], line, 64); write(STDOUT_FILENO, line, n); printf("I'm the child(%d).\n", getpid()); } exit(0); }