a.A进程先发送─句话给B进程,B进程接收后打印b.B进程再回复一句话给A进程,A进程接收后打印c.重复1.2步骤,当收到quit后,要结束AB进程d.提示:两根管道
#include <head.h>
int main()
{
int k1 = mkfifo("./fifo",0664);//创建有名管道文件fifo
if(k1 < 0)
{
if(errno != 17)
{
perror("mkfido");
return -1;
}
}
printf("mkfifo fifo success\n");
int k12 = mkfifo("./fifo1",0664);//创建有名管道文件fifo1
if(k12 < 0)
{
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("mkfifo fifo1 success\n");
int m1 = open("fifo1",O_RDONLY);//打开建立好的有名管道文件
if(m1 < 0)
{
perror("open: ");
return -1;
}
printf("fifo1 open read success\n");
int k2 = open("fifo",O_WRONLY);//打开建立好的有名管道文件
if(k2 < 0)
{
perror("open: ");
return -1;
}
printf("fifo open write success\n");
char buf[128] = "";
char buf1[128] = "";
int res;
int res1;
while(1)
{
printf("请输入==> ");
fgets(buf,sizeof(buf),stdin);
if(strcmp(buf,"quit") == 0)
{
return 0;
}
buf[strlen(buf) -1] = '\0';
if((res = write(k2,buf,sizeof(buf))) < 0)
{
perror("write");
return -1;
}
bzero(buf1,sizeof(buf1));
res1 = read(m1,buf1,sizeof(buf1));
if(strcmp(buf1,"quit") == 0)
{
printf("__%d__\n",__LINE__);
return 0;
}
if(res1 < 0)
{
perror("read");
return -1;
}
if(0 == res1)
{
break;
}
printf("%s",buf1);
}
return 0;
}
--------------------------------------------------
#include <head.h>
int main()
{
int m1 = open("fifo1",O_WRONLY);
if(m1 < 0)
{
perror("open");
return -1;
}
printf("fifo1 open write success\n");
int k2 = open("fifo",O_RDONLY);
if(k2 < 0)
{
perror("open");
return -1;
}
printf("fifo open read success\n");
char buf[128] = "";
char buf1[128] = "";
int res;
int res1;
while(1)
{
bzero(buf,sizeof(buf));
res = read(k2,buf,sizeof(buf));
if(strcmp(buf,"quit") == 0)
{
printf("__%d__\n",__LINE__);
break;
}
if(res < 0)
{
perror("read");
return -1;
}
if(0 == res)
{
break;
}
printf("%s\n",buf);
printf("请输入==> ");
fgets(buf1,sizeof(buf1),stdin);
if(strcmp(buf1,"quit") == 0)
{
printf("__%d__\n",__LINE__);
break;
}
buf[strlen(buf) -1] = '\0';
if((res1 = write(m1,buf1,sizeof(buf1))) < 0)
{
perror("write");
return -1;
}
}
return 0;
}