#include<unistd.h> #include<sys/types.h> #include<errno.h> #include<stdio.h> #include<stdlib.h> #include<string.h> int main() { int pipe_fd[2]; pid_t pid; char buf_r[100]; char* p_wbuf; memset(buf_r,0,sizeof(buf_r)); if(pipe(pipe_fd)<0) { printf("pipe create error\n"); return -1; } if((pid=fork())==0) { printf("\n"); close(pipe_fd[1]); sleep(2); if(read(pipe_fd[0],buf_r,100)>0) printf("The Child read pipe is:\n %s\n\n\n",buf_r); close(pipe_fd[0]); exit(0); } else if(pid>0){ close(pipe_fd[0]); if(write(pipe_fd[1],"This is father,",15)!= -1) printf("Father is write pipe:\nThis is father,\n"); if(write(pipe_fd[1],"time is over.",13)!=-1) printf("time is over.\n\n\n"); close(pipe_fd[1]); sleep(3); waitpid(pid,NULL,0); exit(0); } }
C.匿名管道
最新推荐文章于 2021-05-19 01:24:47 发布