有名管道
#include <myhead.h>
int main(int argc, const char *argv[])
{
if(mkfifo("./my_fifo_1",0664)==-1)
{
perror("mkfifo_1");
return -1;
}
if(mkfifo("./my_fifo_2",0664)==-1)
{
perror("mkfifo_2");
return -1;
}
return 0;
}
就收发送
#include <myhead.h>
//
//有名管道,创建两个发送接收端,
//父进程写入管道1和管道2,
//子进程读取管道2和管道1
//
int main(int argc, const char *argv[])
{
pid_t pid1=fork();
if(pid1==0)
{
int fd2;
fd2=open("./my_fifo_2",O_RDONLY);
if(fd2==-1)
{
perror("open2");
return -1;
}
char buff1[1024];
while(1)
{
int len=read(fd2,buff1,sizeof(buff1));
write(1,buff1,len);
if(strcmp(buff1,"~")==0)
{
break;
}
}
close(fd2);
}
else if(pid1>0)
{
int fd1;
fd1=open("./my_fifo_1",O_WRONLY);
if(fd1==-1)
{
perror("open");
return -1;
}
char buff[1024];
while(1)
{
int len=read(0,buff,sizeof(buff));
write(fd1,buff,len);
if(strcmp(buff,"~")==0)
{
break;
}
}
close(fd1);
}
else
{
perror("fork");
return -1;
}
return 0;
}
2
#include <myhead.h>
//
//有名管道,创建两个发送接收端,
//父进程写入管道1和管道2,
//子进程读取管道2和管道1
//
//
int main(int argc, const char *argv[])
{
pid_t pid2=fork();
if(pid2==0)
{
int fd2;
fd2=open("./my_fifo_1",O_RDONLY);
if(fd2==-1)
{
perror("open2");
return -1;
}
char buff1[1024];
while(1)
{
int len=read(fd2,buff1,sizeof(buff1));
write(1,buff1,len);
if(strcmp(buff1,"~")==0)
{
break;
}
}
}
else if(pid2>0)
{
int fd1;
fd1=open("./my_fifo_2",O_WRONLY);
if(fd1==-1)
{
perror("open1");
return -1;
}
char buff[1024];
while(1)
{
int len=read(0,buff,sizeof(buff));
write(fd1,buff,len);
if(strcmp(buff,"~")==0)
{
break;
}
}
close(fd1);
}
else
{
perror("fork");
return -1;
}
return 0;
}
思维导图