#include<head.h>
int main(int argc, const char *argv[])
{
//以只写的形式打开管道文件
int rfd=-1;
if((rfd=open("./myfifo",O_RDONLY))==-1)
{
perror("open rfd error");
return -1;
}
printf("读端打开\n");
//从管道文件中读取数据
char rbuf[128]="";
while(1)
{
bzero(rbuf,sizeof(rbuf));
//读取数据
read(rfd,rbuf,sizeof(rbuf));
printf("收到1:%s\n",rbuf);
if(strcmp(rbuf,"quit")==0)
{
break;
}
}
close(rfd);
if((rfd=open("./myfifo",O_WRONLY))==-1)
{
perror("open rfd error");
return -1;
}
printf("写端打开成功\n");
while(1)
{
bzero(rbuf,sizeof(rbuf));
//终端输入
read(0,rbuf,sizeof(rbuf));
rbuf[strlen(rbuf)-1]='\0';
write(rfd,rbuf,sizeof(rbuf));
if(strcmp(rbuf,"quit")==0)
{
break;
}
}
close(rfd);
return 0;
}
#include<head.h>
int main(int argc, const char *argv[])
{
//以只写的形式打开管道文件
int wfd=-1;
if((wfd=open("./myfifo",O_WRONLY))==-1)
{
perror("open wfd error");
return -1;
}
printf("写端打开成功\n");
char wbuf[128]="";
while(1)
{
bzero(wbuf,sizeof(wbuf));
//终端输入
read(0,wbuf,sizeof(wbuf));
wbuf[strlen(wbuf)-1]='\0';
write(wfd,wbuf,sizeof(wbuf));
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(wfd);
if((wfd=open("./myfifo",O_RDONLY))==-1)
{
perror("open wfd error");
return -1;
}
printf("读端打开\n");
//从管道文件中读取数据
while(1)
{
bzero(wbuf,sizeof(wbuf));
//读取数据
read(wfd,wbuf,sizeof(wbuf));
printf("收到1:%s\n",wbuf);
if(strcmp(wbuf,"quit")==0)
{
break;
}
}
close(wfd);
return 0;
}
![](https://i-blog.csdnimg.cn/blog_migrate/1e60b6fb172590df6d0a120dd1846efa.png)