#include <stdio.h>
#include <head.h>
int main(int argc, const char *argv[])
{
//创建有名管道
if(mkfifo("./fifo", 0664) < 0)
{
if (errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("mififo success\n");
char buf[128] = "";
char s[128] = "";
ssize_t res1 = 0;
ssize_t res2 = 0;
while (1)
{
int fd = open("./fifo", O_WRONLY);
if (fd < 0)
{
ERR_MSG("open");
return -1;
}
//printf("open success fd = %d\n", fd);
printf("请输入》》》");
fgets(buf, sizeof(buf), stdin);
buf[strlen(buf) - 1] = 0;
if (res1 = write(fd, buf, sizeof(buf)) < 0)
{
ERR_MSG("write");
return -1;
}
printf("写入数据成功 res1 = %ld\n", res1);
if (strcmp(buf, "quit") == 0)
{
break;
}
close(fd);
int fs = open("./fifo", O_RDONLY);
if (fs < 0)
{
ERR_MSG("open");
return -1;
}
bzero(s, sizeof(s));
res2 = read(fs, s, sizeof(s));
if (res2 < 0)
{
ERR_MSG("read");
return -1;
}
if (0 == res2)
{
printf("写端关闭\n");
break;
}
printf("res2 = %ld : %s\n", res2, s);
if (strcmp(s, "quit") == 0)
{
break;
}
close(fs);
}
return 0;
}
#include <stdio.h>
#include <head.h>
int main(int argc, const char *argv[])
{
//创建有名管道
if(mkfifo("./fifo", 0664) < 0)
{
if (errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("mififo success\n");
//printf("open success fd = %d\n", fd);
char buf[128] = "";
char s[128] = "";
ssize_t res1 = 0;
ssize_t res2 = 0;
while (1)
{
int fd = open("./fifo", O_RDONLY);
if (fd < 0)
{
ERR_MSG("open");
return -1;
}
bzero(buf, sizeof(buf));
res1 = read(fd, buf, sizeof(buf));
if (res1 < 0)
{
ERR_MSG("read");
return -1;
}
if (0 == res1)
{
printf("写端关闭\n");
break;
}
printf("res1 = %ld : %s\n", res1, buf);
if (strcmp(buf, "quit") == 0)
{
break;
}
close(fd);
int fs = open("./fifo", O_WRONLY);
if (fs < 0)
{
ERR_MSG("open");
return -1;
}
printf("请输入》》》");
fgets(s, sizeof(s), stdin);
s[strlen(s) - 1] = 0;
if (res2 = write(fs, s, sizeof(s)) < 0)
{
ERR_MSG("write");
return -1;
}
printf("写入数据成功 res2 = %ld\n", res2);
if (strcmp(s, "quit") == 0)
{
break;
}
close(fs);
}
return 0;
}