1、有名管道
◼ 匿名管道,由于没有名字,只能用于亲缘关系的进程间通信。为了克服这个缺点,提出了有名管道(FIFO),也叫命名管道、
FIFO文件。
◼ 有名管道(FIFO)不同于匿名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,并且其打开方式与打开一个普通文件是一样的,这样即使与 FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通过 FIFO 相互通信,因此,通过 FIFO 不相关的进程也能交换数据。
◼ 一旦打开了 FIFO,就能在它上面使用与操作匿名管道和其他文件的系统调用一样的I/O系统调用了(如read()、write()和close())。与管道一样,FIFO 也有一个写入端和读取端,并且从管道中读取数据的顺序与写入的顺序是一样的。FIFO 的名称也由此而来:先入先出。
◼ 有名管道(FIFO)和匿名管道(pipe)有一些特点是相同的,不一样的地方在于:
- FIFO 在文件系统中作为一个特殊文件存在,但 FIFO 中的内容却存放在内存中。
- 当使用 FIFO 的进程退出后,FIFO 文件将继续保存在文件系统中以便以后使用。
- FIFO 有名字,不相关的进程可以通过打开有名管道进行通信。
2、有名管道的使用
◼ 通过命令创建有名管道
mkfifo 名字
◼ 通过函数创建有名管道
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
◼ 一旦使用 mkfifo 创建了一个 FIFO,就可以使用 open 打开它,常见的文件I/O 函数都可用于 fifo。如:close、read、write、unlink 等。
◼ FIFO 严格遵循先进先出(First in First out),对管道及 FIFO 的读总是从开始处返回数据,对它们的写则把数据添加到末尾。它们不支持诸如 lseek() 等文件定位操作。
3、创建有名管道
/*
创建fifo文件
1、通过命令:mkfifo 名字
2、通过函数: mkfifo(const char *pathname, mode_t mode);
#include <sys/types.h>
#include <sys/stat.h>
-pathname:管道名称的路径
-mode:文件的权限和open一样 和umask相与
返回值:返回0,失败返回-1,并设置错误号
*/
#include <sys/types.h>
#include <sys/stat.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
int main()
{
//先判断文件是否存在
int ret=mkfifo("fifo1",0664);
if(ret == -1)
{
perror("管道不存在,创建管道\n");
perror("mkfifo");
exit(0);
}
return 0;
}
4、有名管道进行读写
//向管道中写数据
#include <sys/types.h>
#include <sys/stat.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include<fcntl.h>
/*
1.一个为只读打开管道进程会阻塞,直到写端打开
2.一个为只写打开管道进程会阻塞,直到读端打开
*/
int main()
{
//1、先判断文件是否存在
int ret=access("test",F_OK);
if(ret == -1)
{
perror("管道不存在,创建管道\n");
// 2、创建管道文件
ret=mkfifo("test",0664);
if(ret == -1)
{
perror("mkfifo");
exit(0);
}
}
//3、打开管道,以只写的方式
int fd=open("test",O_WRONLY);
if(fd ==-1)
{
perror("open");
exit(0);
}
//4、写数据
for(int i=0;i<100;i++)
{
char buf[1024];
sprintf(buf,"hello, %d\n ",i);
printf("write data: %s\n",buf);
write(fd,buf,strlen(buf));
sleep(1);
}
close(fd);
return 0;
}
#include <sys/types.h>
#include <sys/stat.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <unistd.h>
#include<fcntl.h>
int main()
{
//1、打开管道文件
int fd=open("test",O_RDONLY);
if(fd==-1)
{
perror("open");
exit(0);
}
//2、读数据
while(1)
{
char buf[1024]={0};
int len=read(fd,buf,sizeof(buf));
if(len==0)
{
printf("写端断开连接...\n");
break;
}
printf("recv buf: %s\n",buf);
}
close(fd);
return 0;
}
读写进程同时打开,进程才能运行
5、使用有名管道进行简单地聊天
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main()
{
//1、判断有名管道文件是否存在
int ret=access("fifo1",F_OK);
if(ret==-1)
{
//文件不存在
printf("管道不存在,创建对应的有名管道\n");
//创建管道
ret= mkfifo("fifo1",0664);
if (ret ==-1)
{
perror("mkfifo");
exit(0);
}
}
//判断管道2是否存在
ret=access("fifo2",F_OK);
if(ret==-1)
{
//文件不存在
printf("管道不存在,创建对应的有名管道\n");
ret= mkfifo("fifo2",0664);
if (ret ==-1)
{
perror("mkfifo");
exit(0);
}
}
//以只写的方式打开fifo1
int fdw =open("fifo1",O_WRONLY);
if(fdw ==-1)
{
perror("open");
exit(0);
}
printf("打开管道1fifo1成功,等待写入数据\n");
//以只读的方式打开fifo2
int fdr=open("fifo2",O_RDONLY);
if(fdr ==-1)
{
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待读取...\n");
char buf[128];
//4、循环读取数据
while(1)
{
memset(buf,0,128);
//读取标准输入的数据
fgets(buf,128,stdin);
//写数据
ret=write(fdw,buf,strlen(buf));
if(ret==-1)
{
perror("write");
exit(0);
}
//5、读管道数据
memset(buf,0,128);//清空数据
ret=read(fdr,buf,128);
if(ret<=0)
{
perror("read");
break;
}
printf("buf:%s\n",buf);
}
//6、关闭文件描述符
close(fdr);
close(fdw);
return 0;
}
#include<stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main()
{
//1、判断有名管道文件是否存在
int ret=access("fifo1",F_OK);
if(ret==-1)
{
//文件不存在
printf("管道不存在,创建对应的有名管道\n");
ret= mkfifo("fifo1",0664);
if (ret ==-1)
{
perror("mkfifo");
exit(0);
}
}
ret=access("fifo2",F_OK);
if(ret==-1)
{
//文件不存在
printf("管道不存在,创建对应的有名管道\n");
ret= mkfifo("fifo2",0664);
if (ret ==-1)
{
perror("mkfifo");
exit(0);
}
}
//2、以只读的方式打开fifo1
int fdr =open("fifo1",O_RDONLY);
if(fdr ==-1)
{
perror("open");
exit(0);
}
printf("打开管道1fifo1成功,等待读取数据\n");
//3、以只写的方式打开fifo2
int fdw=open("fifo2",O_WRONLY);
if(fdw ==-1)
{
perror("open");
exit(0);
}
printf("打开管道fifo2成功,等待写入...\n");
char buf[128];
//4、循环读写数据
while(1)
{
//5、读管道数据
memset(buf,0,128);
ret=read(fdr,buf,128);
if(ret<=0)
{
perror("read");
break;
}
printf("buf:%s\n",buf);
}
memset(buf,0,128);
//读取标准输入的数据
fgets(buf,128,stdin);
//写数据
ret= write(fdw,buf,strlen(buf));
if(ret==-1)
{
perror("write");
exit(0);
}
//6、关闭文件描述符
close(fdr);
close(fdw);
return 0;
}