#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>
管道读写
最新推荐文章于 2024-04-29 13:06:26 发布
本文介绍了一个简单的父子进程间通过管道进行通信的C语言程序示例。该程序演示了如何使用pipe函数创建管道,并通过write和read函数实现数据的写入与读取。
1172

被折叠的 条评论
为什么被折叠?



