#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
char str[120] = "";
char buf[120] ="";
//打开管道
int fp = open("./myfifo", O_WRONLY);
if(fp < 0)
{
perror("mkfifo");
return -1;
}
printf("open1打开成功 fp = %d\n",fp);
int dp = open("./yourfifo", O_RDONLY);
if(dp < 0)
{
perror("open2");
return -1;
}
printf("open2 打开成功\n");
while(1)
{
printf("一写>>>");
scanf("%s",str);
int wr = write(fp, str, strlen(str));
if(wr < 0)
{
perror("write");
return -1;
}
if(strcmp(str,"quit") == 0)
{
printf("open1 写道关闭");//*
break;
}
int rd = read(dp, str, sizeof(str));
if(rd < 0)
{
perror("read");
return -1;
}
else if(rd == 0)
{
printf("管道写道关闭\n");
break;
}
printf("一号读>>>%s\n",str);
if(strcmp(str,"quit") == 0)
break;
/*
close(fp);
printf("fp关闭\n");
*/
}
close(fp);
close(dp);
printf("fo 以关闭\n");
return 0;
}
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, const char *argv[])
{
//创建管道文件
if(mkfifo("./myfifo", 0777) < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("myfifo 创建完成 \n");
if(mkfifo("./yourfifo", 0777) < 0)
{
if(errno != 17)
{
perror("yourfifo");
return -1;
}
}
printf("yourfifo is success\n");
char str[120] = "";
char buf[120] = "";
//打开管道
int fo = open("./myfifo", O_RDONLY);
if(fo < 0)
{
perror("open1");
return -1;
}
printf("open1管道打开成功 fo = %d\n",fo);
int dp = open("./yourfifo", O_WRONLY);
if(dp < 0)
{
perror("open2");
return -1;
}
printf("open2管道打开成功 dp = %d\n",dp);
//循环传输信息
while(1)
{
//读
int rd = read(fo, str, sizeof(str));
if(rd < 0)
{
perror("read");
return -1;
}
else if(rd == 0)
{
printf("管道写道关闭\n");
break;
}
printf("二号读>>>%s\n",str);
if(strcmp(str,"quit") == 0)
break;
//写
printf("二号写>>>");
scanf("%s",buf);
int wr = write(dp, buf, strlen(buf));
if(wr < 0)
{
perror("open2");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
}
close(fo);
close(dp);
printf("fo 关闭\n");
return 0;
}