Linux系统编程——进程间通信:命名管道(FIFO)

本文详细介绍了命名管道的概念,与无名管道的区别,包括命名管道的创建、默认操作、非堵塞标志下的操作及特点。文章通过示例代码演示了命名管道的创建、读写操作以及非堵塞模式下的应用,帮助读者全面了解命名管道的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

命名管道的概述

无名管道,因为没有名字,仅仅能用于亲缘关系的进程间通信(很多其它详情。请看《无名管道》)。为了克服这个缺点。提出了命名管道(FIFO)。也叫有名管道、FIFO 文件。


命名管道(FIFO)不同于无名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,这样。即使与 FIFO 的创建进程不存在亲缘关系的进程,仅仅要可以訪问该路径。就行彼此通过 FIFO 相互通信,因此,通过 FIFO 不相关的进程也能交换数据


命名管道(FIFO)和无名管道(pipe)有一些特点是同样的,不一样的地方在于:

1、FIFO 在文件系统中作为一个特殊的文件而存在,但 FIFO 中的内容却存放在内存中。

2、当使用 FIFO 的进程退出后。FIFO 文件将继续保存在文件系统中以便以后使用。


3、FIFO 有名字。不相关的进程能够通过打开命名管道进行通信。


命名管道的创建

所需头文件:

#include <sys/types.h>

#include <sys/stat.h>


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

功能:

命名管道的创建。

參数:

pathname: 普通的路径名,也就是创建后 FIFO 的名字。

mode: 文件的权限,与打开普通文件的 open() 函数中的 mode 參数同样,相关说明请点此链接

返回值:

成功:0
失败:假设文件已经存在。则会出错且返回 -1。


演示样例代码例如以下:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(int argc, char *argv[])
{
	int ret;
	
	ret = mkfifo("my_fifo", 0666); // 创建命名管道
	if(ret != 0){	// 出错
		perror("mkfifo");
	}
	
	return 0;
}

执行结果例如以下:



命名管道的默认操作

后期的操作。把这个命名管道当做普通文件一样进行操作:open()、write()、read()、close()。

可是。和无名管道一样,操作命名管道肯定要考虑默认情况下其堵塞特性


以下验证的是默认情况下的特点。即 open() 的时候没有指定非堵塞标志( O_NONBLOCK )。

1)

open() 以仅仅读方式打开 FIFO 时,要堵塞到某个进程为写而打开此 FIFO
open() 以仅仅写方式打开 FIFO 时,要堵塞到某个进程为读而打开此 FIFO。

简单一句话,仅仅读等着仅仅写,仅仅写等着仅仅读,仅仅有两个都运行到,才会往下运行。


仅仅读端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666);
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	printf("before open\n");
	fd = open("my_fifo", O_RDONLY);//等着仅仅写
	if(fd < 0)
	{
		perror("open fifo");
	}
	
	printf("after open\n");
	
	return 0;
}

仅仅写端代码例如以下

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666);
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	printf("before open\n");
	fd = open("my_fifo", O_WRONLY); //等着仅仅读
	if(fd < 0)
	{
		perror("open fifo");
	}
	
	printf("after open\n");
	
	return 0;
}

大家开启两个终端。分别编译以上代码,读端程序和写端程序各自执行,例如以下图,大家自行验证其特点,由于光看结果图是没有效果,大家须要分析其执行过程是怎样变化。



假设大家不想在 open() 的时候堵塞。我们能够以可读可写方式打开 FIFO 文件。这样 open() 函数就不会堵塞。

// 可读可写方式打开
int fd = open("my_fifo", O_RDWR);

2)假如 FIFO 里没有数据,调用 read() 函数从 FIFO 里读数据时 read() 也会堵塞。这个特点和无名管道是一样的。


写端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666);//创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	printf("before open\n");
	fd = open("my_fifo", O_WRONLY); //等着仅仅读
	if(fd < 0)
	{
		perror("open fifo");
	}
	printf("after open\n");
	
	printf("before write\n");
	// 5s后才往命名管道写数据,没数据前。读端read()堵塞
	sleep(5);
	char send[100] = "Hello Mike";
	write(fd, send, strlen(send));
	printf("write to my_fifo buf=%s\n", send);
	
	return 0;
}

读端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666); //创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	printf("before open\n");
	fd = open("my_fifo", O_RDONLY);//等着仅仅写
	if(fd < 0)
	{
		perror("open fifo");
	}
	printf("after open\n");
	
	printf("before read\n");
	char recv[100] = {0};
	
	//读数据,命名管道没数据时会堵塞。有数据时就取出来
	read(fd, recv, sizeof(recv)); 
	printf("read from my_fifo buf=[%s]\n", recv);
	
	return 0;
}

请依据下图自行编译执行验证:



3)通信过程中若写进程先退出了。就算命名管道里没有数据,调用 read() 函数从 FIFO 里读数据时不堵塞;若写进程又又一次执行,则调用 read() 函数从 FIFO 里读数据时又恢复堵塞。


写端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666); // 创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	fd = open("my_fifo", O_WRONLY); // 等着仅仅读
	if(fd < 0)
	{
		perror("open fifo");
	}
	
	char send[100] = "Hello Mike";
	write(fd, send, strlen(send));	//写数据
	printf("write to my_fifo buf=%s\n",send);
	
	while(1); // 堵塞,保证读写进程保持着通信过程
	
	return 0;
}

读端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666);// 创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	fd = open("my_fifo", O_RDONLY);// 等着仅仅写
	if(fd < 0)
	{
		perror("open fifo");
	}
	
	while(1)
	{
		char recv[100] = {0};
		read(fd, recv, sizeof(recv)); // 读数据
		printf("read from my_fifo buf=[%s]\n",recv);
		sleep(1);
	}
	
	return 0;
}

请依据下图自行编译执行验证:



5)通信过程中,读进程退出后,写进程向命名管道内写数据时,写进程也会(收到 SIGPIPE 信号)退出。


6)调用 write() 函数向 FIFO 里写数据,当缓冲区已满时 write() 也会堵塞。


5) 6)这两个特点和无名管道是一样的,这里不再验证。详情请看《无名管道》


命名管道非堵塞标志操作

命名管道能够以非堵塞标志(O_NONBLOCK)方式打开:

fd = open("my_fifo", O_WRONLY|O_NONBLOCK);
fd = open("my_fifo", O_RDONLY|O_NONBLOCK);


非堵塞标志(O_NONBLOCK)打开的命名管道有下面特点:
1、先以仅仅读方式打开。假设没有进程已经为写而打开一个 FIFO, 仅仅读 open() 成功。而且 open() 不堵塞。


2、先以仅仅写方式打开。假设没有进程已经为读而打开一个 FIFO。仅仅写 open() 将出错返回 -1。


3、read()、write() 读写命名管道中读数据时不堵塞。



请依据下面代码自行编译执行验证。


写端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666); // 创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	// 仅仅写并指定非堵塞方式打开
	fd = open("my_fifo", O_WRONLY|O_NONBLOCK);
	if(fd<0)
	{
		perror("open fifo");
	}
	
	char send[100] = "Hello Mike";
	write(fd, send, strlen(send));
	printf("write to my_fifo buf=%s\n",send);
	while(1);
	
	return 0;
}

读端代码例如以下:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	ret = mkfifo("my_fifo", 0666); // 创建命名管道
	if(ret != 0)
	{
		perror("mkfifo");
	}
	
	// 仅仅读并指定非堵塞方式打开
	fd = open("my_fifo", O_RDONLY|O_NONBLOCK);
	if(fd < 0)
	{
		perror("open fifo");
	}
	
	while(1)
	{
		char recv[100] = {0};

		read(fd, recv, sizeof(recv));
		printf("read from my_fifo buf=[%s]\n",recv);
		sleep(1);
	}
	
	return 0;
}

本教程演示样例代码下载请点此处。

转载于:https://www.cnblogs.com/mengfanrong/p/5347027.html

内容概要:论文提出了一种基于空间调制的能量高效分子通信方案(SM-MC),将传输符号分为空间符号和浓度符号。空间符号通过激活单个发射纳米机器人的索引来传输信息,浓度符号则采用传统的浓度移位键控(CSK)调制。相比现有的MIMO分子通信方案,SM-MC避免了链路间干扰,降低了检测复杂度并提高了性能。论文分析了SM-MC及其特例SSK-MC的符号错误率(SER),并通过仿真验证了其性能优于传统的MIMO-MC和SISO-MC方案。此外,论文还探讨了分子通信领域的挑战、优势及相关研究工作,强调了空间维度作为新的信息自由度的重要性,并提出了未来的研究方向和技术挑战。 适合人群:具备一定通信理论基础,特别是对纳米通信和分子通信感兴趣的科研人员、研究生和工程师。 使用场景及目标:①理解分子通信中空间调制的工作原理及其优势;②掌握SM-MC系统的具体实现细节,包括发射、接收、检测算法及性能分析;③对比不同分子通信方案(如MIMO-MC、SISO-MC、SSK-MC)的性能差异;④探索分子通信在纳米网络中的应用前景。 其他说明:论文不仅提供了详细的理论分析和仿真验证,还给出了具体的代码实现,帮助读者更好地理解和复现实验结果。此外,论文还讨论了分子通信领域的标准化进展,以及未来可能的研究方向,如混合调制方案、自适应调制技术和纳米机器协作协议等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值