FIFO
FIFO Concept
管道的一个不足之处是没有名字,因此,只能用于具有亲缘关系的进程间通信,在命名管道(named pipe或FIFO)提出后,该限制得到了克服。
- FIFO不同于管道之处在于它提供一个路径名(pathname)与之关联,以FIFO的文件形式存储于文件系统中
- 命名管道是一个设备文件,因此,即使进程与创建FIFO的进程不存在亲缘关系,只要可以访问该路径,就能够通过FIFO相互通信
- FIFO(first input first output)总是按照先进先出的原则工作,第一个被写入的数据将首先从管道中读出
- 命名管道存储于硬盘,使用前要先打开(open())
- 命名管道也被称为FIFO文件,它是一种特殊类型的文件,它在文件系统中以文件名的形式存在,但是它的行为却和之前所讲的没有名字的管道(匿名管道)类似。
FIFO Create
- 在Shell下交互的创建一个命名管道
- 在程序中使用系统函数创建命名管道
//头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
//函数原型:
int mknod(const char *pathname, mode_t mode, dev_t dev);
int mkfifo(const char *pathname, mode_t mode);//建议尽量使用这个函数
//两个函数的pathname和mode参数含义相同
//pathname-->命名管道的全路径名
//mode-->创建命名管道的模式,指定文件的读取权限
//dev-->设备值,只在创建设备文件是才会用到
//返回值:成功(0);失败(-1)
FIFO Using Process
- 创建FIFO(FIFO Create)
打开FIFO(open())
O_RDWR(读写方式):不会导致阻塞
O_RDONLY(只读方式):调用open()函数的进程将会被阻塞,直到有写方打开管道
O_WRONLY(写方式):也会阻塞,直到有读方打开管道使用FIFO(与管道基本相同)
//FIFO write
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#define PATHNAME "/home/wgb/code/FIFO/FIFO/file.tmp"//注意路径的设置
#define MAX_SIZE 100
int main()
{
int ret = mkfifo(PATHNAME,S_IFIFO|0666);//创建管道
if(ret<0)
{
printf("mkfifo error\n");
printf("error num:%d error info:%s\n",ret,strerror(ret));
return -1;
}
int fd = open(PATHNAME,O_WRONLY);//打开管道
if(fd<0)
{
printf("open error\n");
printf("error num:%d error info:%s\n",fd,strerror(fd));
return -2;
}
char buf[MAX_SIZE];
memset(buf,'\0',sizeof(buf));
while(1)
{
scanf("%s",buf);
int ret = write(fd,buf,strlen(buf)+1);//写
if(ret<0)
{
printf("write error\n");
printf("error num:%d error info:%s\n",fd,strerror(fd));
break;
}
if(strncmp(buf,"exit",4)==0)//输入exit退出
break;
}
close(fd);
return 0;
}
//FIFO read:
#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>
#define PATHNAME "/home/wgb/code/FIFO/FIFO/file.tmp"
#define MAX_SIZE 100
int main()
{
int fd = open(PATHNAME,O_RDONLY);//打开管道
if(fd<0)
{
printf("open error\n");
printf("error num:%d error info:%s\n",fd,strerror(fd));
return -2;
}
char buf[MAX_SIZE];
memset(buf,'\0',sizeof(buf));
while(1)
{
int ret = read(fd,buf,sizeof(buf));//读取
if(ret<0)
{
printf("open error\n");
printf("error num:%d error info:%s\n",fd,strerror(fd));
break;
}
printf("%s\n",buf);
if(strncmp(buf,"exit",4)==0)//退出
break;
}
close(fd);//打开就要关闭
return 0;
}
总结
文件系统中的路径名是全局的,各进程都可以访问,因此可以用文件系统中的路径名来标识一个IPC通道。
由于Linux中所有的事物都可被视为文文件,所以对命名管道的使用也就变得与文件操作非常的统一,也使它的使用非常方便,同时我们也可以像平常的文件名一样在命令中使用。
mkfifo函数的作用用是在文文件系统中创建一一个文文件,该文文件用用于提供FIFO功能,即命名管道。
前边讲的那些管道都没有名字,因此它们被称为匿名管道,或简称管道。对文件系统来说,匿名管道是不可见的,它的作用仅限于在父进程和子进程两个进程间进行通信。而命名管道是一个可见的文件,因此,它可以用于任何两个进程之间的通信,不管这两个进程是不是父子进程,也不管这两个进程之间有没有关系。