linux进程间通信(命名管道)

在处理进程间通信的问题时,匿名管道只能在有亲缘关系的进程中进行通信。如何做到在任意两个进程之间通信,这就要用到命名管道。

命名管道也被称为fifo文件,它是一种特殊类型的文件,在文件系统中以文件的形式存在,它的行为和匿名管道类似。可以使用mkfifo函数来创建一个命名管道。

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

filename指定了文件名,mode指定了读写权限。

server.c:

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

#define _PATH_ "/tmp/file.tmp"
#define _SIZE_ 100

int main()
{
	int fd = open(_PATH_,O_RDONLY);
	if(fd < 0)
	{
		printf("open file error!\n");
		return 1;
	}
	char buf[_SIZE_];
	memset(buf,'\0',sizeof(buf));
	while(1)
	{
		int ret = read(fd,buf,sizeof(buf));
		if(ret <= 0)	
		{
			printf("read end or error!\n");
			break;
		}
		printf("%s\n",buf);
	}
	close(fd);
	return 0;
}
client.c:

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

#define _PATH_ "/tmp/file.tmp"
#define _SIZE_ 100

int main()
{
	int ret = mkfifo(_PATH_,0666|S_IFIFO);
	if(ret == -1)
	{
		printf("mkfifo error!\n");
		return 1;
	}
	int fd = open(_PATH_,O_WRONLY);
	if(fd < 0)
	{
		printf("open error!\n");
	}
	char buf[_SIZE_];
	memset(buf,'\0',sizeof(buf));
	while(1)
	{
		scanf("%s",buf);
		int ret = write(fd,buf,sizeof(buf)+1);
		if(ret < 0)
		{
			printf("write error!\n");
			break;
		}
	}
	close(fd);
	return 0;
}

运行结果:用client发送数据,用server接受数据

总结:

使用命名管道可以实现任意两个进程间的通信,文件系统中的路径名是全局的,各进程都可以访问,因此可以用文件系统中的路径名来标识一个ipc通道。

linux中一切皆文件,可以通过文件操作对命名管道进行使用。


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值