#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char str[1024] = {0};
//创建管道文件
mkfifo("/home/gec/pipe",0777);
mkfifo("/home/gec/pipe1",0777);
int fd = open("/home/gec/pipe",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道1打开成功\n");
}
int fd1 = open("/home/gec/pipe1",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道2打开成功\n");
}
size_t pid = fork();
if(pid == 0)//子进程
{
while (1)
{
read(fd,str,1024);
printf("%s\n",str);
}
}else if(pid > 0)
{
while (1)
{
scanf("%s",str);
write(fd1,str,strlen(str));
}
}
return 0;
}
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char str[1024] = {0};
int fd = open("/home/gec/pipe",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道1打开成功\n");
}
int fd1 = open("/home/gec/pipe1",O_RDWR);
if(fd < 0)
{
perror("管道打开失败\n");
return -1;
}else
{
printf("管道2打开成功\n");
}
size_t pid = fork();
if(pid == 0)//子进程
{
while (1)
{
read(fd1,str,1024);
printf("%s\n",str);
}
}else if(pid > 0)
{
while (1)
{
scanf("%s",str);
write(fd,str,strlen(str));
}
}
return 0;
}
创建两个管道文件 互相发送接收