1.
A进程先发送一句话给B进程,B进程接收后打印
B进程再回复一句话给A进程,A进程接收后打印
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0664) < 0)
{
if(errno != 17)
{
Err("mkfifo");
return -1;
}
}
int fd1 = open("./fifo",O_RDWR);
if(fd1 < 0)
{
Err("open");
return -1;
}
printf("open ok\n");
if(mkfifo("./fifo-1",0664) < 0)
{
if(errno != 17)
{
Err("mkfifo");
return -1;
}
}
int fd2 = open("./fifo-1",O_RDWR);
if(fd2 < 0)
{
Err("open");
return -1;
}
printf("open ok\n");
char buf[128]="";
ssize_t res = 0;
while(1)
{
//终端输入,打印到文件
printf("plesae enter:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if((res = write(fd1,buf,sizeof(buf))) < 0)
{
Err("write");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
//读取文件,打印到终端
bzero(buf,sizeof(buf));
if((res = read(fd2,buf,sizeof(buf))) < 0)
{
Err("read");
return -1;
}
if(0 == res)
{
printf("exit\n");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
printf("%s\n",buf);
}
close(fd1);
close(fd2);
return 0;
}
other
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <head.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./fifo",0664) < 0)
{
if(errno != 17)
{
Err("mkfifo");
return -1;
}
}
int fd1 = open("./fifo",O_RDWR);
if(fd1 < 0)
{
Err("open");
return -1;
}
printf("open ok\n");
if(mkfifo("./fifo-1",0664) < 0)
{
if(errno != 17)
{
Err("mkfifo");
return -1;
}
}
int fd2 = open("./fifo-1",O_RDWR);
if(fd2 < 0)
{
Err("open");
return -1;
}
printf("open ok\n");
char buf[128]="";
ssize_t res = 0;
while(1)
{
//读取文件,打印到终端
bzero(buf,sizeof(buf));
if((res = read(fd1,buf,sizeof(buf))) < 0)
{
Err("read");
return -1;
}
if(0 == res)
{
printf("exit\n");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
printf("%s\n",buf);
//终端输入,打印到文件
printf("plesae enter:");
fgets(buf,sizeof(buf),stdin);
buf[strlen(buf)-1] = '\0';
if((res = write(fd2,buf,sizeof(buf))) < 0)
{
Err("write");
return -1;
}
if(strcmp(buf,"quit") == 0)
break;
}
close(fd1);
close(fd2);
return 0;
}
2.思维导图