进程通信之无名管道(双向通信)

2 篇文章 0 订阅
1 篇文章 0 订阅

进程间的通信方式有多种,最典型的无名管道,有名管道,二者的区别在于,无名管道适用于小规模,具有血缘关系的进程间通信,有名管道适用于小规模,无血缘和有血缘的进程间通信,管道的实质是:内核开辟的一端缓存区,一端读,一端写。两端默认都是阻塞状态想要实现双向通信就得创建两个管道,因为管道是半双工,写的时候就只能写,读的时候就只读,数据的传输是单向的,如果要实现双向,就必须增加一根管道。

下面直接上demo:

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

#define FILENAME "./file1"

void print_err(char* str)
{
		perror(str);
		exit(-1);
}

char buf[1024] = {0};

int main()
{
		//[0]: 读端
		//[1]: 写端
		int pipe1[2];  //管道1
		int pipe2[2];  //管道2
		int ret;
		ret = pipe(pipe1);
		if(ret == -1)  print_err("pipe1 fail:");

		ret = pipe(pipe2);
		if(ret == -1)  print_err("pipe2 fail:");

		pid_t pid = fork();

		if(pid > 0)
		{
				//父进程往管道1中写数据,关闭管道1的读端
				close(pipe1[0]);
				//父进程从管道2中读数据,关闭管道2的写端
				close(pipe2[1]);
				while(1)
				{
						int fd = open(FILENAME,O_RDWR|O_CREAT|O_APPEND,0664);
						if( fd == -1) print_err("open fail:");

						//写之前清空一下buf
						bzero(buf,sizeof(buf));

						//记录是谁发的消息
						strcpy(buf,"parent:");

						printf("parent write:\n");
						//从标准输入读取消息放到buf中
						read(0,buf+strlen("parent:"),sizeof(buf));
						//把消息写进文件做记录,后面可以直接打开文件查看各自发了哪些消息
						write(fd,buf,strlen(buf));

						//父进程往管道1里写数据,阻塞
						int ret = write(pipe1[1],buf,sizeof(buf));
						if(ret != -1) printf("send success!\n");

						//父进程从管道2里读数据,阻塞
						ret = read(pipe2[0],buf,sizeof(buf));
						//睡眠一段时间,场景需要
						usleep(10);
						if(ret == -1) print_err("parent process read fail:");
						else printf("parent receive success!\n");
				}
		}

		else if(pid == 0)
		{
				//子进程从管道1中读数据,关闭管道1的写端
				close(pipe1[1]);
				//子进程往管道2中写数据,关闭管道2的读端
				close(pipe2[0]);
				while(1)
				{
						//打开文件,以追加的方式
						int fd = open(FILENAME,O_RDWR|O_CREAT|O_APPEND,0664);
						if(fd == -1) print_err("open fail:");

						//子进程从管道1中读数据,阻塞
						int ret = read(pipe1[0],buf,sizeof(buf));
						if(ret == -1) print_err("child process read fail:");
						else printf("child receive success!\n");

						//读之后清空一下buf
						bzero(buf,sizeof(buf));

						//记录是谁发的消息
						strcpy(buf,"child:");

						printf("child write:\n");
						//从标准输入读取消息放到buf中
						read(0,buf+strlen("child:"),sizeof(buf));
						//把buf中的消息写进文件做记录
						write(fd,buf,strlen(buf));

						//子进程往管道2中写数据,阻塞
						ret = write(pipe2[1],buf,sizeof(buf));
						if(ret != -1) printf("send success!\n");
				}
		}
		return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值