达者为先 师者之意
进程间通信(IPC)介绍
进程间通信(IPC,InterProcess Communication)是指在不同进程之间传播或交换信息。
IPC的方式通常有管道(包括无名管道和命名管道)、消息队列、信号量、共享存储、Socket、Streams等。其中 Socket和Streams支持不同主机上的两个进程IPC。
1 管道
管道,通常指无名管道,是 UNIX 系统IPC最古老的形式。
特点:
- 它是半双工的(即数据只能在一个方向上流动),具有固定的读端和写端。
- 它只能用于具有亲缘关系的进程之间的通信(也是父子进程或者兄弟进程之间)。
- 它可以看成是一种特殊的文件,对于它的读写也可以使用普通的read、write 等函数。但是它不是普通的文件,并不属于其他任何文件系统,并且只存在于内存中。
每个进程的用户地址空间都是独立的,一般而言是不能互相访问的,但内核空间是每个进程都共享的,所以进程之间要通信必须通过内核。
函数原型:
#include <unistd.h>
int pipe(int fd[2]); // 返回值:若成功返回0,失败返回-1
返回值:若成功返回0,失败返回-1
当一个管道建立时,它会创建两个文件描述符:fd[0]为读而打开,fd[1]为写而打开。
这里表示创建一个匿名管道,并返回了两个描述符,一个是管道的读取端描述符 fd[0],另一个是管道的写入端描述符 fd[1]。注意,这个匿名管道是特殊的文件,只存在于内存,不存于文件系统中。
其实,所谓的管道,就是内核里面的一串缓存。从管道的一段写入的数据,实际上是缓存在内核中的,另一端读取,也就是从内核中读取这段数据。另外,管道传输的数据是无格式的流且大小受限。
看到这,你可能会有疑问了,这两个描述符都是在一个进程里面,并没有起到进程间通信的作用,怎么样才能使得管道是跨过两个进程的呢?
我们可以使用 fork 创建子进程,创建的子进程会复制父进程的文件描述符,这样就做到了两个进程各有两个「 fd[0] 与 fd[1]」,两个进程就可以通过各自的 fd 写入和读取同一个管道文件实现跨进程通信了。
管道只能一端写入,另一端读出,所以上面这种模式容易造成混乱,因为父进程和子进程都可以同时写入,也都可以读出。那么,为了避免这种情况,通常的做法是:
- 父进程关闭读取的 fd[0],只保留写入的 fd[1];
- 子进程关闭写入的 fd[1],只保留读取的 fd[0];
所以说如果需要双向通信,则应该创建两个管道。
到这里,我们仅仅解析了使用管道进行父进程与子进程之间的通信,但是在我们 shell 里面并不是这样的。
在 shell 里面执行 A | B命令的时候,A 进程和 B 进程都是 shell 创建出来的子进程,A 和 B 之间不存在父子关系,它俩的父进程都是 shell。
所以说,在 shell 里通过「|」匿名管道将多个命令连接在一起,实际上也就是创建了多个子进程,那么在我们编写 shell 脚本时,能使用一个管道搞定的事情,就不要多用一个管道,这样可以减少创建子进程的系统开销。
我们可以得知,对于匿名管道,它的通信范围是存在父子关系的进程。因为管道没有实体,也就是没有管道文件,只能通过 fork 来复制父进程 fd 文件描述符,来达到通信的目的。
注意:对于命名管道,它可以在不相关的进程间也能相互通信。因为命令管道,提前创建了一个类型为管道的设备文件,在进程里只要使用这个设备文件,就可以相互通信。
不管是匿名管道还是命名管道,进程写入的数据都是缓存在内核中,另一个进程读取数据时候自然也是从内核中获取,同时通信数据都遵循先进先出(先写后读)原则,不支持 lseek 之类的文件定位操作。
例子如下:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int fd[2];
pid_t pidl
char buf[128];
if(pipe(fd) == -1){
printf("creat pipe failed\n");
}
pid = fork();
if(pid < 0){
printf("creat child failed\n");
}
else if(pid > 0){
printf("this is father\n");
close(fd[0]);//关闭读
write(fd[1],"hello from father",strlen("hello from father"));//关闭写
wait(NULL);
}else{
printf("this is child\n");
close(fd[1]);//关闭写
read(fd[0],buf,128);
printf("read from father:%s\n",buf);
exit(0);
}
return 0;
}
输出结果:
this is father
this is child
read from father:hello from father
2 创建命名管道(FIFO)
FIFO,也称为命名管道,它是一种文件类型。
特点
- FIFO可以在无关的进程之间交换数据,与无名管道不同。
- FIFO有路径名与之相关联,它以一种特殊设备文件形式存在于文件系统中。
函数原型:
include <sys/stat.h>
// 返回值:成功返回0,出错返回-1
int mkfifo(const char *pathname, mode_t mode);
返回值:成功返回0,出错返回-1
其中的 mode 参数与open函数中的 mode 相同。一旦创建了一个 FIFO,就可以用一般的文件I/O函数操作它。
-
若没有指定O_NONBLOCK(默认),只读 open 要阻塞到某个其他进程为写而打开此 FIFO(只读 open程序会等到其他进程写内容进管道后才会执行下去,否则则一直等待其他进程写内容进管道,形成阻塞)。类似的,只写 open 要阻塞到某个其他进程为读而打开它。
-
若指定了O_NONBLOCK,则只读 open 立即返回。而只写 open 将出错返回 -1 如果没有进程已经为读而打开该 FIFO,其errno置ENXIO。
在前面的文章中我们已经对命名管道进行了简单介绍,我们知道命名管道的本质就是文件系统中的一个特殊设备文件,即管道文件(FIFO)。虽然它和其他普通文件的用途不同,但它的打开方式却和普通文件相同,都是通过调用open函数来打开。通常而言,打开管道文件的方式有以下几种:
- 只读且阻塞方式
open(const char *pathname, O_RDONLY); - 只读且非阻塞方式
open(const char *pathname, O_RDONLY | O_NONBLOCK); - 只写且阻塞方式
open(const char *pathname, O_WRONLY); - 只写且非阻塞方式
open(const char *pathname, O_WRONLY | O_NONBLOCK);
注:在这里我们需要注意一点,就是不能以 O_RDWR 方式打开管道文件,这种行为是未定义的。倘若有一个进程以读写方式打开了某个管道,那么该进程写入的数据又会被该进程本身读取,而管道一般只用于进程间的单向数据通信。
例子如下:
创建命名管道
#include <sys/types.h>
#include <sys/stat.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
mkfifo("./file",0600);//文件名,模式
return 0;
}
输出结果:
file1 文件会出现 prw,就是管道+可读+可写的意思
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
int ret = mkfifo("./file",0600);//文件名,模式
if(ret == 0){
printf("mkfifo success");
}
if(ret == -1){
printf("mkfifo failed");
perror("why");
}
return 0;
}
输出结果:失败的可能原因是文件可能存在
mkfifo failed
why:File exists
// 对以上两个例子进行了优化
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
if((mkfifo("./file",0600)==-1) && errno == EEXIST){
//文件名,模式
printf("mkfifo failed");
perror("why");
}else{
if(errno == EEXIST){
printf("file exisit\n");
}else{
printf("mkfifo success\n");
}
}
return 0;
}
输出结果:
没有file文件 mkfifo success
有file文件 file exisit
下面举例解释只读 open 要阻塞到某个其他进程为写而打开此 FIFO
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
if((mkfifo("./file",0600)==-1) && errno == EEXIST){
//文件名,模式
printf("mkfifo failed");
perror("why");
}
int fd = open("./file",O_RDONLY);
printf("open successful\n");
return 0;
}
Linux命令行:
gcc read.c -o read
运行./read
输出结果没有,为什么,
因为只读 open 要阻塞到某个其他进程为写而打开此 FIFO。
被阻塞了
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
int fd = open("./file",O_WRONLY);
printf("write open successful\n");
return 0;
}
接上面 如何让上面不阻塞
gcc write.c -o write
先在read.c的命令行窗口运行./read
后在write.c的命令行在运行./write
read.c的命令行窗口输出结果:open successful
write.c的命令行输出结果:write open successful
下面是读:
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
// int mkfifo(const char *pathname,mode_t mode);
int main()
{
char buf[20] = {
0};
if((mkfifo("./file",0600)==-1) && errno == EEXIST){
//文件名,模式
printf("mkfifo failed");
perror("why");
}
int fd = open("./file",O_RDONLY);
printf("open successful\n");
int n_read = read(fd,buf,20