Linux进程间通信——管道

        管道是UNIX系统中最古老的进程间通信技术,古老意味着所有系统都支持,早期的管道是半双工通信,现有的系统管道是全双工通信,管道就是一种特殊的文件,数据在文件中是流动的,读取之后就自动消失,如果文件中没有数据则会阻塞。管道又细分为有名管道和匿名管道,本章咱们主打有名管道的实现。

既然是通信,那必然是双方,这里我们就用两个 .c 文件同时运行代表两个进程,让他们实现数据的交互。

因为是半双工通信,这里我们让第一个文件只写描述为进程A,让第二个文件只读描述为进程B,基本思路即:进程A负责创建和删除管道,同时也负责数据的输入,即写数据;进程B只负责把管道中传过来的数据读出来,也可以理解为这种通信有主次之分吧。

先来写进程A:

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

int main(int argc,const char* argv[])
{
	//创建管道文件
	if(mkfifo("fifo",0644))
	{
		perror("mkfifo");
		return -1;
	}
	//打开管道文件
	int fd=open("fifo",O_WRONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	//写数据
	char buf[256];
	while(1)
	{
		printf(">>>>>>>");
		fgets(buf,sizeof(buf),stdin);
		write(fd,buf,sizeof(buf));
		if(0==strncmp("quit",buf,4))

		{
			printf("通信结束\n");
			break;
		}
	}
	//关闭管道
	close(fd);
	usleep(1000);
	//删除管道
	unlink("fifo");
		
	return 0;
}

再来写进程B:

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

int main(int argc,const char* argv[])
{
	int fd=open("fifo",O_RDONLY);
	if(fd<0)
	{
		perror("open");
		return -1;
	}
	while(1)
	{
		char buf[256];
		read(fd,buf,sizeof(buf));
		printf("%s\n",buf);
		if(0==strncmp("quit",buf,4))
		{
			printf("通信结束\n");
			break;
		}
	}
	close(fd);
		
	return 0;
}

管道间要实现通信的话步骤还是比较固定的,只需要实现相关步骤代码即可实现通信。

over

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值