进程间通信之无名管道


前言

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


一、创建无名管道

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
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

free(me)

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

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

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

打赏作者

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

抵扣说明:

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

余额充值