Linux(高级编程)6————进程间通信2(命名管道)

通过匿名管道学习,我们对管道有了一些了解,下面我们将进行下一步学习管道,这次来学习一下匿名管道,关于管道这里就不在赘言了,直接上命名管道。

  • 命名管道:
    1.文件系统可见。
    2.命名管道是一类特殊类型文件(管道类型文件) 。
    3.命名管道可以应用于同一主机上所有的进程之间的通信(命名管道于匿名管道区别)。
  • 命名管道的创建:
    函数原型:
       #include <sys/types.h>
       #include <sys/stat.h>
       int mkfifo(const char *pathname, mode_t mode);

pathname:创建管道文件的名字
mode:文件权限
成功返回:返回0;失败返回:-1。
当管道创建好后,即可打开像操作文件那样操作管道了。

  • 命名管道的打开特性(只有命名管道有这个特性):
    1.如果以只读的方式打开命名管道 ,那么open函数将阻塞,直到其它进程以写的方式打开这个命名管道。
    2.如果以只写的方式打开命名管道 ,那么open函数将阻塞,直到其它进程以读的方式打开这个命名管道。
    3.如果以读写的方式打开命名管道,则不会阻塞(但这样会与管道特性半双工向违背,以读写的方式打开会出现问题的)。
    4读的特性:
    read特性:if(ret>0)读到数据;if(ret == 0)写端关闭。
    了解了命名管道的这些特性后就可以来做一个小案例练练手了:
    案例1:通过命名管道实现文件拷贝
    read_file.c:读取将要复制的文件并写入管道
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#define OUT "out"

/*
 *这段代码将文件out中的内容写入管道
 *
 */

int main(void)
{
	//1.创建管道
	if(mkfifo("F_PIPE",0664)<0)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo");
			exit(-1);
		}
	}
	//2.打开管道文件,打开要写入的文件
	int rfd = open(OUT,O_RDONLY|0664);
	int fd = open("F_PIPE",O_WRONLY);
	if(rfd<0 || fd<0)
	{
		perror("open");
		exit(-1);
	}
	//3.将要复制的文件写如管道
	ssize_t size = 0;
	char buf[1024];
	while((size = read(rfd,buf,sizeof(buf)-1))>0)
	{
		write(fd,buf,size);
	}
	printf("read file over! and write to F_PIPE\n");
	close(rfd);
	close(fd);
	return 0;
}


write_file.c:读取管道中的文件并写入目标文件

#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<string.h>
#include<errno.h>
#define OUT "out1"


/*
 *这段代码将管道中的文件读取并写入out1中
 */
int main(void)
{
	//1.创建管道
	if(mkfifo("F_PIPE",0664)<0)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo");
			exit(-1);
		}
	}
	//2.打开管道文件,打开要写入的文件
	int fd = open(OUT,O_CREAT|O_TRUNC|O_WRONLY|0664);
	int rfd = open("F_PIPE",O_RDONLY);
	if(rfd<0 || fd<0)
	{
		perror("open");
		exit(-1);
	}
	//3.将管道中的文件写入要复制的目标文件中
	ssize_t size = 0;
	char buf[1024];
	while((size = read(rfd,buf,sizeof(buf)-1))>0)
	{
		write(fd,buf,size);
	}
	printf("write file to out1!\n");
	close(rfd);
	close(fd);
	return 0;
}

运行效果展示:
在这里插入图片描述
当管道以只读或只写的方式打开时,会在open函数处阻塞(命名管道的打开规则)。
当我们在运行write_file时,管道的读写都被打开,阻塞就被解除。
接下来在运行write_file:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
当write_file运行管道读端被打开,read_file被解除阻塞!!!,文件复制完成!!!
案例2:通过命名管道实现server&client通信
server.c:

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

int main(void)
{
	//创建命名管道
	int rfd = -1;
	if((mkfifo("FIFO",0664))<0)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo!");
			exit(-1);
		}
	}
	//打开管道
	rfd = open("FIFO",O_RDONLY);
	if(rfd<0)
	{
		perror("open");
		exit(-1);
	}
	//循环的从命名管道中读取数据
	char buf[1024];
	while(1)
	{
		printf("please wait client enter....\n");
		int ret = read(rfd,buf,sizeof(buf)-1);
		buf[ret] = 0;
		if(ret>0)
			printf("client say:%s",buf);
		//当ret == 0时,说明写端关闭
		else if(ret == 0)
		{
			printf("client has quit,exit now!\n");
			exit(1);
		}
		else
		{
			perror("read!");
			exit(-1);
		}
	}
	close(rfd);
	return 0;
}

client.c:

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

int main(void)
{
	//创建命名管道
	int wfd = -1;
	if((mkfifo("FIFO",0664))<0)
	{
		if(errno != EEXIST)
		{
			perror("mkfifo!");
			exit(-1);
		}
	}
	//打开管道
	wfd = open("FIFO",O_WRONLY);
	if(wfd<0)
	{
		perror("open");
		exit(-1);
	}
	//循环的向命名管道中写入数据
	char buf[1024];
	while(1)
	{
		buf[0] = 0;
		printf("client enter# ");
		fflush(stdout);
		int ret = read(0,buf,sizeof(buf)-1);
		if(ret>0)
		{
			int size = write(wfd,buf,ret);
			if(size<0)
			{
				perror("write!");
				exit(-1);
			}
		}
		
	}
	close(wfd);
	return 0;
}
	

运行效果展示:
在这里插入图片描述
在这里插入图片描述
总结(命名管道与匿名管道的区别):

1.匿名管道由pipe创建并打开;命名管道由mkfifo创建,通过open打开。
2.匿名管道只能用于具有亲缘关系的进程间通信;命名管道可以应用于同一主机上的所有进程间通信。
3.命名管道文件系统可见。
4.命名管道是一个特殊类型(管道类型)文件。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值