有名管道也称命名管道
命名管道(FIFO)和管道基本相同,但也有一些显著的不同
其特点是
4.FIFO在文件系统中作为一个特殊的文件而存在并且在文件系统中可见,所以有名管道可以实现不相关进程间通信,但FIFO中的内容却存放在内存中
7.当使用FIFO的进程退出后,FIFO文件将继续保存在文件系统中以便以后使用
8.FIFO有名字,不相关的进程可以通过打开命名管道进行通信
通过有名管道所创建的进程间通信方式,最终会在本地给你创建一个p类型的文件
就是一个管道文件。
linux中文件的类型有7中
b c d - lsp
b 代表块设备文件
c 表示字符设备文件
d 表示目录文件
- 表示普通文件
l 表示软连接文件
s 表示套接字文件
p 表示管道文件
有名管道的创建
方法1:通过shell命令mkfifo创建有名管道
mkfifo + 文件名
方法2:使用函数mkfifo
#include <sys/types.h>
#include <sys/stat.h>
int mkfifo(const char *pathname, mode_t mode);
功能
创建一个有名管道,产生一个本地文件系统可见的文件pathname
参数
path:name 有名管道创建后生成的文件,可以带路径
mode:管道文件的权限,一般通过八进制数设置即可,例如0664
返回值
成功:返回0
失败:返回-1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main()
{
//通过mkfifo函数创建有名管道
if(mkfifo("fifo_filt", 0664) == -1)
{
//printf("errno = %d\n",errno); 17
//如果管道文件已经存在,不需要报错退出,直接使用即可,所以需要在
//错误输出之前把因为文件存在的错误排除
if( errno != EEXIST)
{
perror("fail to mkfifo");
exit(1);
}
}
return 0;
}
~
执行结果:
有名管道的基本读写操作
由于有名管道在本地创建了一个文件,所以系统调用的IO函数基本都可以对有名管道进行操作,但是不能使用lseek修改管道文件的偏移量
注意:有名管道创建的本地的文件只是起到标识作用,真正有名管道实现进程间通信还是在内核空间开辟内存,所以本地产生的文件只是一个标识,没有其他作用,对本地管道文件的操作,实质就是对内核空间的操作
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
int main()
{
//通过mkfifo函数创建有名管道
if(mkfifo("fifo_filt", 0664) == -1)
{
if( errno != EEXIST)
{
perror("fail to mkfifo");
exit(1);
}
}
//对有名管道进行操作
//管道后写入的数据会保存在之前写入数据的后面,不会替换
//如果管道中没有数据了,读操作会阻塞
//通过open函数打开管道文件并得到文件描述符
int fd;
fd = open("fifo_filt", O_RDWR);
if(fd == -1)
{
perror("fail to open");
exit(1);
}
//通过write函数向有名管道中写入数据
if(write(fd, "hello world", strlen("hello world")) == -1)
{
perror("fail to write");
exit(1);
}
//通过read函数读取管道中的数据
char buf[32] = "";
if(read(fd, buf, sizeof(buf)) == -1)
{
perror("fail to read");
exit(1);
}
printf("buf = [%s]\n", buf);
//使用close函数关闭文件描述符
close(fd);
return 0;
}
有名管道实现进程间通信
由于有名管道在本地创建了一个管道文件,所以不相关的进程间也可以实现通信
send.c
#