概念
何谓命名管道
- 匿名管道应用的一个限制就是只能在具有共同祖先(具有亲缘关系)的进程间通信。
- 如果我们想在不相关的进程之间交换数据,可以使用FIFO文件来做这项工作,它经常被称为命名管道。
- 命名管道是一种特殊类型的文件。
FIFO是Linux基础文件类型中的一种。但FIFO文件在磁盘上没有数据块,仅仅用来标识内核中一条通道。各进程可以打开这个文件进行read/write,实际上是在读写内核通道,这样就实现了进程间通信。
匿名管道与命名管道的区别
- 匿名管道由pipe函数创建并打开–一个步骤
- 命名管道由mkfifo函数创建,打开用open–两个步骤
- FIFO(命名管道)与pipe(匿名管道)之间唯一的区别在它们创建与打开的方式不同,一但这些工作完成之后,它们具有相同的语义。
创建一个命名管道
- 命名管道可以从命令行上创建,命令行方法是使用下面这个命令:
$ mkfifo filename
- 命名管道也可以从程序里创建,相关函数有:
int mkfifo(const char *filename,mode_t );
- 参考手册
man 3 mkfifo
命名管道的打开规则
- 如果当前打开操作是为读而打开FIFO时
- O_NONBLOCK disable:阻塞直到有相应进程为写而打开该FIFO
- O_NONBLOCK enable:立刻返回成功–意味着非阻塞模式下按读的方式打开永远成功
如果当前打开操作是为写而打开FIFO时
- O_NONBLOCK disable:阻塞直到有相应进程为读而打开该FIFO
- O_NONBLOCK enable:立刻返回失败–意味着非阻塞模式下按写的方式打开永远失败,错误码为ENXIO
mknod
管道文件不能使用vi打开–vi不能打开看管道内容
一旦使用mkfifo创建了一个FIFO,就可以使用open打开它,常见的文件I/O函数都可用于fifo。如:close、read、write、unlink等。
命名管道的读写规则
同匿名管道
示例代码
- 写FIFO
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
/*
利用管道,两个进程间进行文件复制。
fifow
读文件1.txt
写入管道
fifor
读管道
写入2.txt文件
*/
int main(int argc, char *argv[])
{
int infd;
int outfd;
char buf[1024];
int n = 0;
//建立管道文件
mkfifo("fifop", 0644);
//按只读方式 打开现有文件--文件作为输入
infd = open("./1.txt", O_RDONLY);
if (infd == -1)
{
printf("infd open() err..\n");
exit(0);
}
//打开管道文件,准备写数据--FIFO管道作为输出
//outfd = open("fifop", O_WRONLY | O_NONBLOCK);
outfd = open("fifop", O_WRONLY ); //默认的阻塞模式下按只写方式打开会阻塞--直到有另一个进程按只读方式打开该FIFO
if (outfd == -1)
{
printf("infd open() err..\n");
exit(0);
}
//从文件中读数据写入管道文件
while ( (n = read(infd, buf, sizeof(buf))) > 0)
{
write(outfd, buf, n);
}
close (infd);
close (outfd);
printf("fifow 写管道文件 success\n");
return 0;
}
- 读FIFO
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
int main(int argc, char *argv[])
{
int infd;
int outfd;
char buf[1024];
int n = 0;
//打开2.txt 准备写数据--文件作为输出
outfd = open("./2.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644); //O_TRUNC 打开文件清空
if (outfd == -1)
{
printf("open() 2.txt err..\n");
exit(0);
}
//打开管道文件,准备读数据--FIFO管道作为输入
infd = open("fifop", O_RDONLY);//默认的阻塞模式下按只读方式打开会阻塞--直到有另一个进程按只写方式打开该FIFO
if (infd == -1)
{
printf("open err ... \n");
exit(0);
}
//从文件中读数据写入管道文件
while ( (n = read(infd, buf, 1024)) > 0)
{
write(outfd, buf, n);
}
close (infd);
close (outfd);
unlink("fifop");//删除FIFO--直到该进程结束才真正删除FIFO,参考Ulink的用法
printf("fifor 读管道文件 success\n");
return 0;
}
- 测试非阻塞模式下打开FIFO的规则
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <sys/time.h>
//测试再FIFO存在的前提下,非阻塞模式下,按只写方式打开会失败
int main(int argc, char *argv[])
{
//建立管道文件
int outfd = 0;
mkfifo("fifop", 0644);
outfd = open("fifop", O_RDONLY | O_NONBLOCK);//非阻塞模式下按只读方式打开永远成功--返回描述符
if (outfd != -1)
{
printf("outfd : %d\n",outfd);
printf("infd open() O_RDONLY | O_NONBLOCK OK!!\n");
}
close(outfd);
//打开管道文件,准备写数据
outfd = open("fifop", O_WRONLY | O_NONBLOCK);//非阻塞模式下按只写方式打开会永远失败--返回-1
if (outfd == -1)
{
printf("infd open() O_WRONLY | O_NONBLOCK err..\n");
exit(0);
}
return 0;
}
- 常用来修改文件是否阻塞的函数模板
int fcntl(int fd, int cmd);
int fcntl(int fd, int cmd, long arg);
int flags = 0;
flags = fcntl(infd, F_GETFD);
flags = flags | O_NONBLOCK;
fcntl(infd, F_SETFD, flags);