进程间通信之无名管道


前言

本文记录的是进程间通信之无名管道


一、创建无名管道

pipe ( 建立无名管道 )
头文件:
#include <unistd.h>

			定义函数:
				int pipe(int pipefd[2]);

			参数分析:
				pipefd[0] --> 接收管道描述符
				pipefd[1] --> 写管道描述符

			返回值:
				若成功则返回 0, 
				否则返回-1, 错误原因存于 errno 中.
#include <stdio.h>
 #include <unistd.h>

int main(int argc, char const *argv[])
{
	int pipe_fd[2];	//两个文件描述符
	//int pipe(int pipefd[2]);

	int ret = pipe( pipe_fd );
	if ( ret < 0 ) {
		printf("create pipe failure\n");
		perror("create pipe error");
		return -1;
	} 
	printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
	return 0;
}

结果显示:
在这里插入图片描述

二、尝试在无名管道中读取

1.引入库

把"hello linux!" 写进无名管道,然后通过read函数读取出来。

#include <stdio.h>
 #include <unistd.h>

int main(int argc, char const *argv[])
{
	char buffer[20];
	int pipe_fd[2];	//两个文件描述符
	//int pipe(int pipefd[2]);

	int ret_pipe = pipe( pipe_fd );
	if ( ret_pipe < 0 ) {
		printf("create pipe failure\n");
		perror("create pipe error");
		return -1;
	} 
	printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
	
	//把hello linux!写入无名管道
	int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
	if( ret_write < 0 ) {
		printf("写入失败!\n");
		perror("write error");
		return -1;
	}

	read(pipe_fd[0], buffer, sizeof(buffer) );
	printf("%s\n", buffer);
	

	close(pipe_fd[0]);
	close(pipe_fd[1]);

	return 0;
}


结果显示:
在这里插入图片描述

2.验证无名管道的内容只能读一次

代码如下(示例):

#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	char buffer[20];
	int pipe_fd[2];	//两个文件描述符
	//int pipe(int pipefd[2]);

	int ret_pipe = pipe( pipe_fd );
	if ( ret_pipe < 0 ) {
		printf("create pipe failure\n");
		perror("create pipe error");
		return -1;
	} 
	printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
	
	//把hello linux!写入无名管道
	int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
	if( ret_write < 0 ) {
		printf("写入失败!\n");
		perror("write error");
		return -1;
	}

	//The first read
	read(pipe_fd[0], buffer, sizeof(buffer) );
	printf("%s\n", buffer);
	
	//The second read void *memset(void *s, int c, size_t n);
	memset(buffer, 0, 20);		//前20个字节清零
	read(pipe_fd[0], buffer, sizeof(buffer) );
	printf("%s\n", buffer);


	close(pipe_fd[0]);
	close(pipe_fd[1]);

	return 0;
}


结果显示:
在这里插入图片描述

从执行结果来看,第二次读取无名管道的内容的时候,并没有成功,终端一直在执行着。

三、测试无名管道能写入多少个字节

/************************************************************************
*
* 文件名:pipe_03.c
*
* 功能:测试无名管道能写入多少个字节
*
* 创建人:LiZhenhao
*
* 时间:2021年10月20日19:02:56
*
* 版本号:3.0
*
* 结果:无名管道最多能写入65507个字节
*
************************************************************************/


#include <stdio.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
	char buffer[20];
	int pipe_fd[2];	//两个文件描述符
	int i = 0;

	//创建无名管道
	int ret_pipe = pipe( pipe_fd );
	if ( ret_pipe < 0 ) {
		printf("create pipe failure\n");
		perror("create pipe error");
		return -1;
	} 
	printf("create pipe success! pipe_fd[0] = %d, pipe_fd[1] = %d\n", pipe_fd[0], pipe_fd[1]);
	
	//把hello linux!写入无名管道
	int ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
	printf("ret_write = %d\n", ret_write);
	while( ret_write != -1 ) {
		ret_write = write(pipe_fd[1], "hello linux!", sizeof("hello linux!") );
		i++;
		printf("total = %d\n", i);
	}


	//关闭文件描述符
	close(pipe_fd[0]);
	close(pipe_fd[1]);

	return 0;
}


结果显示:在这里插入图片描述

从上面界面的结果来看,执行次数是5039次,然后写入的字节一次总共写了13个字节,通过计算13*5039 = 65,507个字节

四、测试两个进程是否能通信

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


int main(int argc, char const *argv[])
{
	int status;
	//创建无名管道
	int pipefd[2];
	if( 0 == pipe(pipefd) ) {
		printf("创建无名管道成功!\n");
	}

	//创建子进程
	pid_t pid = fork();

	if(pid > 0) {
		printf("我是父进程,ID:%d\n", getpid());
		ssize_t ret = write (pipefd[1], "hello linux!", strlen("hello linux!"));
		printf("成功写入%ld个字节进无名管道\n", ret);
		wait(&status);
		if( WIFEXITED(status) ) {
			printf("子进程正常退出,退出值为:%d\n", WEXITSTATUS(status));
		}
		sleep(1);
	} else if(0 == pid){
		printf("我是子进程,ID:%d\n", getpid());
		char re_buf[20] = {0};
		ssize_t ret = read(pipefd[0], re_buf, sizeof(re_buf));
		printf("成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, re_buf);
		//exit(1);
	}


	return 0;
}




结果显示:
在这里插入图片描述

从上面结果分析:父子进程同时进行,父进程写入数据到无名管道,子进程成功从无名管道读取数据

五、父子进程通信案例

/* 实习父子进程通信  */
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>

//char* msg_buf = NULL;


int main(int argc, char const *argv[])
{
	int status;
	//msg_buf = (char*)malloc(sizeof(char) * 1024);
	char msg_buf[1024];

	//创建无名管道
	int pipefd[2];
	if( pipe(pipefd) < 0) {
		printf("创建无名管道失败!\n");
		perror("create pipe error");
		return -1;
	}

	//创建子进程
	pid_t pid = fork();

	if(pid > 0) {	//父进程
		printf("我是父进程,ID:%d\n", getpid());
		usleep(1000);
		while(1) {
			
			printf("父进程请输入:\n");
			memset(msg_buf, 0, 1024);
			fgets(msg_buf, 1024, stdin);
			ssize_t ret = write (pipefd[1], msg_buf, strlen(msg_buf)+1);
			printf("父进程成功写入%ld个字节进无名管道\n", ret);
			usleep(1000);

			ret = read(pipefd[0], msg_buf, sizeof(msg_buf));
			printf("父进程成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, msg_buf);
			
		}

		wait(&status);

		
		close(pipefd[0]);
		close(pipefd[1]);
	} else if(0 == pid){	//子进程
		printf("我是子进程,ID:%d\n", getpid());		
		ssize_t ret;

		while(1) {
			ret = read(pipefd[0], msg_buf, 1024);
			printf("子进程成功进从无名管道读取%ld个字节, 内容是:%s\n", ret, msg_buf);
			

			printf("子进程请输入:\n");
			memset(msg_buf, 0, 1024);
			fgets(msg_buf, 1024, stdin);
			ssize_t ret = write (pipefd[1], msg_buf, strlen(msg_buf) + 1);
			printf("子进程成功写入%ld个字节进无名管道\n", ret);
			usleep(1000);
			
		}

		close(pipefd[0]);
		close(pipefd[1]);
		//exit(1);
	}


	return 0;
}






结果如图所示:
在这里插入图片描述

父进程和子进程在写完内容都无名管道以后,要usleep等待一下,不然会出现父进程写,父进程读这种情况。同理,子进程也一样。


总结

1、无名管道的通信方式是要有血缘关系的进程才能使用,父子进程对同一管道的通信
2、要在创建父子进程fork()之前,先创建无名管道,不然的话,父进程写完数据到无名管道,子进程会发生读阻塞。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
### 回答1: 无名管道(Anonymous Pipe)是操作系统中用于实现进程间通信(IPC)的一种机制,Windows操作系统也支持使用无名管道进行进程间通信。 在Windows操作系统中,无名管道是一种特殊类型的文件,只能用于相关进程间通信,无法用于不相关进程间通信无名管道通常是单向的,有一个读取端(Read end)和一个写入端(Write end),一个进程可以往管道的写入端写入数据,另一个进程可以从管道的读取端读取数据。 无名管道常用于兄弟进程间通信(如父子进程)或者由同一个进程创建的两个进程间通信。 使用无名管道进行进程间通信的步骤如下: 1. 创建管道:调用CreatePipe函数创建一个无名管道,返回读取端和写入端句柄。 2. 创建进程:使用CreateProcess函数创建需要进行通信进程。 3. 设置进程的标准输入输出:使用SetStdHandle函数将管道的读取端和写入端句柄分别设置为所创建进程的标准输入和标准输出。 4. 进程通信:通过写入端句柄向管道写入数据,然后通过读取端句柄从管道读取数据。 在这个过程中,写入端和读取端可以位于同一个进程中,也可以位于不同的进程中。写入端写入的数据会被读取端读取,实现了进程间的通信无名管道可以用于传输任意类型的数据,包括文本、二进制等。只要进程通过读取端和写入端的兼容接口进行数据的读写操作,就可以实现进程间的通信。 总之,使用无名管道是一种简单有效的方式来实现Windows操作系统中进程间的通信,通过读取端和写入端的数据传输,可以实现数据的共享和交换,满足进程间通信的需求。 ### 回答2: 无名管道(Anonymous Pipes)是一种在Windows系统中用于实现进程间通信的机制。它通常用于在父子进程之间或者兄弟进程之间进行数据传输。 在使用无名管道之前,我们需要创建一个匿名的管道对象,它定义了一个读取端和一个写入端。父进程可以通过创建管道对象,并将写入端传递给子进程,来实现进程间通信。子进程可以通过读取从父进程写入端传来的数据来获取信息。 无名管道的使用步骤如下: 1. 使用CreatePipe函数创建一个无名管道,该函数返回两个句柄(一个读句柄和一个写句柄),分别用于读取和写入管道中的数据。 2. 通常,我们会在父进程中调用CreateProcess函数创建一个子进程。子进程会继承父进程的句柄。 3. 在创建子进程后,父进程可以关闭子进程中不需要的句柄(写句柄),子进程可以关闭父进程中不需要的句柄(读句柄)。 4. 当父进程想要向子进程发送数据时,它可以使用WriteFile函数来向写入端写入数据。 5. 子进程可以使用ReadFile函数从读取端读取父进程写入的数据。 通过这种方式,父进程和子进程之间可以进行双向的通信。 需要注意的是,无名管道只适用于具有亲密关系(比如父子进程)或具有共同祖先(比如兄弟进程)的进程之间。此外,无名管道是半双工的,意味着只能在一个方向上传递数据。如果需要实现双向通信,可以使用两个无名管道来分别实现。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

free(me)

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值