#include <stdio> #include <stdlib> #include <unistd> #include <string> int main(int argc,char *argv[]){ int pipe_fd[2]; pid_t pid; char buff_r[100]; char *p_wbuf,*wdata; wdata = "Hello world"; int r_num; //初始化 memset(buff_r,0,sizeof(buff_r)); //创建管道 if(pipe(pipe_fd) 0){ printf("%d nums read from the pipe is %s\n",r_num,buff_r); } //关闭子进程读描述符 close(pipe_fd[0]); exit(0); }else if(pid > 0){ //父进程 //关闭父进程读描述符 close(pipe_fd[0]); //向管道写数据 if((write(pipe_fd[1],wdata,strlen(wdata))) != -1){ printf("write success\n"); } //关闭父进程写描述符 close(pipe_fd[1]); sleep(3); //等待子进程退出 waitpid(pid,NULL,0); exit(0); } } </string></unistd></stdlib></stdio>
管道读写
最新推荐文章于 2023-10-03 11:36:09 发布