#include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <stdio.h> #include <string.h> int main () { int fd[2],cld_pid,status; char buf[200],len; if (pipe(fd)==-1){ perror("create pipe error"); return(1); } if ((cld_pid=fork())== 0){ close(fd[1]); len=read((fd[0]),buf,sizeof(buf)); buf[len]=0; printf("子进程从管道中读取的数据是:%s ",buf); return(0); } else { close(fd[0]); sprintf(buf,"父进程为子进程(pid=%d) 创建的数据!",cld_pid); write(fd[1],buf,strlen(buf)); return(0); } return 0; }