Linux--进程间通讯--FIFO(open打开)

1. 什么是FIFO

FIFO命名管道,也叫有名管道 ,来区分管道pipe 。管道pipe只能用于有血缘关系的进程间通信,但通过FIFO可以实现不相关的进程之间交换数据。FIFO是Linux基础文件类型中的一种,但是FIFO文件在磁盘上没有数据块,仅仅用来标识内核中的一条通道。各进程可以打开这个文件进行read/write操作,实际上是在读写内核通道,这样就实现了进程间通信。

创建FIFO的方式:

使用命令创建:mkfifo 管道名,可以理解为创建一个管道伪文件。

使用库函数创建:mkfifo()函数,并且一旦使用mkfifo()创建了一个FIFO,就可以使用open来打开它,常见的文件I/O函数都可用于FIFO。如:close、read、write、unlink等。

实际上,创建一个FIFO命名管道的时候,内核会为FIFO(伪)文件开辟一个缓冲区,操作FIFO文件就相当于操作这个缓冲区,以此来实现进程间的通信,这种通信实际上就是文件读写的操作来实现的。(可以把FIFO理解为一个文件,一个进程向该文件写数据,另一个进程从该文件中读书数据,前提是两个进程读写的是同一个FIFO文件才能实现通信)

2.当open一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:

若没有执行O_NONBLOCK(默认),只读open要阻塞到某个其他进程为写而打开此FIFO。类似的,只写open要阻塞到其他进程为读而打开它。

若指定了O_NONBLOCK,则只读open立即返回。而只写open将出错返回-1,如果没有进程已经为读而打开该FIFO,其errno置ENXIO。

3.代码:

创建两个进程,一个进程向FIFO 数据,一个进程从FIFO数据。

write:

cpp 复制代码
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>



//       int mkfifo(const char *pathname, mode_t mode);
//   ssize_t write(int fd, const void *buf, size_t count);



int main()
{

	int cnt = 0;
	char *str = "message from fifo";

	int fd = open("./ipc-file",O_WRONLY);
	printf("write open success\n");

	while(1){


		write(fd,str,strlen(str));
		sleep(2);
		cnt++;
		if(cnt == 5){
			break;



		}


	}

	close(fd);

	return 0;

}

read:

cpp 复制代码
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>

//       int mkfifo(const char *pathname, mode_t mode);

int main()
{
	int cnt = 0;
	char buf[30]={0};	

	if(mkfifo("./ipc-file",0700)==-1 && errno != EEXIST){
		printf("mkfifo fail");
		perror("why");
	}	


	int fd = open("./ipc-file",O_RDONLY);
	printf("open success\n");


	while(1){

		int nread = read(fd,buf,30);
		printf("read %d byte from fifo/n,context :%s\n",nread,buf);
		cnt++;
		if(cnt == 3){

			break;


		}
	}

	close(fd);
	return 0;

}

结果:

相关推荐
Wy_编程3 分钟前
Linux-文本搜索工具grep
linux·运维·服务器
xujiangyan_6 分钟前
linux的sysctl系统以及systemd系统。
linux·服务器·网络
Lovyk8 分钟前
Linux Shell 常用操作与脚本示例详解
linux·运维·服务器
yuanpan4 小时前
ubuntu系统上的conda虚拟环境导出方便下次安装
linux·ubuntu·conda
AOwhisky5 小时前
Linux 文本处理三剑客:awk、grep、sed 完全指南
linux·运维·服务器·网络·云计算·运维开发
Gavin_9155 小时前
从零开始部署经典开源项目管理系统最新版redmine6-Linux Debian12
linux·ruby on rails·开源·debian·ruby·redmine
花小璇学linux6 小时前
imx6ull-驱动开发篇31——Linux异步通知
linux·驱动开发·嵌入式软件
shelutai6 小时前
ubuntu 编译ffmpeg6.1 增加drawtext,libx264,libx265等
linux·ubuntu·ffmpeg
runfarther6 小时前
搭建LLaMA-Factory环境
linux·运维·服务器·python·自然语言处理·ai编程·llama-factory
hello_ world.7 小时前
RHCA10NUMA
linux