学习笔记——进程间通信之管道详解

重要概念!!!

进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。

IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。

管道

管道,顾名思义,是进程之间数据流输入输出的通道,我们称pipe管道,通常是把一个进程的输出通过管道连接到另一个进程的输入。管道又分为两种:无名管道(也就是我们说的管道)和FIFO(又称命名管道),FIFO和管道都是半双工,单向的数据通信。接下来看看二者有怎样的异同点。

管道(无名管道)

管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。

特点:

  1. 它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。

  2. 它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。

  3. 它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中

原型:

NAME
       pipe, pipe2 - create pipe

SYNOPSIS
       #include <unistd.h>

       int pipe(int pipefd[2]);

返回值:
若成功返回0

失败返回-1


当一个管道建立时,它会创建两个文件描述符fd[0]和d[1],其中fd[0]为读而打开,fd[1]为写而打开,关闭管道只需将这两个文件描述符关闭即可。

在这里插入图片描述

单个进程中的管道几乎没有任何用处。所以,通常调用 pipe 的进程接着调用 fork,这样就创建了父进程与子进程之间的 IPC 通道。

若要数据流从父进程流向子进程,则关闭父进程的读端(fd[0])与子进程的写端(fd[1]);反之,则可以使数据流从子进程流向父进程。

如下爬的图:
在这里插入图片描述

看看这个demo:

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

int main(){
	
	//int pipe(int pipefd[2]);
	int fd[2];
	pid_t pid;
	char *buf = "hello from father";	
	char readBuf[128];
	
	if(pipe(fd) == -1){	
		printf("create pipe failed!\n");
	}
	pid = fork();
	if(pid < 0){
		printf("fork failed!\n");
	}else if(pid > 0){	
		sleep(3);	
		printf("this is father\n");
		close(fd[0]);	
//		ssize_t write(int fd, const void *buf, size_t count);
		write(fd[1],buf,strlen(buf));
		wait();
	}else{
		printf("this is child\n");
		close(fd[1]);
//		ssize_t read(int fd, void *buf, size_t count);
		read(fd[0],readBuf,strlen(buf));
//		printf("%s\n",readBuf);
		printf("%s\n",readBuf);
		exit(0);
		
	}
	
	return 0;
}

我们通过pipe创建管道,搭配fork使用,使用fork就说明父进程和子进程是依据系统调度进行的,不知道哪个先哪个后,所以当先执行子进程时,readbuf里面没有数据,会自己阻塞,将资源分配给父进程,父进程中write之后再去子进程中read

这里注意的是,我们在读的时候要关闭写,在写的时候要关闭读,如下图
在这里插入图片描述

FIFO(命名通道)

FIFO,也称为命名管道,它是一种文件类型。

特点:

  1. FIFO可以在无关的进程之间交换数据,与无名管道不同。

  2. FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。(这里是特殊的设备文件形式,而管道是存在内存中。。注意区别)

原型:

NAME
       mkfifo - make a FIFO special file (a named pipe)

SYNOPSIS
       #include <sys/types.h>
       #include <sys/stat.h>

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

pathname就是文件路径, mode 参数与open函数中的 mode 相同(可以看看里面的open函数mode学习笔记——Linux的open、write、read、lseek、close函数)。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。

返回值

成功返回0
出错返回-1

当 open 一个FIFO时,是否设置非阻塞标志(O_NONBLOCK)的区别:

若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO。类似的,只写 open 要阻塞到某个其他进程为读而打开它。可以理解为,我这边的只读进程阻塞了,进行不下去了,需要另一个只写进程来配合打开FIFO。

若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。

我们看看demo是怎么使用的。。

read.c文件

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>    
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(){
	
	int fd;
	char buf[128] = {0};
	int cnt = 0;

	//int mkfifo(const char *pathname, mode_t mode);
	if((mkfifo("./file",0600) == -1) && (errno == EEXIST)){
		printf("create fifo failed!\n");
		perror("because");
	}
//      int open(const char *pathname, int flags);
	fd = open("./file",O_RDONLY);
	if(fd < 0){
		printf("open fifo failed!\n");
		exit(-1);
	}else{
		printf("open successful!\n");
	}
//      ssize_t read(int fd, void *buf, size_t count);
	while(1){	
		int nread = read(fd,buf,50);
		printf("read %d byte,context: %s\n",nread,buf);
		cnt++;
		if(cnt == 5){
			break;
		}
	}
	close(fd);
	return 0;
}
write.c文件


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>    
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
int main(){
	
	int fd;
	char *str = "message write by son;read for father";
	int cnt;
//      int open(const char *pathname, int flags);
	fd = open("./file",O_WRONLY);
	if(fd < 0){
		printf("open fifo failed!\n");
		exit(-1);
	}else{
		printf("open successful!\n");
	}
	while(1){
		sleep(2);	
		int nwrite = write(fd,str,strlen(str));
		if(nwrite < 0){
			printf("write failed!\n");
			exit(-1);
		}
		
		cnt++;
		if(cnt == 5){
			break;
		}	
	}
	close(fd);
	return 0;
}

这里我们模拟通信,在read.c文件创建管道只读打开,在write.c文件中只写打开,当我们输入执行read的时候,会阻塞,在另一命令框中输入write执行才会输出数据流read

注意:read文件中EEXIST实际上是mkfifo的error值,当error等于EEXIST表明这个file文件流已经存在了,这点也可以在perror中查看

结果如下:
为了不刷屏,设置了sleep和次数。
在这里插入图片描述


!!!这里记录下小失误

read中buf没有分配空间,不能使用strlen,可以使用sizeof或者直接数字大小
read(fd,buf,50);

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

石子君

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

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

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

打赏作者

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

抵扣说明:

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

余额充值