main.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./my_fifo",0664)==-1);
{
perror("mkfifo");
return -1;
}
return 0;
}
main1.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./my_fifo1",0664)==-1)
{
perror("mkfifo1");
return -1;
}
return 0;
}
send.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid = fork();
if(pid>0)//父进程
{
int send = open("./my_fifo",O_WRONLY);
if(send==-1)
{
perror("open send");
return -1;
}
char buff[1024];
while(1)
{
int len = read(0,buff,sizeof(buff));
write(send,buff,len);
if(strcmp(buff,"quit")==0)
{
break;
}
}
close(send);
}
else if(pid==0)//子进程
{
int send = open("./my_fifo1",O_RDONLY);
if(send==-1)
{
perror("open send");
return -1;
}
char buff[1024];
while(1)
{
int len = read(send,buff,sizeof(buff));
write(1,buff,len);
if(strcmp(buff,"quit")==0)
{
break;
}
}
close(send);
}
else
{
perror("fork");
return -1;
}
return 0;
}
rev.c
#include <myhead.h>
int main(int argc, const char *argv[])
{
pid_t pid;
pid = fork();
if(pid>0)//父进程
{
int rev = open("./my_fifo1",O_WRONLY);
if(rev==-1)
{
perror("open rev");
return -1;
}
char buff[1024];
while(1)
{
int len = read(0,buff,sizeof(buff));
write(rev,buff,len);
if(strcmp(buff,"quit")==0)
{
break;
}
}
close(rev);
}
else if(pid==0)//子进程
{
int rev = open("./my_fifo",O_RDONLY);
if(rev==-1)
{
perror("open rev");
return -1;
}
char buff[1024];
while(1)
{
int len = read(rev,buff,sizeof(buff));
write(1,buff,len);
if(strcmp(buff,"quit")==0)
{
break;
}
}
close(rev);
}
else
{
perror("fork");
return -1;
}
return 0;
}
08-22
296
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-23
498
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-24
1391
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-03