#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <unistd.h>
#include<fcntl.h>
#include<string.h>int main(int argc, const char *argv[])
{//开辟写入管道1if(mkfifo("./AAA",0777)<0){if(errno!=17){perror("fifo");return -1;}}printf("success\n");int fp=open("./AAA",O_WRONLY);if(fp<0){perror("open");return -1;}printf("打开成功\n");//开辟读取管道2if(mkfifo("./BBB",0777)<0){if(errno!=17){perror("fifo2");return -1;}}int RD=open("./BBB",O_RDONLY);if(RD<0){perror("RDeer");return -1;}char buf[20]="";char Rdate[20]="";//用于接收ssize_t res;while(1){//发送数据printf("请输入\n");fgets(buf,sizeof(buf),stdin);buf[strlen(buf)-1]=0;if(write(fp,buf,sizeof(buf))<0){perror("write");return -1;}printf("写入成功\n");if(strcmp(buf,"quit")==0){break;}//接受数据bzero(Rdate,sizeof(Rdate));res=read(RD,Rdate,sizeof(Rdate));if(res<0){perror("RES");return-1;}else if(res==0){printf("终端关闭,管道2\n");break;}printf("res=%ld Ddate=%s",res,Rdate);if(strcmp(Rdate,"quit")==0){break;}}return 0;
}
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<errno.h>
#include <unistd.h>
#include<fcntl.h>
#include<string.h>int main(int argc, const char *argv[])
{//接受管道if(mkfifo("./AAA",0777)<0){if(errno!=17){perror("fifo");return -1;}}printf("success\n");int fp=open("./AAA",O_RDONLY);if(fp<0){perror("open");return -1;}printf("打开成功2\n");//发送管道if(mkfifo("./BBB",0777)<0){if(errno!=17){perror("fifo2");return -1;}}int RD=open("./BBB",O_WRONLY);if(RD<0){perror("RDeer");return -1;}ssize_t res=0;char buf[20]="";//发送char Wdate[20]="";while(1){bzero(buf,sizeof(buf));res=read(fp,buf,sizeof(buf));if(res<0){perror("read");return -1;}else if(res==0){printf("写终端关闭,且没有数据\n");break;}printf("res=%ld buf=%s \n",res,buf);if(strcmp(buf,"quit")==0)break;//发送管道printf("请输入\n");fgets(Wdate,sizeof(Wdate),stdin);Wdate[strlen(Wdate)-1]=0;if(write(RD,Wdate,sizeof(Wdate))<0){perror("写入");return -1;}printf("写入成功\n");if(strcmp(Wdate,"quit")==0){break;}}close(fp);return 0;
}
运行结果