实现AB进程随时收发数据
代码实现如下:
fifo_r.c:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo文件
if(mkfifo("./my_fifo", 0664) < 0)
{
//文件已经存在导致的错误是允许的
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO success\n");
//创建fifo文件
if(mkfifo("./my_fifo1", 0664) < 0)
{
//文件已经存在导致的错误是允许的
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO1 success\n");
//打开,只写
int fd = open("./my_fifo",O_WRONLY);
if(fd < 0)
{
perror("open");
return -1;
}
printf("open FIFO success\n");
//打开,只读
int fd1 = open("./my_fifo1",O_RDONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open FIFO1 success\n");
char buf[128] = "";
ssize_t res = 0;
pid_t pid = fork();
if(pid > 0)
{
while(1)
{
//往管道中写入数据
bzero(buf,sizeof(buf)); //清空字符串
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin); //等待终端输入
buf[strlen(buf)-1] = 0;
res = write(fd,buf, sizeof(buf));
if(res < 0)
{
perror("write");
return -1;
}
printf("发送数据成功 res = %ld\n", res);
if(strcmp(buf,"quit") == 0)
break;
}
//关闭
close(fd);
wait(NULL);
}
else if(0 == pid)
{
while(1)
{
//从管道中读取数据
bzero(buf,sizeof(buf)); //清空字符串
res = read(fd1,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res) //写端均关闭
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("对方说 : %s\n", buf);
if(strcmp(buf,"quit") == 0)
break;
}
//关闭
close(fd1);
}
else
{
perror("fork");
return -1;
}
return 0;
}
fifo_w.c:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
int main(int argc, const char *argv[])
{
//清空umask
umask(0);
//创建fifo文件
if(mkfifo("./my_fifo", 0664) < 0)
{
//文件已经存在导致的错误是允许的
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO success\n");
//创建fifo文件
if(mkfifo("./my_fifo1", 0664) < 0)
{
//文件已经存在导致的错误是允许的
if(errno != 17)
{
perror("mkfifo");
return -1;
}
}
printf("create FIFO1 success\n");
//打开,只读
int fd = open("./my_fifo",O_RDONLY);
if(fd < 0)
{
perror("open");
return -1;
}
printf("open FIFO success\n");
char buf[128] = "";
ssize_t res = 0;
//打开,只写
int fd1 = open("./my_fifo1",O_WRONLY);
if(fd1 < 0)
{
perror("open");
return -1;
}
printf("open FIFO1 success\n");
pid_t pid = fork();
if(pid > 0)
{
while(1)
{
//从管道中读取数据
bzero(buf,sizeof(buf)); //清空字符串
res = read(fd,buf,sizeof(buf));
if(res < 0)
{
perror("read");
return -1;
}
else if(0 == res) //写端均关闭
{
fprintf(stderr,"对方进程退出\n");
break;
}
printf("对方说 : %s\n", buf);
if(strcmp(buf,"quit") == 0)
break;
}
//关闭
close(fd);
wait(NULL);
}
else if(0 == pid)
{
while(1)
{
//往管道中写入数据
bzero(buf,sizeof(buf)); //清空字符串
printf("请输入>>>");
fgets(buf, sizeof(buf), stdin); //等待终端输入
buf[strlen(buf)-1] = 0;
res = write(fd1,buf, sizeof(buf));
if(res < 0)
{
perror("write");
return -1;
}
printf("发送数据成功 res = %ld\n", res);
if(strcmp(buf,"quit") == 0)
break;
}
//关闭
close(fd1);
}
else
{
perror("fork");
return -1;
}
return 0;
}
运行结果如下: