[go: up one dir, main page]

CN113495795A - Inter-process communication method and related equipment - Google Patents

Inter-process communication method and related equipment Download PDF

Info

Publication number
CN113495795A
CN113495795A CN202010262408.1A CN202010262408A CN113495795A CN 113495795 A CN113495795 A CN 113495795A CN 202010262408 A CN202010262408 A CN 202010262408A CN 113495795 A CN113495795 A CN 113495795A
Authority
CN
China
Prior art keywords
shared memory
mapping
memory
data
control information
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
CN202010262408.1A
Other languages
Chinese (zh)
Other versions
CN113495795B (en
Inventor
叶建平
王超
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Huawei Technologies Co Ltd
Original Assignee
Huawei Technologies Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Huawei Technologies Co Ltd filed Critical Huawei Technologies Co Ltd
Priority to CN202010262408.1A priority Critical patent/CN113495795B/en
Publication of CN113495795A publication Critical patent/CN113495795A/en
Application granted granted Critical
Publication of CN113495795B publication Critical patent/CN113495795B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/54Interprogram communication
    • G06F9/544Buffers; Shared memory; Pipes
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/54Indexing scheme relating to G06F9/54
    • G06F2209/543Local

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Multi Processors (AREA)

Abstract

本申请提供一种进程间通信方法及相关设备,方法包括:第一进程创建第二进程以及第三进程之间的第一共享内存;所述第二进程将第一数据写入所述第一映射区域,并根据所述第一映射关系将所述第一数据从所述第一映射区域映射至所述第一共享内存,所述第三进程根据所述第二映射关系,将所述第一数据从所述第一共享内存映射至所述第二映射区域,从而实现所述第二进程向所述第三进程的单向通信。上述方法,不仅能够提升进程间通信的安全可靠性,还能够有效提升进程间通信的性能。

Figure 202010262408

The present application provides an inter-process communication method and related equipment. The method includes: a first process creates a first shared memory between a second process and a third process; the second process writes first data into the first process mapping area, and maps the first data from the first mapping area to the first shared memory according to the first mapping relationship, and the third process maps the first data to the first shared memory according to the second mapping relationship A piece of data is mapped from the first shared memory to the second mapping area, so as to realize one-way communication from the second process to the third process. The above method can not only improve the security and reliability of the inter-process communication, but also can effectively improve the performance of the inter-process communication.

Figure 202010262408

Description

一种进程间通信方法及相关设备An inter-process communication method and related equipment

技术领域technical field

本申请涉及通信技术领域,尤其涉及一种进程间通信(inter processcommunications,IPC)方法及相关设备。The present application relates to the field of communication technologies, and in particular, to an inter process communication (inter process communications, IPC) method and related devices.

背景技术Background technique

IPC是一个进程与另一个进程间共享消息的一种通信方式,主要用于计算机系统中的不同进程之间的消息传递。其中,进程是计算机资源分配的最小单位,每个进程都拥有自己的虚拟地址空间,并与其他进程的虚拟地址空间相互隔离,也就是说,一个进程只能访问自己的虚拟地址空间,不能访问其他进程的虚拟地址空间。为了使不同的进程之间能够互相访问并协调工作,操作系统提供了多种IPC机制。IPC is a communication method for sharing messages between a process and another process, and is mainly used for message passing between different processes in a computer system. Among them, a process is the smallest unit of computer resource allocation. Each process has its own virtual address space and is isolated from the virtual address space of other processes. That is to say, a process can only access its own virtual address space and cannot access it. The virtual address space of other processes. In order to enable different processes to access each other and coordinate their work, the operating system provides a variety of IPC mechanisms.

但是,现有的IPC机制存在着性能低或者安全性低等问题。However, the existing IPC mechanism has problems such as low performance or low security.

发明内容SUMMARY OF THE INVENTION

本申请实施例公开了一种进程间通信方法及相关设备,不仅能够提升进程间通信的安全可靠性,还能够有效提升进程间通信的性能。The embodiments of the present application disclose an inter-process communication method and related devices, which can not only improve the security and reliability of the inter-process communication, but also effectively improve the performance of the inter-process communication.

第一方面,本申请提供一种进程间通信方法,所述方法包括:In a first aspect, the present application provides an inter-process communication method, the method comprising:

第一进程创建第二进程以及第三进程之间的第一共享内存,其中,所述第一进程、所述第二进程以及所述第三进程运行在用户空间,所述第一进程对所述第一共享内存具有读写权限,所述第二进程对所述第一共享内存具有读写权限,所述第三进程对所述第一共享内存具有只读权限,所述第二进程和所述第三进程均在所述第一进程中注册,第一映射区域与所述第一共享内存之间具有第一映射关系,第二映射区域与所述第一共享内存之间具有第二映射关系,所述第一映射区域为所述第二进程虚拟地址空间中与所述第一共享内存建立映射关系的区域,所述第二映射区域为所述第三进程虚拟地址空间中与所述第一共享内存建立映射关系的区域;The first process creates the first shared memory between the second process and the third process, wherein the first process, the second process and the third process run in the user space, and the first process The first shared memory has read and write permissions, the second process has read and write permissions to the first shared memory, the third process has read-only permissions to the first shared memory, the second process and The third processes are all registered in the first process, the first mapping area and the first shared memory have a first mapping relationship, and the second mapping area and the first shared memory have a second mapping relationship. A mapping relationship, the first mapping area is an area in the virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is in the virtual address space of the third process. Describe the area where the first shared memory establishes a mapping relationship;

所述第二进程将第一数据写入所述第一映射区域,并根据所述第一映射关系将所述第一数据从所述第一映射区域映射至所述第一共享内存,所述第三进程根据所述第二映射关系,将所述第一数据从所述第一共享内存映射至所述第二映射区域,从而实现所述第二进程向所述第三进程的单向通信。The second process writes the first data into the first mapping area, and maps the first data from the first mapping area to the first shared memory according to the first mapping relationship, and the The third process maps the first data from the first shared memory to the second mapping area according to the second mapping relationship, thereby implementing one-way communication from the second process to the third process .

本申请提供的进程间通信方法,通过第一进程创建第二进程以及第三进程之间的第一共享内存,第二进程将第一数据写入第一映射区域,并根据第一映射关系将第一数据从第一映射区域映射至第一共享内存,第三进程根据第二映射关系,将第一数据从第一共享内存映射至第二映射区域,从而实现第二进程向第三进程的单向通信。其中,第二进程向第三进程传输第一数据的过程,是通过第一映射关系以及第二映射关系进行映射实现的,无需通过系统调用内核陷入内核空间才能实现,无需进行数据拷贝,系统开销小,通信效率高。而且,本申请提供的进程间通信方法在应用于操作系统多样的嵌入式设备时,由于没有在第二进程向第三进程的单向通信过程中引入内核态,所以无需考虑单板和操作系统适配的问题,操作简单,移植性强。同时,除了第一进程之外,只有第二进程具有第一共享内存的读写权限,可以避免多个进程同时对第一共享内存进行写操作,造成第一共享内存中的数据混乱。另外,第二进程和第三进程均是在第一进程进行注册的进程,第一进程可以对第二进程和第三进程的身份进行校验,并且只有具有第一共享内存的读写权限或者只读权限的进程才可以访问第一共享内存,可以避免恶意程序篡改或者破坏第一共享内存中的数据,能够有效提升进程间通信的安全可靠性。In the inter-process communication method provided by the present application, the first shared memory between the second process and the third process is created by the first process, the second process writes the first data into the first mapping area, and according to the first mapping relationship The first data is mapped from the first mapping area to the first shared memory, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing the transfer from the second process to the third process. One-way communication. Wherein, the process of transmitting the first data from the second process to the third process is realized by mapping the first mapping relationship and the second mapping relationship, and it can be realized without the system calling the kernel into the kernel space, without data copying and system overhead. Small, high communication efficiency. Moreover, when the inter-process communication method provided by the present application is applied to embedded devices with various operating systems, since the kernel state is not introduced in the one-way communication process from the second process to the third process, there is no need to consider the single board and the operating system. The problem of adaptation, the operation is simple, and the portability is strong. Meanwhile, in addition to the first process, only the second process has the read and write authority of the first shared memory, which can prevent multiple processes from simultaneously writing to the first shared memory, resulting in confusion of data in the first shared memory. In addition, the second process and the third process are both processes registered in the first process, the first process can verify the identities of the second process and the third process, and only has the read and write permissions of the first shared memory or Only processes with read-only permissions can access the first shared memory, which can prevent malicious programs from tampering with or destroy data in the first shared memory, and can effectively improve the security and reliability of inter-process communication.

在一种具体的实现方式中,在所述第一进程创建第二进程以及第三进程之间的第一共享内存之后,所述方法还包括:In a specific implementation manner, after the first process creates the first shared memory between the second process and the third process, the method further includes:

所述第一进程配置所述第二进程具有所述第一共享内存的读写权限,以及,配置所述第三进程具有所述第一共享内存的只读权限。The first process configures the second process to have read and write permissions to the first shared memory, and configures the third process to have read-only permissions to the first shared memory.

上述方案中,由第一进程配置第二进程具有第一共享内存的读写权限,以及,配置第三进程具有第一共享内存的只读权限,确保具有第一共享内存的读写权限的进程才能对第一共享内存进行写操作,以及具有第一共享内存的只读权限的进程才能对第一共享内存进行读操作,可以避免恶意程序对第一共享内存进行读写操作,保证第一共享内存中的数据不会被恶意程序修改甚至破坏。In the above solution, the first process configures the second process to have the read-write permission of the first shared memory, and configures the third process to have the read-only permission of the first shared memory to ensure that the process has the read-write permission of the first shared memory. Only the first shared memory can be written to, and the process with the read-only permission of the first shared memory can only read the first shared memory, which can prevent malicious programs from reading and writing the first shared memory and ensure the first shared memory. The data in memory will not be modified or even destroyed by malicious programs.

在一种具体的实现方式中,所述第一进程配置所述第二进程具有所述第一共享内存的读写权限,以及配置所述第三进程具有所述第一共享内存的只读权限,包括:In a specific implementation manner, the first process configures the second process to have read and write permissions of the first shared memory, and configures the third process to have read-only permissions of the first shared memory ,include:

所述第一进程通过访问控制列表ACL配置所述第二进程具有所述第一共享内存的读写权限,以及配置所述第三进程具有所述第一共享内存的只读权限。The first process configures the second process to have read and write permissions of the first shared memory by using an access control list ACL, and configures the third process to have read-only permissions of the first shared memory.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,所述方法还包括:In a specific implementation manner, before the second process writes the first data into the first mapping area, the method further includes:

所述第二进程分配第一堆内存,所述第一堆内存用于存储所述第二进程的控制信息;The second process allocates a first heap memory, and the first heap memory is used to store control information of the second process;

所述第二进程从所述第一堆内存中获取所述第二进程的控制信息,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The second process acquires the control information of the second process from the first heap memory, so that the second process writes the first data into the first process according to the control information of the second process. a mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述方法还包括:Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

所述第三进程分配第二堆内存,所述第二堆内存用于存储所述第三进程的控制信息;The third process allocates a second heap memory, and the second heap memory is used to store control information of the third process;

所述第三进程从所述第二堆内存中获取所述第三进程的控制信息,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The third process obtains the control information of the third process from the second heap memory, so that the third process converts the first data from the first data from the first process according to the control information of the third process. The shared memory is mapped to the second mapping area.

上述方案中,通过将第二进程的控制信息存储在第一堆内存(第一进程的堆内存),将第三进程的控制信息存储在第二堆内存(第三进程的堆内存),在第二进程与第三进程进行通信时,第二进程和第三进程可以直接从自己的堆内存中获取自己的控制信息进行通信,进程间通信的效率高。同时,通过将第二进程的控制信息存储在第一堆内存,将第三进程的控制信息存储在第二堆内存,还可以实现第二进程的控制信息和第三进程的控制信息的分开存放,有利于第二进程和第三进程在运行时相互独立、互不干扰,在第二进程和第三进程中的其中一个进程出现故障时,故障不会扩散至另外一个进程。In the above scheme, by storing the control information of the second process in the first heap memory (the heap memory of the first process), and storing the control information of the third process in the second heap memory (the heap memory of the third process), When the second process communicates with the third process, the second process and the third process can directly obtain their own control information from their own heap memory for communication, and the efficiency of inter-process communication is high. At the same time, by storing the control information of the second process in the first heap memory and storing the control information of the third process in the second heap memory, the control information of the second process and the control information of the third process can also be stored separately. , which is beneficial for the second process and the third process to be independent and non-interfering with each other during operation. When one of the second process and the third process fails, the failure will not spread to the other process.

在一种具体的实现方式中,所述第一堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第二堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the first heap memory and the first mapping area are different memories, the first mapping area is used to store the shared memory information of the second process, and the second The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

上述方案中,将第二进程的控制信息存储在第一堆内存,将第二进程的共享内存信息存储在第一映射区域,第一堆内存与第一映射区域为不同的内存,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同理,将第三进程的控制信息存储在第二堆内存,将第三进程的共享内存信息存储在第二映射区域,第二堆内存与第二映射区域为不同的内存,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above solution, the control information of the second process is stored in the first heap memory, the shared memory information of the second process is stored in the first mapping area, and the first heap memory and the first mapping area are different memories, which can realize the first The separate storage of the control information of the second process and the shared memory information of the second process is beneficial to ensure the safety of the shared memory information of the second process. Similarly, the control information of the third process is stored in the second heap memory, and the shared memory information of the third process is stored in the second mapping area, and the second heap memory and the second mapping area are different memories, so that the third The separate storage of the control information of the process and the shared memory information of the third process is beneficial to ensure the safety of the shared memory information of the third process.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,所述方法还包括:In a specific implementation manner, before the second process writes the first data into the first mapping area, the method further includes:

所述第一进程分配第三堆内存,所述第三堆内存用于存储所述第二进程的控制信息;The first process allocates a third heap memory, and the third heap memory is used to store control information of the second process;

所述第一进程接收所述第二进程发送的第一获取信息请求,其中,所述第一获取信息请求用于请求从所述第三堆内存中获取所述第二进程的控制信息;The first process receives a first information acquisition request sent by the second process, wherein the first information acquisition request is used to request to acquire control information of the second process from the third heap memory;

所述第一进程根据所述第一获取信息请求将所述第二进程的控制信息发送至所述第二进程,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The first process sends the control information of the second process to the second process according to the first request for obtaining information, so that the second process sends the control information of the second process to the second process according to the control information of the second process. A data is written into the first mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述方法还包括:Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

所述第三进程分配第二堆内存,所述第二堆内存用于存储所述第三进程的控制信息;The third process allocates a second heap memory, and the second heap memory is used to store control information of the third process;

所述第三进程从所述第二堆内存中获取所述第三进程的控制信息,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The third process obtains the control information of the third process from the second heap memory, so that the third process converts the first data from the first data from the first process according to the control information of the third process. The shared memory is mapped to the second mapping area.

上述方案中,通过将第二进程的控制信息存储在第三堆内存(第一进程的堆内存),将第三进程的控制信息存储在第三进程的堆内存,在第二进程与第三进程进行通信时,第二进程需要先向第一进程发送获取控制信息的请求,然后获得第一进程返回的第二进程的控制信息,第三进程可以从自己的堆内存中获取自己的控制信息,可以看出,第二进程的通信效率低于第三进程的通信效率。也就是说,将进程的控制信息存储在进程自己的堆内存的方法,适用于性能要求高的进程;将进程的控制信息存储在第一进程的堆内存的方法,适用于性能要求低一些的进程。可以看出,上述方法不仅适用于通信性能要求高的进程,还适用于通信性能要求低的进程,灵活性高。同时,通过将第二进程的控制信息存储在第一进程的堆内存,将第三进程的控制信息存储在第三进程的堆内存,还可以实现第二进程的控制信息和第三进程的控制信息的分开存放,有利于第二进程和第三进程在运行时相互独立、互不干扰,在第二进程和第三进程中的其中一个进程出现故障时,故障不会扩散至另外一个进程。In the above solution, by storing the control information of the second process in the third heap memory (the heap memory of the first process), and storing the control information of the third process in the heap memory of the third process, between the second process and the third process When processes communicate, the second process needs to first send a request for obtaining control information to the first process, and then obtain the control information of the second process returned by the first process, and the third process can obtain its own control information from its own heap memory. , it can be seen that the communication efficiency of the second process is lower than that of the third process. That is to say, the method of storing process control information in the process's own heap memory is suitable for processes with high performance requirements; the method of storing process control information in the heap memory of the first process is suitable for lower performance requirements. process. It can be seen that the above method is not only suitable for processes with high communication performance requirements, but also suitable for processes with low communication performance requirements, and has high flexibility. At the same time, by storing the control information of the second process in the heap memory of the first process, and storing the control information of the third process in the heap memory of the third process, the control information of the second process and the control of the third process can also be realized. The separate storage of information is beneficial for the second process and the third process to be independent of each other and not interfere with each other during operation. When one of the second process and the third process fails, the failure will not spread to the other process.

在一种具体的实现方式中,所述第三堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第二堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the third heap memory and the first mapping area are different memory, the first mapping area is used to store the shared memory information of the second process, the second The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

上述方案中,将第二进程的控制信息存储在第三堆内存(,将第二进程的共享信息存储在第一映射区域,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同时,将第三进程的控制信息存储在第二堆内存,将第三进程的共享内存信息存储在第二映射区域,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above scheme, the control information of the second process is stored in the third heap memory (, the shared information of the second process is stored in the first mapping area, and the control information of the second process and the shared memory information of the second process can be realized. Store separately, help to guarantee the safety of the shared memory information of the second process.Meanwhile, the control information of the third process is stored in the second heap memory, and the shared memory information of the third process is stored in the second mapping area, which can realize the first The separate storage of the control information of the three processes and the shared memory information of the third process is beneficial to ensure the safety of the shared memory information of the third process.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,所述方法还包括:In a specific implementation manner, before the second process writes the first data into the first mapping area, the method further includes:

所述第二进程分配第一堆内存,所述第一堆内存用于存储所述第二进程的控制信息;The second process allocates a first heap memory, and the first heap memory is used to store control information of the second process;

所述第二进程从所述第一堆内存中获取所述第二进程的控制信息,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The second process acquires the control information of the second process from the first heap memory, so that the second process writes the first data into the first process according to the control information of the second process. a mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述方法还包括:Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

所述第一进程分配第三堆内存,所述第三堆内存用于存储所述第三进程的控制信息;The first process allocates a third heap memory, and the third heap memory is used to store control information of the third process;

所述第一进程接收所述第三进程发送的第二获取信息请求,其中,所述第二获取信息请求用于请求从所述第三堆内存中获取所述第三进程的控制信息;The first process receives a second information acquisition request sent by the third process, where the second information acquisition request is used to request to acquire control information of the third process from the third heap memory;

所述第一进程根据所述第二获取信息请求将所述第三进程的控制信息发送至所述第三进程,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The first process sends the control information of the third process to the third process according to the second request for obtaining information, so that the third process sends the control information of the third process to the third process according to the control information of the third process. A data is mapped from the first shared memory to the second mapped area.

在一种具体的实现方式中,所述第一堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第三堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the first heap memory and the first mapping area are different memory, the first mapping area is used to store the shared memory information of the second process, the third The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

上述方案中,将第二进程的控制信息存储在第一堆内存,将第二进程的共享信息存储在第一映射区域,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同时,将第三进程的控制信息存储在第三堆内存,将第三进程的共享内存信息存储在第二映射区域,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above solution, the control information of the second process is stored in the first heap memory, and the shared information of the second process is stored in the first mapping area, so that the control information of the second process and the shared memory information of the second process can be separated. The storage is beneficial to ensure the safety of the shared memory information of the second process. At the same time, the control information of the third process is stored in the third heap memory, and the shared memory information of the third process is stored in the second mapping area, so that the control information of the third process and the shared memory information of the third process can be stored separately. , which is beneficial to ensure the security of the shared memory information of the third process.

在一种具体的实现方式中,所述第一进程包括检测线程,所述方法还包括:In a specific implementation, the first process includes a detection thread, and the method further includes:

所述检测线程检测所述第二进程或者所述第三进程是否出现复位;The detection thread detects whether the second process or the third process is reset;

在检测到所述第二进程或者所述第三进程出现复位的情况下,所述检测线程通知所述第二进程和所述第三进程中没有出现复位的进程终止通信,并释放所述第一共享内存。In the case of detecting that the second process or the third process is reset, the detection thread notifies the second process and the third process that have not been reset to terminate communication, and releases the third process a shared memory.

上述方案中,第一进程还可以检测第二进程或者第三进程是否出现复位,在检测到第二进程或者第三进程出现复位的情况下,可以及时终止第二进程和第三进程之间的通信,释放第一共享内存所占用的资源,有利于资源及时回收。In the above solution, the first process can also detect whether the second process or the third process is reset, and in the case of detecting that the second process or the third process is reset, the communication between the second process and the third process can be terminated in time. communication, and release the resources occupied by the first shared memory, which is conducive to the timely recovery of resources.

在一种具体的实现方式中,所述第一进程包括恢复共享内存线程,所述恢复共享内存线程为所述第一进程包含的线程中运行级别最高的线程,所述方法还包括:In a specific implementation manner, the first process includes a restoration shared memory thread, and the restoration shared memory thread is a thread with the highest running level among the threads included in the first process, and the method further includes:

当所述第一进程重启时,所述恢复共享内存线程恢复所述第一共享内存。When the first process is restarted, the restoring shared memory thread restores the first shared memory.

上述方案中,在第一进程重启时,第一进程中的运行级别最高的恢复共享内存线程可以快速恢复第一共享内存,进一步提升进程间通信的可靠性。In the above solution, when the first process is restarted, the restoring shared memory thread with the highest running level in the first process can quickly restore the first shared memory, thereby further improving the reliability of inter-process communication.

在一种具体的实现方式中,所述第一共享内存中包括破坏者队列,所述第二进程将所述第一数据从所述第一映射区域映射至所述第一共享内存,包括:In a specific implementation manner, the first shared memory includes a destroyer queue, and the second process maps the first data from the first mapping area to the first shared memory, including:

所述第二进程将所述第一数据从所述第一映射区域映射至所述破坏者队列;the second process maps the first data from the first mapping area to the destroyer queue;

所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域,包括:The third process maps the first data from the first shared memory to the second mapping area, including:

所述第三进程将所述第一数据从所述破坏者队列映射至所述第二映射区域。The third process maps the first data from the spoiler queue to the second mapping area.

上述方案中,第一共享内存中包括破坏者队列,支持第一共享内存的反复读写,能够有效地提高进程间信息的吞吐量,进一步提升进程间通信的性能。In the above solution, the first shared memory includes a destroyer queue, which supports repeated reading and writing of the first shared memory, which can effectively improve the throughput of inter-process information and further improve the performance of inter-process communication.

在一种具体的实现方式中,所述方法还包括:In a specific implementation, the method further includes:

所述第一进程创建所述第二进程以及所述第三进程之间的第二共享内存,其中,所述第一进程对所述第二共享内存具有读写权限,所述第三进程对所述第二共享内存具有读写权限,所述第二进程对所述第二共享内存具有只读权限,第三映射区域与所述第二共享内存之间具有第三映射关系,第四映射区域与所述第二共享内存之间具有第四映射关系,所述第三映射区域为所述第三进程虚拟地址空间中与所述第二共享内存建立映射关系的区域,所述第四映射区域为所述第二进程虚拟地址空间中与所述第二共享内存建立映射关系的区域;The first process creates a second shared memory between the second process and the third process, wherein the first process has read and write permissions to the second shared memory, and the third process has read and write permissions to the second shared memory. The second shared memory has read and write permissions, the second process has read-only permissions to the second shared memory, the third mapping area and the second shared memory have a third mapping relationship, and the fourth mapping There is a fourth mapping relationship between the area and the second shared memory, the third mapping area is an area in the virtual address space of the third process that establishes a mapping relationship with the second shared memory, and the fourth mapping The area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

所述第三进程将所述第二数据写入所述第三映射区域,并根据所述第三映射关系,将所述第二数据从所述第三映射区域映射至所述第二共享内存,所述第二进程根据所述第四映射关系,将所述第二数据从所述第二共享内存映射至所述第四映射区域,从而实现所述第三进程向所述第二进程的单向通信。The third process writes the second data into the third mapping area, and maps the second data from the third mapping area to the second shared memory according to the third mapping relationship , the second process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship, so as to realize the transfer from the third process to the second process One-way communication.

上述方案中,通过第一进程创建第二进程和第三进程之间的第二共享内存,第三进程将第二数据写入第三映射区域,并根据第三映射关系将第二数据从第三映射区域映射至第二共享内存,第三进程根据第四映射关系,将第二数据从第二共享内存映射至第四映射区域。从而实现第三进程向第二进程的单向通信。其中,第三进程向第二进程传输第二数据的过程,是通过第三映射关系以及第四映射关系进行映射实现的,无需通过系统调用内核陷入内核空间才能实现,无需进行数据拷贝,系统开销小,通信效率高。而且,在应用于操作系统多样的嵌入式设备时,由于没有在第三进程向第二进程的单向通信过程中引入内核态,所以无需考虑单板和操作系统适配的问题,操作简单,移植性强。同时,除了第一进程之外,只有第三进程具有第二共享内存的读写权限,可以避免多个进程同时对第二共享内存进行写操作,或者恶意程序篡改或者破坏第二共享内存的数据,造成第二共享内存中的数据混乱。另外,第二进程和第三进程均是在第一进程进行注册的进程,第一进程可以对第二进程和第三进程的身份进行校验,并且只有具有第二共享内存的读写权限或者只读权限的进程才可以访问第二共享内存,能够有效地提升进程间通信的安全可靠性。In the above solution, the second shared memory between the second process and the third process is created by the first process, the third process writes the second data into the third mapping area, and according to the third mapping relationship The third mapping area is mapped to the second shared memory, and the third process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship. Thereby, one-way communication from the third process to the second process is realized. Wherein, the process of transmitting the second data from the third process to the second process is realized by mapping the third mapping relationship and the fourth mapping relationship, which can be realized without the system calling the kernel into the kernel space, without data copying and system overhead. Small, high communication efficiency. Moreover, when applied to embedded devices with various operating systems, since the kernel state is not introduced in the one-way communication process from the third process to the second process, there is no need to consider the problem of adapting the board and the operating system, and the operation is simple. Portable. At the same time, in addition to the first process, only the third process has read and write permissions to the second shared memory, which can prevent multiple processes from simultaneously writing to the second shared memory, or malicious programs tampering or destroying data in the second shared memory , causing data confusion in the second shared memory. In addition, both the second process and the third process are processes registered in the first process, the first process can verify the identities of the second process and the third process, and only has the read and write permissions of the second shared memory or Only processes with read-only permissions can access the second shared memory, which can effectively improve the security and reliability of inter-process communication.

第二方面,本申请提供一种进程间通信装置,所述装置包括:In a second aspect, the present application provides an inter-process communication device, the device comprising:

创建模块,用于通过第一进程创建第二进程以及第三进程之间的第一共享内存,其中,所述第一进程、所述第二进程以及所述第三进程运行在用户空间,所述第一进程对所述第一共享内存具有读写权限,所述第二进程对所述第一共享内存具有读写权限,所述第三进程对所述第一共享内存具有只读权限,所述第二进程和所述第三进程均在所述第一进程中注册,第一映射区域与所述第一共享内存之间具有第一映射关系,第二映射区域与所述第一共享内存之间具有第二映射关系,所述第一映射区域为所述第二进程虚拟地址空间中与所述第一共享内存建立映射关系的区域,所述第二映射区域为所述第三进程虚拟地址空间中与所述第一共享内存建立映射关系的区域;The creation module is used to create the first shared memory between the second process and the third process through the first process, wherein the first process, the second process and the third process run in the user space, so the first process has read and write permissions to the first shared memory, the second process has read and write permissions to the first shared memory, and the third process has read-only permissions to the first shared memory, Both the second process and the third process are registered in the first process, there is a first mapping relationship between the first mapping area and the first shared memory, and the second mapping area is shared with the first There is a second mapping relationship between memories, the first mapping area is an area in the virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is the third process. an area in the virtual address space that establishes a mapping relationship with the first shared memory;

通信模块,用于通过所述第二进程将第一数据写入所述第一映射区域,并根据所述第一映射关系将所述第一数据从所述第一映射区域映射至所述第一共享内存,还通过所述第三进程根据所述第二映射关系,将所述第一数据从所述第一共享内存映射至所述第二映射区域,从而实现所述第二进程向所述第三进程的单向通信。a communication module, configured to write first data into the first mapping area through the second process, and map the first data from the first mapping area to the first mapping area according to the first mapping relationship a shared memory, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relationship, so as to realize the second process to all One-way communication of the third process.

在一种具体的实现方式中,所述创建模块,还用于:In a specific implementation manner, the creation module is further used for:

所述创建模块通过所述第一进程配置所述第二进程具有所述第一共享内存的读写权限,以及,配置所述第三进程具有所述第一共享内存的只读权限。The creation module configures, through the first process, the second process to have read and write permissions of the first shared memory, and configures the third process to have read-only permissions of the first shared memory.

在一种具体的实现方式中,所述创建模块具体用于:In a specific implementation manner, the creation module is specifically used for:

所述创建模块通过所述第一进程通过访问控制列表ACL配置所述第二进程具有所述第一共享内存的读写权限,以及配置所述第三进程具有所述第一共享内存的只读权限。The creation module configures the second process to have read and write permissions of the first shared memory through the access control list ACL by the first process, and configures the third process to have read-only access to the first shared memory permissions.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,所述装置还包括:分配模块和获取模块;In a specific implementation manner, before the second process writes the first data into the first mapping area, the apparatus further includes: an allocation module and an acquisition module;

所述分配模块,用于通过所述第二进程分配第一堆内存,所述第一堆内存用于存储所述第二进程的控制信息;the allocation module, configured to allocate a first heap memory through the second process, where the first heap memory is used to store control information of the second process;

所述获取模块,用于通过所述第二进程从所述第一堆内存中获取所述第二进程的控制信息,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The acquiring module is configured to acquire the control information of the second process from the first heap memory through the second process, so that the second process can retrieve the control information of the second process according to the control information of the second process. first data is written into the first mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述分配模块,还用于通过所述第三进程分配第二堆内存,所述第二堆内存用于存储所述第三进程的控制信息;Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module is further configured to allocate the second heap memory by the third process, so The second heap memory is used to store the control information of the third process;

所述获取模块,还用于通过所述第三进程从所述第二堆内存中获取所述第三进程的控制信息,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The obtaining module is further configured to obtain, through the third process, the control information of the third process from the second heap memory, so that the third process can obtain the control information of the third process according to the control information of the third process. The first data is mapped from the first shared memory to the second mapping area.

在一种具体的实现方式中,所述第一堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第二堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the first heap memory and the first mapping area are different memories, the first mapping area is used to store the shared memory information of the second process, and the second The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,所述装置还包括发送模块;In a specific implementation manner, before the second process writes the first data into the first mapping area, the apparatus further includes a sending module;

所述分配模块,用于通过所述第一进程分配第三堆内存,所述第三堆内存用于存储所述第二进程的控制信息;the allocation module, configured to allocate a third heap memory through the first process, where the third heap memory is used to store control information of the second process;

所述获取模块,用于通过所述第一进程接收所述第二进程发送的第一获取信息请求,其中,所述第一获取信息请求用于请求从所述第三堆内存中获取所述第二进程的控制信息;The obtaining module is configured to receive, through the first process, a first obtaining information request sent by the second process, wherein the first obtaining information request is used to request to obtain the information from the third heap memory. control information of the second process;

所述发送模块,用于通过所述第一进程根据所述第一获取信息请求将所述第二进程的控制信息发送至所述第二进程,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The sending module is configured to send, through the first process, the control information of the second process to the second process according to the first information acquisition request, so that the second process The control information of the process writes the first data into the first mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述分配模块,还用于通过所述第三进程分配第二堆内存,所述第二堆内存用于存储所述第三进程的控制信息;Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module is further configured to allocate the second heap memory by the third process, so The second heap memory is used to store the control information of the third process;

所述获取模块,还用于通过所述第三进程从所述第二堆内存中获取所述第三进程的控制信息,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The obtaining module is further configured to obtain, through the third process, the control information of the third process from the second heap memory, so that the third process can obtain the control information of the third process according to the control information of the third process. The first data is mapped from the first shared memory to the second mapping area.

在一种具体的实现方式中,所述第三堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第二堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the third heap memory and the first mapping area are different memory, the first mapping area is used to store the shared memory information of the second process, the second The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

在一种具体的实现方式中,在所述第二进程将第一数据写入所述第一映射区域之前,In a specific implementation manner, before the second process writes the first data into the first mapping area,

所述分配模块,用于通过所述第二进程分配第一堆内存,所述第一堆内存用于存储所述第二进程的控制信息;the allocation module, configured to allocate a first heap memory through the second process, where the first heap memory is used to store control information of the second process;

所述获取模块,用于通过所述第二进程从所述第一堆内存中获取所述第二进程的控制信息,以使得所述第二进程根据所述第二进程的控制信息将所述第一数据写入所述第一映射区域;The acquiring module is configured to acquire the control information of the second process from the first heap memory through the second process, so that the second process can retrieve the control information of the second process according to the control information of the second process. first data is written into the first mapping area;

在所述第三进程将所述第一数据从所述第一共享内存映射至所述第二映射区域之前,所述装置还包括接收模块;Before the third process maps the first data from the first shared memory to the second mapping area, the apparatus further includes a receiving module;

所述分配模块,用于通过所述第一进程分配第三堆内存,所述第三堆内存用于存储所述第三进程的控制信息;the allocation module, configured to allocate a third heap memory through the first process, where the third heap memory is used to store control information of the third process;

所述接收模块,用于通过所述第一进程接收所述第三进程发送的第二获取信息请求,其中,所述第二获取信息请求用于请求从所述第三堆内存中获取所述第三进程的控制信息;The receiving module is configured to receive, through the first process, a second request for obtaining information sent by the third process, wherein the second request for obtaining information is used to request to obtain the information from the third heap memory. Control information of the third process;

所述发送模块,用于通过所述第一进程根据所述第二获取信息请求将所述第三进程的控制信息发送至所述第三进程,以使得所述第三进程根据所述第三进程的控制信息将所述第一数据从所述第一共享内存映射至所述第二映射区域。The sending module is configured to send, through the first process, the control information of the third process to the third process according to the second request for obtaining information, so that the third process The control information of the process maps the first data from the first shared memory to the second mapping area.

在一种具体的实现方式中,所述第一堆内存与所述第一映射区域为不同的内存,所述第一映射区域用于存储所述第二进程的共享内存信息,所述第三堆内存与所述第二映射区域为不同的内存,所述第二映射区域用于存储所述第三进程的共享内存信息。In a specific implementation manner, the first heap memory and the first mapping area are different memory, the first mapping area is used to store the shared memory information of the second process, the third The heap memory and the second mapping area are different memory, and the second mapping area is used to store the shared memory information of the third process.

在一种具体的实现方式中,所述第一进程包括检测线程,所述装置还包括检测模块,所述检测模块具体用于:In a specific implementation manner, the first process includes a detection thread, and the apparatus further includes a detection module, and the detection module is specifically configured to:

所述检测模块通过所述检测线程检测所述第二进程或者所述第三进程是否出现复位;The detection module detects, through the detection thread, whether the second process or the third process is reset;

所述检测模块在检测到所述第二进程或者所述第三进程出现复位的情况下,通过所述检测线程通知所述第二进程和所述第三进程中没有出现复位的进程终止通信,并释放所述第一共享内存。When the detection module detects that the second process or the third process is reset, the detection thread notifies the second process and the third process that no reset occurs to terminate the communication, and release the first shared memory.

在一种具体的实现方式中,所述第一进程包括恢复共享内存线程,所述恢复共享内存线程为所述第一进程包含的线程中运行级别最高的线程,所述装置还包括恢复模块,所述恢复模块具体用于:In a specific implementation manner, the first process includes a restoration shared memory thread, and the restoration shared memory thread is a thread with the highest running level among the threads included in the first process, and the apparatus further includes a restoration module, The recovery module is specifically used for:

当所述第一进程重启时,所述恢复模块通过所述恢复共享内存线程恢复所述第一共享内存。When the first process is restarted, the restoring module restores the first shared memory through the restoring shared memory thread.

在一种具体的实现方式中,所述第一共享内存中包括破坏者队列,所述通信模块具体用于:In a specific implementation manner, the first shared memory includes a destroyer queue, and the communication module is specifically configured to:

所述通信模块通过所述第二进程将所述第一数据从所述第一映射区域映射至所述破坏者队列;The communication module maps the first data from the first mapping area to the destroyer queue through the second process;

所述通信模块通过所述第三进程将所述第一数据从所述破坏者队列映射至所述第二映射区域。The communication module maps the first data from the spoiler queue to the second mapping area through the third process.

在一种具体的实现方式中,所述创建模块,还用于通过所述第一进程创建所述第二进程以及所述第三进程之间的第二共享内存,其中,所述第一进程对所述第二共享内存具有读写权限,所述第三进程对所述第二共享内存具有读写权限,所述第二进程对所述第二共享内存具有只读权限,第三映射区域与所述第二共享内存之间具有第三映射关系,第四映射区域与所述第二共享内存之间具有第四映射关系,所述第三映射区域为所述第三进程虚拟地址空间中与所述第二共享内存建立映射关系的区域,所述第四映射区域为所述第二进程虚拟地址空间中与所述第二共享内存建立映射关系的区域;In a specific implementation manner, the creating module is further configured to create a second shared memory between the second process and the third process by using the first process, wherein the first process The second shared memory has read and write permissions, the third process has read and write permissions to the second shared memory, the second process has read-only permissions to the second shared memory, and the third mapping area There is a third mapping relationship with the second shared memory, a fourth mapping relationship is between the fourth mapping area and the second shared memory, and the third mapping area is in the virtual address space of the third process an area that establishes a mapping relationship with the second shared memory, and the fourth mapping area is an area that establishes a mapping relationship with the second shared memory in the virtual address space of the second process;

所述通信模块,还用于通过所述第三进程将所述第二数据写入所述第三映射区域,并根据所述第三映射关系,将所述第二数据从所述第三映射区域映射至所述第二共享内存,所述第二进程根据所述第四映射关系,将所述第二数据从所述第二共享内存映射至所述第四映射区域,从而实现所述第三进程向所述第二进程的单向通信。The communication module is further configured to write the second data into the third mapping area through the third process, and according to the third mapping relationship, write the second data from the third mapping The area is mapped to the second shared memory, and the second process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship, so as to realize the first One-way communication of the three processes to the second process.

第三方面,本申请提供一种进程间通信装置,所述装置包括:处理器、通信接口以及存储器;所述存储器用于存储指令,所述处理器用于执行所述指令,所述通信接口用于接收或者发送数据;其中,所述处理器执行所述指令时执行如上述第一方面或者第一方面的任意具体实现方式中所描述方法。In a third aspect, the present application provides an inter-process communication device, the device includes: a processor, a communication interface, and a memory; the memory is used for storing instructions, the processor is used for executing the instructions, and the communication interface is used for for receiving or sending data; wherein, when the processor executes the instruction, the method described in the first aspect or any specific implementation manner of the first aspect is performed.

第四方面,本申请提供一种计算机存储介质,所述计算机存储介质存储有用于进程间通信的计算机程序,所述计算机程序被处理器执行时实现如上述第一方面或者第一方面的任意具体实现方式中所描述方法。In a fourth aspect, the present application provides a computer storage medium, where the computer storage medium stores a computer program for inter-process communication, and when the computer program is executed by a processor, implements the first aspect or any specific implementation of the first aspect. The method described in the implementation.

第五方面,本申请提供一种计算机程序产品,其特征在于,当所述计算机程序产品被计算机读取并执行时,实现如上述第一方面或者第一方面的任意具体实现方式中所描述方法。In a fifth aspect, the present application provides a computer program product, characterized in that, when the computer program product is read and executed by a computer, the method described in the first aspect or any specific implementation manner of the first aspect is implemented. .

附图说明Description of drawings

以下对本申请实施例用到的附图进行介绍。The accompanying drawings used in the embodiments of the present application will be introduced below.

图1是本申请实施例提供的进程间通信方法的应用场景示意图;1 is a schematic diagram of an application scenario of an inter-process communication method provided by an embodiment of the present application;

图2是本申请实施例提供的一种数据传输过程的示意图;2 is a schematic diagram of a data transmission process provided by an embodiment of the present application;

图3是本申请实施例提供的一种基于结合剂机制的进程间通信系统的示意图;3 is a schematic diagram of an interprocess communication system based on a binding agent mechanism provided by an embodiment of the present application;

图4是本申请实施例提供的一种进程间通信方法的流程示意图;4 is a schematic flowchart of an inter-process communication method provided by an embodiment of the present application;

图5是本申请实施例提供的一种破坏者队列的原理图;5 is a schematic diagram of a destroyer queue provided by an embodiment of the present application;

图6是本申请实施例提供的一种进程虚拟地址空间的示意图;6 is a schematic diagram of a process virtual address space provided by an embodiment of the present application;

图7是本申请实施例提供的一种进程间通信方法的流程示意图;7 is a schematic flowchart of an inter-process communication method provided by an embodiment of the present application;

图8是本申请实施例提供的一种进程间通信方法的交互示意图;FIG. 8 is an interactive schematic diagram of an inter-process communication method provided by an embodiment of the present application;

图9是本申请实施例提供的一种进程间通信装置的结构示意图;FIG. 9 is a schematic structural diagram of an inter-process communication apparatus provided by an embodiment of the present application;

图10是本申请实施例提供的另一种进程间通信装置的结构示意图。FIG. 10 is a schematic structural diagram of another inter-process communication apparatus provided by an embodiment of the present application.

具体实施方式Detailed ways

下面结合本申请实施例中的附图对本申请实施例进行描述。The embodiments of the present application will be described below with reference to the accompanying drawings in the embodiments of the present application.

在介绍本申请实施例中的方法之前,为了便于本领域技术人员对本申请实施例的理解,先对本申请实施例中涉及到的相关概念进行介绍。Before introducing the methods in the embodiments of the present application, in order to facilitate the understanding of the embodiments of the present application by those skilled in the art, related concepts involved in the embodiments of the present application are first introduced.

进程隔离是为保护操作系统中进程互不干扰而设计的技术。这个技术是为了避免进程A修改进程B中的数据的情况发生,以免进程A影响进程B的运行,甚至进程A会破坏进程B的运行。进程是通过虚拟地址空间实现隔离的,具体地,进程A的虚拟地址空间和进程B的虚拟地址空间不同,这样就可以防止进程A修改进程B中的数据。Process isolation is a technology designed to protect processes in an operating system from interfering with each other. This technique is to prevent process A from modifying the data in process B, so as to prevent process A from affecting the operation of process B, or even process A will destroy the operation of process B. Processes are isolated through virtual address spaces. Specifically, the virtual address space of process A is different from the virtual address space of process B, so that process A can be prevented from modifying data in process B.

虚拟地址空间:虚拟地址空间并不真实存在于计算机中。每个进程都分配有自己的虚拟地址空间,而且只能访问自己被分配使用的虚拟地址空间。理论上,虚拟地址空间受物理内存大小的限制,如物理内存为4GB,那么虚拟地址空间的地址范围就应该是0x00000000~0xFFFFFFFF。每个进程都有自己独立的虚拟地址空间,这样每个进程都能访问自己的虚拟地址空间,这样做到了有效的进程隔离。Virtual Address Space: Virtual address space does not actually exist in the computer. Each process is allocated its own virtual address space, and can only access the virtual address space it is allocated for. In theory, the virtual address space is limited by the size of physical memory. For example, if the physical memory is 4GB, the address range of the virtual address space should be 0x00000000~0xFFFFFFFF. Each process has its own independent virtual address space, so that each process can access its own virtual address space, which achieves effective process isolation.

物理地址空间:物理地址空间是存在于计算机中的一个实体,在每一台计算机中保持唯一独立性,也可以称它为物理内存。例如,在32位的机器上,物理内存的大小理论上可以达到4GB,但如果实际装了512MB的内存,那么其物理地址空间真正的有效部分只有512MB,其他部分是无效的。Physical address space: The physical address space is an entity that exists in a computer and maintains unique independence in each computer. It can also be called physical memory. For example, on a 32-bit machine, the size of physical memory can theoretically reach 4GB, but if 512MB of memory is actually installed, the real effective part of its physical address space is only 512MB, and other parts are invalid.

虚拟地址空间和物理地址空间之间存在映射,映射是通过内存管理单元(memorymanagement unit,MMU)来完成的,进程可以通过操作自己的虚拟地址空间间接地操作物理地址空间。There is a mapping between the virtual address space and the physical address space. The mapping is done by a memory management unit (MMU), and a process can indirectly operate the physical address space by operating its own virtual address space.

用户空间(user space)是用户程序运行的空间,内核空间(kernel space)是系统内核运行的空间。现在的操作系统都是采用的虚拟存储器,对于32位系统而言,它的虚拟地址空间就是2的32次方,即4GB。操作系统的核心是内核,它独立于普通的应用程序,可以访问受保护的虚拟地址空间,也可以访问底层硬件设备。为了保护用户进程不能直接操作内核,保证内核的安全,操作系统从逻辑上将虚拟地址空间划分为用户空间和内核空间。针对林纳克斯(linux)操作系统而言,将最高的1GB字节供内核使用,称为内核空间,较低的3GB字节供各进程使用,称为用户空间。也就是说,每个进程的4GB虚拟地址空间中,最高1GB都是一样的,即内核空间。只有剩余的3GB归进程自己使用。换句话说,内核空间是被所有进程共享的。User space (user space) is the space where user programs run, and kernel space (kernel space) is the space where the system kernel runs. Today's operating systems use virtual memory. For a 32-bit system, its virtual address space is 2 to the 32nd power, that is, 4GB. The core of the operating system is the kernel, which is independent of ordinary applications and can access the protected virtual address space as well as the underlying hardware devices. In order to protect the user process from directly operating the kernel and ensure the security of the kernel, the operating system logically divides the virtual address space into user space and kernel space. For the Linux operating system, the highest 1GB bytes are used by the kernel, which is called kernel space, and the lower 3GB bytes are used by each process, which is called user space. That is to say, in the 4GB virtual address space of each process, the highest 1GB is the same, that is, the kernel space. Only the remaining 3GB is used by the process itself. In other words, kernel space is shared by all processes.

用户态:当进程运行在用户空间时则处于用户态,此时处理器在特权级最低的Ring3用户代码中运行,Ring3状态不能访问Ring0的地址空间,包括代码和数据。User mode: When the process runs in user space, it is in user mode. At this time, the processor runs in the Ring3 user code with the lowest privilege level, and the Ring3 state cannot access the address space of Ring0, including code and data.

内核态:当进程运行在内核空间时就处于内核态,进程可以执行系统调用陷入内核空间中运行,此时处理器处于特权级最高的Ring0级内核代码中执行。Kernel state: When the process runs in the kernel space, it is in the kernel state. The process can execute system calls and fall into the kernel space to run. At this time, the processor is executed in the kernel code of Ring0 level with the highest privilege level.

操作系统虽然从逻辑上划分了用户空间和内核空间,但不可避免的会存在某些运行在用户空间的进程需要访问内核空间的资源,比如文件操作或者访问网络等操作,需要借助系统调用实现这些操作。系统调用是用户空间的进程访问内核空间的唯一方式,保证了所有的操作都是在内核的控制下进行的,避免了用户空间的进程对内核空间的越权访问,提升系统的安全性和稳定性。Although the operating system logically divides the user space and the kernel space, it is inevitable that some processes running in the user space need to access the resources of the kernel space, such as file operations or network access, etc., which need to be implemented with the help of system calls. operate. System calls are the only way for user space processes to access kernel space, which ensures that all operations are carried out under the control of the kernel, avoids unauthorized access to kernel space by user space processes, and improves system security and stability. .

在用户态状态下,进程只能受限的访问内存,且不允许访问外围设备,例如硬盘,网卡等,占用中央处理器(central processing unit,CPU)的能力被剥夺,CPU资源可以被其他进程获取。In user mode, a process can only access memory in a limited manner, and is not allowed to access peripheral devices, such as hard disks, network cards, etc., the ability to occupy the central processing unit (CPU) is deprived, and CPU resources can be used by other processes. Obtain.

在内核态状态下,CPU可以访问内存的所有数据,包括外围设备,CPU也可以将自己从一个进程切换到另一个进程。In kernel mode, the CPU can access all data in memory, including peripheral devices, and the CPU can also switch itself from one process to another.

IPC的应用场景可以为一个进程将它的数据发送给另一个进程,也可以为一个进程向另一个进程发送消息通知另一个进程发生了某种事件,还可以为两个进程中的其中一个进程修改了共享数据,另一个进程应该立刻看到等。The application scenario of IPC can be for one process to send its data to another process, or for one process to send a message to another process to notify another process that some event has occurred, or for one of the two processes. Shared data is modified, another process should see it immediately, etc.

这里,以一个进程将它的数据发送给另一个进程为例,对本申请实施例提供的进程间通信方法的应用场景进行介绍。Here, an application scenario of the inter-process communication method provided by the embodiment of the present application is introduced by taking one process sending its data to another process as an example.

如图1所示,进程A和进程B均位于用户空间,且进程A和进程B为两个不同的进程,进程A中包括数据A,当进程B也想使用进程A中的数据A时,由于进程A和进程B为不同的进程,存在进程隔离,所以进程B无法直接使用进程A中的数据A,若要实现进程B可以使用进程A中的数据A,必须采用特殊的通信机制,即IPC。As shown in Figure 1, both process A and process B are located in user space, and process A and process B are two different processes. Process A includes data A. When process B also wants to use data A in process A, Since process A and process B are different processes, there is process isolation, so process B cannot directly use data A in process A. If process B can use data A in process A, a special communication mechanism must be adopted, namely ipc.

可以理解,在进程A提供数据A给进程B的情况下,进程A为客户端/服务器(client/server,C/S)模式中的服务端进程,进程B为C/S模式中的客户端进程。同理,若进程B提供自己的数据B给进程A,则进程B为C/S模式中的服务端进程,进程A为C/S模式中的客户端进程。在C/S模式中,运行在客户端的进程为客户端进程,运行在服务器的进程为服务器进程。It can be understood that when process A provides data A to process B, process A is the server process in the client/server (C/S) mode, and process B is the client in the C/S mode. process. Similarly, if process B provides its own data B to process A, process B is the server process in the C/S mode, and process A is the client process in the C/S mode. In the C/S mode, the process running on the client is the client process, and the process running on the server is the server process.

目前,为了使不同的进程能够互相访问并协调工作,操作系统提供了多种IPC机制,例如套接字(socket)、管道、消息队列、共享内存以及结合剂(binder)等。At present, in order to enable different processes to access each other and coordinate their work, the operating system provides a variety of IPC mechanisms, such as sockets, pipes, message queues, shared memory, and binders.

其中,用于进程间通信的socket是一个抽象层,进程A和进程B可以通过socket发送或者接收数据,进程A和进程B可对socket进行像对文件一样的打开、读写和关闭等操作。socket可以看成是两个网络进程进行通信时,各自通信连接中的一个端点。通信时,进程A将要传输的数据A写入进程A所在主机的socket中,该socket通过网络接口卡的传输介质将数据A发送给进程B所在主机的socket中,使数据A能传送到进程B,从而实现进程A和进程B之间的通信。但是进程在使用socket进行通信的过程中,进程需要执行系统调用陷入内核空间才能进行数据传输,传输效率较低。因此,socket主要用于跨网络的不同主机上的进程间通信和同一台主机上进程间的低速通信。Among them, the socket used for inter-process communication is an abstraction layer. Process A and process B can send or receive data through the socket. Process A and process B can open, read, write, and close the socket as if they were files. A socket can be regarded as an endpoint in the respective communication connection when two network processes communicate. During communication, process A writes the data A to be transmitted into the socket of the host where process A is located, and the socket sends data A to the socket of the host where process B is located through the transmission medium of the network interface card, so that data A can be transmitted to process B. , so as to realize the communication between process A and process B. However, in the process of using sockets to communicate, the process needs to execute system calls and get trapped in the kernel space to perform data transmission, and the transmission efficiency is low. Therefore, sockets are mainly used for inter-process communication on different hosts across the network and low-speed communication between processes on the same host.

用于进程间通信的管道是尤尼克斯(unix)IPC的最古老形式,是一种在内存中的特殊文件。管道分为命名管道和匿名管道,命名管道允许毫不相干的两个进程之间进行通信,匿名管道只能单向通信,且只能在有亲缘关系的进程间使用,常用于父子进程。管道存在的问题请参见图2以及相关描述。A pipe used for interprocess communication is the oldest form of Yonex (unix) IPC, a special in-memory file. Pipes are divided into named pipes and anonymous pipes. Named pipes allow communication between two unrelated processes. Anonymous pipes can only communicate in one direction and can only be used between related processes. They are often used in parent and child processes. See Figure 2 and related descriptions for problems with the pipeline.

用于进程间通信的消息队列是消息的链表,存放在内核中并由消息队列标识符标识。通过使用消息队列,进程可以按顺序读信息,或为消息安排优先级顺序。消息队列存在的问题也请参见图2以及相关描述。A message queue used for interprocess communication is a linked list of messages, stored in the kernel and identified by a message queue identifier. By using message queues, processes can read messages sequentially, or prioritize messages. See also Figure 2 and related descriptions for the problems with message queues.

如图2所示,用于进程间通信的管道和消息队列,在进行数据传输时,通常是进程A在自己的虚拟地址空间开辟出一块内存缓存区,进程A将数据A存放在该内存缓存区中,然后,进程A通过系统调用陷入内核空间,由内核调用从用户态拷贝到内核态(copy fromuser)函数将数据A从进程A的内存缓存区拷贝到内核开辟的内核缓存区中。在内核将数据A拷贝至内核缓存区之后,内核再调用从内核态拷贝到用户态(copy to user)函数将数据A从内核缓存区拷贝到进程B开辟的内存缓存区中。这样进程A和进程B就完成了一次数据A的传输,也就是说,进程A和进程B完成了一次进程间通信。As shown in Figure 2, the pipelines and message queues used for inter-process communication, during data transmission, usually process A opens up a memory cache area in its own virtual address space, and process A stores data A in the memory cache. Then, process A falls into the kernel space through a system call, and the kernel calls the copy from user function to copy data A from the memory buffer area of process A to the kernel buffer area opened by the kernel. After the kernel copies the data A to the kernel buffer, the kernel calls the copy to user function from the kernel mode to copy the data A from the kernel buffer to the memory buffer opened by the process B. In this way, process A and process B complete a data A transmission, that is, process A and process B complete an inter-process communication.

可以看出,用于进程间通信的管道和消息队列,在进行数据传输时,进程A也需要执行系统调用陷入内核空间才能进行数据传输,而且内核需要将数据A从进程A的内存缓存区拷贝到内核缓存区,再从内核缓存区拷贝到进程B的内存缓存区,该过程需要进行2次数据拷贝,数据传输的效率较低。It can be seen that the pipes and message queues used for inter-process communication, when performing data transmission, process A also needs to execute a system call to fall into the kernel space to perform data transmission, and the kernel needs to copy data A from the memory buffer area of process A. To the kernel buffer area, and then copy from the kernel buffer area to the memory buffer area of process B, this process requires two data copies, and the efficiency of data transmission is low.

用于进程间通信的共享内存,就是允许进程A和进程B访问的同一个物理内存,进程A和进程B可以将该同一段物理内存映射到他们自己的虚拟地址空间,然后进程A和进程B均可以访问该物理内存。Shared memory used for inter-process communication is the same physical memory that process A and process B are allowed to access. Process A and process B can map the same piece of physical memory into their own virtual address space, and then process A and process B can access the physical memory.

在进程A通过共享内存向进程B传输数据A时,进程A将数据A写入自己的虚拟地址空间,通过进程A的虚拟地址空间与共享内存的映射关系,进程A的虚拟地址空间中的数据A被映射至共享内存,然后,再通过进程B的虚拟地址空间与共享内存的映射关系,共享内存中的数据A被映射至进程B的虚拟地址空间。也就是说,进程A在自己的虚拟地址空间写入数据A,数据A将立即被映射到可以访问同一个共享内存的进程B的虚拟地址空间,从而被进程B及时读取。When process A transmits data A to process B through shared memory, process A writes data A into its own virtual address space, and through the mapping relationship between process A's virtual address space and shared memory, the data in process A's virtual address space A is mapped to the shared memory, and then, through the mapping relationship between the virtual address space of process B and the shared memory, the data A in the shared memory is mapped to the virtual address space of process B. That is to say, when process A writes data A in its own virtual address space, data A will be immediately mapped to the virtual address space of process B that can access the same shared memory, so that it can be read by process B in time.

可以看出,用于进程间通信的共享内存仅仅通过映射便可实现数据传输,无需进行数据拷贝,因此能够实现进程间的高效通信,是进程之间共享和传递数据的一种非常有效的方式。但是共享内存控制复杂,比如在进程A对共享内存进行写操作时,并无自动机制可以阻止进程B对共享内存进行读操作,同时,若是有多个进程同时对共享内存进行写操作或读操作,多个进程之间争抢资源,会导致共享内存中的数据错乱。因此,在现有技术中,通常需要使用读写锁或互斥锁等其他机制,确保在进程A对共享内存进行写操作时,进程B不能对共享内存进行读操作,以及防止多个进程同时对共享内存进行读操作或写操作引起的资源争抢。It can be seen that the shared memory used for inter-process communication can realize data transmission only through mapping without data copying, so efficient communication between processes can be achieved, which is a very effective way to share and transfer data between processes . However, shared memory control is complicated. For example, when process A writes to the shared memory, there is no automatic mechanism to prevent process B from reading the shared memory. At the same time, if multiple processes simultaneously write or read the shared memory , multiple processes compete for resources, which will cause the data in the shared memory to be chaotic. Therefore, in the prior art, it is usually necessary to use other mechanisms such as read-write locks or mutex locks to ensure that when process A writes to the shared memory, process B cannot read the shared memory, and to prevent multiple processes from simultaneously Resource contention caused by read or write operations to shared memory.

另外,以上几种用于进程间通信的机制,在进行数据传输时,进程A以及进程B无法获得对方可靠的用户标识(user identification,UID)或者进程标识(processidentification,PID),也就是说,进程A以及进程B无法对对方的身份进行可靠的验证。同时,以上几种通信机制的访问接入点均是开放的,无法在进程A和进程B之间建立私有通信通道,恶意程序可以通过猜测进程A或者进程B的地址,与另一个进程建立连接,窃取消息。可以看出,以上几种用于进程间通信的机制,还存在着安全性低的问题。In addition, with the above mechanisms for inter-process communication, during data transmission, process A and process B cannot obtain each other's reliable user identification (user identification, UID) or process identification (process identification, PID), that is, Process A and process B cannot reliably verify the identity of each other. At the same time, the access points of the above communication mechanisms are all open, and a private communication channel cannot be established between process A and process B. A malicious program can establish a connection with another process by guessing the address of process A or process B. , stealing messages. It can be seen that the above mechanisms for inter-process communication still have the problem of low security.

接下来,对目前比较流行的用于进程间通信的binder机制进行介绍,binder机制是安卓(android)操作系统中的进程间通信机制,常用于android设备。Next, the currently popular binder mechanism for inter-process communication is introduced. The binder mechanism is an inter-process communication mechanism in the Android (android) operating system and is often used in android devices.

如图3所示,一个基于binder机制的IPC系统通常包括四部分:客户端、服务端、binder驱动、服务管理模块,可以看出,binder机制所采用的服务模式也是C/S模式。其中,As shown in Figure 3, an IPC system based on the binder mechanism usually includes four parts: client, server, binder driver, and service management module. It can be seen that the service mode adopted by the binder mechanism is also the C/S mode. in,

服务端进程(如进程A):提供服务(如数据A)给客户端进程使用的进程。Server process (such as process A): a process that provides services (such as data A) for use by client processes.

客户端进程(如进程B):想要使用服务端进程中的服务(如数据A)的进程。Client process (such as process B): The process that wants to use the service (such as data A) in the server process.

binder驱动:客户端进程可以通过binder驱动获取到服务端进程中的某个服务,然后客户端可以调用该服务获取结果。Binder driver: The client process can obtain a service in the server process through the binder driver, and then the client can call the service to obtain the result.

服务管理模块:服务端进程若要为客户端进程提供服务,则服务端进程需要先在服务管理模块提交自己的UID或者PID等进行注册,并将自己可以提供的服务在服务管理模块进行登记,以使服务管理模块获知服务端进程有哪些服务可以提供给客户端进程,当客户端进程要使用服务端进程中的服务时,也需要先在服务管理模块提交自己的UID或者PID等进行注册,然后可以去服务管理模块获取服务。Service management module: If the server process wants to provide services for the client process, the server process needs to submit its own UID or PID for registration in the service management module, and register the services it can provide in the service management module. In order to let the service management module know which services the server process can provide to the client process, when the client process wants to use the services in the server process, it also needs to submit its own UID or PID for registration in the service management module. Then you can go to the service management module to get the service.

从图3还可以看出,用于进程间通信的binder机制中用到了共享内存,该共享内存是通过内存映射实现的。It can also be seen from Figure 3 that shared memory is used in the binder mechanism for inter-process communication, and the shared memory is implemented through memory mapping.

binder机制中通过共享内存实现的数据传输的过程为:The process of data transmission through shared memory in the binder mechanism is:

进程A通过调用copy from user函数将数据A从进程A的虚拟地址空间拷贝至内核缓存区,由于内核缓存区和数据接收缓存区之间存在映射,所以内核缓存区的数据A可以直接被映射至数据接收缓存区,由于数据接收缓存区与进程B的虚拟地址空间存在映射,所以数据接收缓存区的数据A直接被映射至进程B的虚拟地址空间,这样进程A和进程B就完成了一次数据A的传输,也就是说,进程A和进程B完成了一次进程间通信。可以看出,该数据传输过程内核需要执行系统调用将数据A从进程A的虚拟地址空间拷贝至内核缓存区,也就是说,该数据传输过程需要进行一次数据拷贝。Process A copies data A from the virtual address space of process A to the kernel buffer area by calling the copy from user function. Since there is a mapping between the kernel buffer area and the data receiving buffer area, the data A in the kernel buffer area can be directly mapped to The data receiving buffer area, since the data receiving buffer area is mapped to the virtual address space of process B, the data A in the data receiving buffer area is directly mapped to the virtual address space of process B, so that process A and process B complete the data once A's transmission, that is, process A and process B complete an inter-process communication. It can be seen that in this data transmission process, the kernel needs to execute a system call to copy data A from the virtual address space of process A to the kernel cache area, that is, in this data transmission process, a data copy is required.

另外,从图3还可以看出,客户端进程、服务端进程和服务管理模块运行在用户空间,binder驱动运行在内核空间。客户端进程、服务端进程和服务管理模块之间的交互是虚线,表示客户端进程、服务端进程和服务管理模块之间不是直接交互的,而是通过binder驱动进行交互的,也是就说,客户端进程、服务端进程和服务管理模块之间的交互需要通过系统调用陷入内核空间,通过位于内核空间的binder驱动进行交互。In addition, it can be seen from Figure 3 that the client process, the server process and the service management module run in the user space, and the binder driver runs in the kernel space. The interaction between the client process, the server process and the service management module is a dotted line, which means that the client process, the server process and the service management module do not interact directly, but interact through the binder driver, that is to say, The interaction between the client process, the server process and the service management module needs to fall into the kernel space through system calls, and interact through the binder driver located in the kernel space.

由上述对用于进程间通信的binder机制的介绍可知,进程A和进程B在通过binder机制进行通信时,进程A和进程B均需在服务管理模块进行注册登记,然后进程A和进程B之间才能进行通信,可以看出,binder机制支持对进程A和进程B的身份进行校验,能够提升进程间通信的安全性。另外,用于进程间通信的binder机制中引入了共享内存,提升了进程间通信的效率。但是,用于进程间通信的binder机制在通信过程中也引入了内核态,在进行数据传输时,也需要进行一次数据拷贝,对操作系统的性能有影响,无法满足某些需要进行零次数据拷贝的进程间通信场景。同时,binder机制是android操作系统中的IPC机制,不适用于基于linux、视窗(windows)等操作系统构建的终端设备。From the above introduction to the binder mechanism for inter-process communication, when process A and process B communicate through the binder mechanism, both process A and process B need to be registered in the service management module, and then process A and process B are connected. It can be seen that the binder mechanism supports the verification of the identities of process A and process B, which can improve the security of inter-process communication. In addition, shared memory is introduced into the binder mechanism for inter-process communication, which improves the efficiency of inter-process communication. However, the binder mechanism used for inter-process communication also introduces the kernel state in the communication process. When data transmission is performed, a data copy is also required, which has an impact on the performance of the operating system and cannot meet some needs for zero-time data. Copies of interprocess communication scenarios. At the same time, the binder mechanism is an IPC mechanism in the android operating system, and is not applicable to terminal devices built on operating systems such as linux and windows.

另外,以上除了共享内存之外的其他几种进程间通信机制,进程之间在进行数据传输时,均需要进程执行系统调用陷入内核空间才能进行数据传输,引入了内核态,在将上述除共享内存之外的其他几种进程间通信机制应用于操作系统多样的嵌入式设备时,需要对嵌入式设备的单板和操作系统进行适配,操作复杂,可移植性低。而且,若内核态出现问题,新的进程间通信将脱离内核态,出现混乱。In addition, for the above several other inter-process communication mechanisms except shared memory, when data transmission is performed between processes, the process needs to execute system calls and fall into the kernel space to perform data transmission. The kernel state is introduced. When several other inter-process communication mechanisms other than memory are applied to embedded devices with various operating systems, it is necessary to adapt the single board of the embedded device and the operating system, which is complicated in operation and low in portability. Moreover, if there is a problem in the kernel state, the new inter-process communication will be separated from the kernel state, resulting in confusion.

为了解决上述进程间通信机制中存在的安全可靠性低,或者由于引入内核态导致的性能低等问题,本申请提供一种进程间通信方法,不仅能够提升进程间通信的安全可靠性,还可以有效地提升进程间通信的性能。本申请实施例提供的进程间通信方法不仅适用于基于android操作系统构建的终端设备,还适用于基于linux、windows等操作系统构建的终端设备,此处不作具体限定。In order to solve the problems of low security and reliability in the above-mentioned inter-process communication mechanism, or low performance due to the introduction of kernel mode, the present application provides an inter-process communication method, which can not only improve the security and reliability of inter-process communication, but also can Effectively improve the performance of inter-process communication. The inter-process communication method provided by the embodiments of the present application is not only applicable to terminal devices constructed based on the android operating system, but also applicable to terminal devices constructed based on operating systems such as linux and windows, which are not specifically limited here.

下面对本申请实施例提供的进程间通信方法进行详细阐述。The inter-process communication method provided by the embodiment of the present application is described in detail below.

请参见图4,图4是本申请实施例提供的一种进程间通信方法的流程示意图,如图4所示,该方法可以包括:Please refer to FIG. 4. FIG. 4 is a schematic flowchart of an inter-process communication method provided by an embodiment of the present application. As shown in FIG. 4, the method may include:

S101、第一进程创建第二进程以及第三进程之间的第一共享内存。S101. The first process creates a first shared memory between the second process and the third process.

其中,第一进程、第二进程以及第三进程运行在用户空间,第一进程对第一共享内存具有读写权限,第二进程对第一共享内存具有读写权限,第三进程对第一共享内存具有只读权限,第二进程和第三进程均在第一进程中注册。The first process, the second process and the third process run in user space, the first process has read and write permissions to the first shared memory, the second process has read and write permissions to the first shared memory, and the third process has read and write permissions to the first shared memory. The shared memory has read-only permission, and both the second process and the third process are registered in the first process.

第二进程和第三进程均在第一进程中注册,也就是说,在第一进程创建第二进程以及第三进程之间的第一共享内存之前,第二进程和第三进程需要先在第一进程中注册。这里,对第二进程和第三进程在第一进程中注册的过程进行描述。Both the second process and the third process are registered in the first process, that is, before the first process creates the first shared memory between the second process and the third process, the second process and the third process need to be registered in the first process. Register in the first process. Here, the process of registering the second process and the third process in the first process is described.

以第二进程在第一进程中进行注册为例,具体地,第一进程和第二进程可以通过域套接字(domain socket)通信机制建立连接,在第一进程和第二进程建立连接之后,第一进程为第二进程进行注册。其中,domain socket,是在socket的框架上发展出一种IPC机制,用于同一主机的进程间通信。Taking the registration of the second process in the first process as an example, specifically, the first process and the second process may establish a connection through a domain socket (domain socket) communication mechanism, and after the first process and the second process establish a connection , the first process registers for the second process. Among them, domain socket is an IPC mechanism developed on the framework of socket, which is used for inter-process communication of the same host.

更具体地,第一进程可以通过domain socket通信机制中的监听(listen)函数监听是否有进程向其发送了注册请求,在第一进程监听到第二进程向其发送了注册请求的情况下,第一进程可以通过domain socket机制中的接收(accept)函数接收注册请求,与第二进程建立连接。在第一进程与第二进程建立连接之后,第一进程可以根据注册请求为第二进程进行注册。其中,第二进程向第一进程发送注册请求可以通过domain socket通信机制中的连接(connect)函数向第一进程发送。More specifically, the first process can use the listen function in the domain socket communication mechanism to monitor whether a process sends a registration request to it. When the first process monitors that the second process sends a registration request to it, The first process may receive the registration request through the accept function in the domain socket mechanism, and establish a connection with the second process. After the first process establishes the connection with the second process, the first process may register the second process according to the registration request. The registration request sent by the second process to the first process may be sent to the first process through a connect (connect) function in the domain socket communication mechanism.

其中,第二进程发送的注册请求中可以包括第二进程的UID或者PID等信息,用于第一进程对第二进程的身份进行校验,还可以用于第一进程根据第二进程的UID或者PID等信息为第二进程进行注册。The registration request sent by the second process may include information such as the UID or PID of the second process, which is used by the first process to verify the identity of the second process, and can also be used by the first process according to the UID of the second process. Or information such as PID is registered for the second process.

可以理解,第三进程在第一进程中注册的过程,与第二进程在第一进程中注册的过程相类似,具体实现可以参考上述实施例中描述的第二进程在第一进程中注册的过程,此处不再展开赘述。It can be understood that the process of registering the third process in the first process is similar to the process of registering the second process in the first process. For specific implementation, reference may be made to the process of registering the second process in the first process described in the foregoing embodiment. The process will not be repeated here.

在第一进程为第二进程进行注册的过程中,第一进程还可以根据注册请求中包括的第二进程的UID或者PID等信息创建第一共享内存,第一共享内存用于实现第二进程向其他进程(如第三进程)的单向通信。同理,在第一进程为第三进程进行注册的过程中,第一进程也可以根据第三进程的UID或者PID等信息创建第二共享内存,第二共享内存用于实现第三进程向其他进程(如第二进程)的单向通信。During the process of registering the second process by the first process, the first process may also create a first shared memory according to information such as the UID or PID of the second process included in the registration request, and the first shared memory is used to implement the second process One-way communication to other processes (such as a third process). Similarly, in the process of registering the third process by the first process, the first process can also create a second shared memory according to the information such as the UID or PID of the third process, and the second shared memory is used to enable the third process to communicate with other processes. One-way communication for a process (such as a second process).

在本申请实施例中,第一进程创建第一共享内存,是通过第一进程创建第一共享文件实现的,第一共享文件为一个真实存在于物理内存的文件,第一共享内存的物理地址和第一共享文件的物理地址相同。同理,第一进程创建第二共享内存,也是通过第一进程创建第二共享文件实现的,第二共享文件也为一个真实存在于物理内存的文件,第二共享内存的物理地址和第二共享文件的物理地址相同。In the embodiment of the present application, the first shared memory is created by the first process by creating a first shared file by the first process. The first shared file is a file that actually exists in the physical memory, and the physical address of the first shared memory is Same as the physical address of the first shared file. Similarly, the creation of the second shared memory by the first process is also achieved through the creation of the second shared file by the first process. The second shared file is also a file that actually exists in the physical memory. The physical address of the second shared memory is the same as the second shared memory. The physical addresses of the shared files are the same.

其中,第一共享内存和第二共享内存也可以具有共享内存标识,共享内存标识用以唯一标识一个共享内存,共享内存标识可以根据共享内存的物理地址或者进程的UID或者PID等信息来命名。例如,第一共享内存的标识可以根据第二进程的UID或者PID等信息来命名,第二共享内存的标识可以根据第三进程的UID或者PID等信息来命名。第一共享文件和第二共享文件也可以具有共享文件标识,共享文件标识用以唯一标识一个共享文件,共享文件标识可以根据共享文件的物理地址或者进程的UID或者PID等信息来命名。例如,第一共享文件的标识可以根据第二进程的UID或者PID等信息来命名,第二共享文件的标识可以根据第三进程的UID或者PID等信息来命名。The first shared memory and the second shared memory may also have a shared memory identifier, the shared memory identifier is used to uniquely identify a shared memory, and the shared memory identifier can be named according to information such as the physical address of the shared memory or the UID or PID of the process. For example, the identifier of the first shared memory may be named according to information such as the UID or PID of the second process, and the identifier of the second shared memory may be named according to information such as the UID or PID of the third process. The first shared file and the second shared file may also have a shared file identifier. The shared file identifier is used to uniquely identify a shared file. The shared file identifier can be named according to the physical address of the shared file or the UID or PID of the process. For example, the identifier of the first shared file may be named according to information such as the UID or PID of the second process, and the identifier of the second shared file may be named according to information such as the UID or PID of the third process.

在具体实现中,第一进程还可以保存第二进程的UID或者PID等信息、第三进程的UID或者PID等信息、第一共享内存标识以及第二共享内存标识等。当第一进程需要管理第二进程或者第三进程时,第一进程可以根据进程的UID或者PID等信息对进程进行管理,进而可以满足进程的管理需求;当第一进程需要管理第一共享内存或者第二共享内存时,第一进程可以根据共享内存标识找到对应的共享内存进行管理,进而可以满足共享内存的管理需求。In a specific implementation, the first process may also save information such as the UID or PID of the second process, information such as the UID or PID of the third process, the first shared memory identifier, the second shared memory identifier, and the like. When the first process needs to manage the second process or the third process, the first process can manage the process according to information such as the UID or PID of the process, so as to meet the management requirements of the process; when the first process needs to manage the first shared memory Or when the second shared memory is used, the first process can find the corresponding shared memory for management according to the shared memory identifier, so as to meet the management requirements of the shared memory.

在本申请实施例中,在第一进程创建完成第一共享内存之后,第一进程作为第一共享内存的创建者,也就是所有者,第一进程不仅具有第一共享内存的读写权限,第一进程还具有为其他进程配置第一共享内存的权限。例如,第一进程可以配置第二进程具有第一共享内存的读写权限,以及配置第三进程具有第一共享内存的只读权限。其中,进程具有第一共享内存的读写权限,表示进程可以对第一共享内存进行读操作和写操作。进程具有第一共享内存的只读权限,表示进程只能对第一共享内存进行读操作,不能进行写操作。In the embodiment of the present application, after the first process completes the creation of the first shared memory, the first process acts as the creator of the first shared memory, that is, the owner, and the first process not only has the read and write permissions of the first shared memory, The first process also has the right to configure the first shared memory for other processes. For example, the first process may configure the second process to have read and write permissions of the first shared memory, and configure the third process to have read-only permissions of the first shared memory. Wherein, the process has the read and write authority of the first shared memory, which means that the process can perform read and write operations on the first shared memory. The process has the read-only permission of the first shared memory, which means that the process can only perform read operations on the first shared memory and cannot perform write operations.

由上述实施例可知,第一共享内存对应于第一共享文件,因此,第一进程可以通过配置第二进程具有第一共享文件的读写权限,使得第二进程具有第一共享内存的读写权限,以及通过配置第三进程具有第一共享文件的只读权限,使得第三进程具有第一共享内存的只读权限。It can be seen from the above embodiment that the first shared memory corresponds to the first shared file. Therefore, the first process can configure the second process to have the read and write permissions of the first shared file, so that the second process has the read and write permissions of the first shared memory. permission, and by configuring the third process to have the read-only permission of the first shared file, the third process has the read-only permission of the first shared memory.

具体地,第一进程可以通过访问控制列表(access control lists,ACL)配置第二进程具有第一共享内存的读写权限,以及配置第三进程具有第一共享内存的只读权限。其中,ACL是一个针对文件/目录的访问控制列表,ACL允许给任何的用户(进程)设置任何文件/目录的访问权限,它由一系列规则组成。访问权限包括可读r(read)、可写w(write)或者可执行x(execute)三种权限。可读r表示能够读取文件的实际内容;可写w表示能够编辑、修改、新增、删除文件的实际内容,但不能删除该文件;可执行x表示文件具有可以被系统执行的权限。Specifically, the first process may configure the second process to have read-write permission of the first shared memory, and configure the third process to have read-only permission of the first shared memory through an access control list (access control list, ACL). Among them, ACL is an access control list for files/directories. ACL allows any user (process) to set access rights to any file/directory, and it consists of a series of rules. Access permissions include three permissions: readable r (read), writable w (write), or executable x (execute). Readable r means that the actual content of the file can be read; writable w means that the actual content of the file can be edited, modified, added, and deleted, but the file cannot be deleted; executable x means that the file has permission to be executed by the system.

具体地,第一进程可以通过ACL中的设置文件ACL权限(setfacl)命令设置第二进程和第三进程作为第一共享文件的其他人,并设置第二进程具有第一共享文件的读写权限(rw-),设置第三进程具有第一共享文件的只读权限(r--)。在设置完成后,第一进程可以通过ACL中的查看文件ACL权限(getfacl)命令查看第二进程是否具有了第一共享文件的读写权限(rw-),以及查看第三进程是否具有了第一共享文件的只读权限(r--)。第二进程具有第一共享文件的读写权限,表示第二进程具有了第一共享内存的读写权限。第三进程具有第一共享文件的只读权限,表示第三进程具有了第一共享内存的只读权限。Specifically, the first process can set the second process and the third process as others of the first shared file through the set file ACL permission (setfacl) command in the ACL, and set the second process to have read and write permissions of the first shared file (rw-), set the third process to have the read-only permission (r--) of the first shared file. After the setting is completed, the first process can check whether the second process has the read and write permission (rw-) of the first shared file through the view file ACL permission (getfacl) command in the ACL, and whether the third process has the first shared file. Read-only permission (r--) for a shared file. The second process has the read and write authority of the first shared file, which means that the second process has the read and write authority of the first shared memory. The third process has the read-only permission of the first shared file, which means that the third process has the read-only permission of the first shared memory.

在第二进程具有第一共享内存的读写权限之后,第二进程若要对第一共享内存进行读写操作,第二进程还需建立自己的虚拟地址空间与第一共享内存之间的映射关系。在映射关系建立之后,第二进程才能对第一共享内存进行读写操作。同理,在第三进程具有第一共享内存的只读权限之后,第三进程若要对第一共享内存进行读操作,第三进程也需建立自己的虚拟地址空间与第一共享内存之间的映射关系,在映射关系建立之后,第三进程才能对第一共享内存进行读操作。After the second process has the read and write permission of the first shared memory, if the second process wants to perform read and write operations on the first shared memory, the second process also needs to establish a mapping between its own virtual address space and the first shared memory relation. After the mapping relationship is established, the second process can perform read and write operations on the first shared memory. Similarly, after the third process has the read-only permission of the first shared memory, if the third process wants to read the first shared memory, the third process also needs to establish its own virtual address space and the first shared memory. After the mapping relationship is established, the third process can read the first shared memory.

在本申请实施例中,第二进程和第三进程可以通过内存映射(memory map,简称mmap)机制建立自己的虚拟地址空间与第一共享内存的映射关系。其中,mmap是一种内存映射文件的方法,即将一个文件映射到进程的虚拟地址空间,实现文件地址和进程的虚拟地址空间的一一对映关系。实现这样的映射关系后,进程就可以采用指针的方式对虚拟地址空间进行读写操作,从而实现对文件的读写操作。In this embodiment of the present application, the second process and the third process may establish a mapping relationship between their own virtual address space and the first shared memory through a memory map (memory map, mmap for short) mechanism. Among them, mmap is a method of memory mapping files, that is, a file is mapped to the virtual address space of the process, and a one-to-one mapping relationship between the file address and the virtual address space of the process is realized. After implementing such a mapping relationship, the process can use the pointer to read and write operations to the virtual address space, so as to realize the read and write operations to the file.

接下来,对第二进程和第三进程通过mmap机制建立自己的虚拟地址空间与第一共享内存的映射关系的过程进行详细描述。Next, the process of establishing the mapping relationship between their own virtual address space and the first shared memory by the second process and the third process through the mmap mechanism will be described in detail.

以第二进程通过mmap机制建立自己的虚拟地址空间与第一共享内存的映射关系为例,该过程可以包括:Taking the second process establishing the mapping relationship between its own virtual address space and the first shared memory through the mmap mechanism as an example, the process may include:

第一步、第二进程在用户空间调用库函数mmap。The first and second processes call the library function mmap in user space.

第二步、第二进程在自己的虚拟地址空间开辟第一映射区域。In the second step, the second process opens up a first mapping area in its own virtual address space.

其中,第一映射区域为第二进程虚拟地址空间中与第一共享内存建立映射关系的区域。The first mapping area is an area in the virtual address space of the second process that establishes a mapping relationship with the first shared memory.

第三步、第二进程为第一映射区域分配一个虚拟区结构,接着对虚拟区结构的各个域进行初始化。In the third step, the second process allocates a virtual area structure to the first mapping area, and then initializes each field of the virtual area structure.

第四步、第二进程将虚拟区结构插入第二进程的第一映射区域的链表或树中。In the fourth step, the second process inserts the virtual area structure into the linked list or tree of the first mapping area of the second process.

第五步、第二进程通过待映射的文件指针,在文件描述符表中找到第一共享文件的文件描述符,通过第一共享文件的文件描述符,链接到内核“已打开文件集”中第一共享文件的文件结构体。The fifth step, the second process finds the file descriptor of the first shared file in the file descriptor table through the file pointer to be mapped, and links to the kernel "open file set" through the file descriptor of the first shared file The file structure of the first shared file.

第六步、第二进程通过第一共享文件的文件结构体,链接到文件组织(fileoperations)模块,调用内核函数mmap。In the sixth step, the second process links to the file organization (file operations) module through the file structure of the first shared file, and calls the kernel function mmap.

第七步、第二进程通过虚拟文件系统索引(inode)模块定位到第一共享文件地址。In the seventh step, the second process locates the address of the first shared file through a virtual file system index (inode) module.

第八步、第二进程通过设备内存到用户空间地址的映射(remap_pfn_range)函数建立页表。In the eighth step, the second process establishes a page table through the function of mapping the device memory to the user space address (remap_pfn_range).

通过以上八步,即建立了第一映射区域与第一共享文件之间的映射关系,也就是建立了第一映射区域与第一共享内存之间的映射关系。Through the above eight steps, the mapping relationship between the first mapping area and the first shared file is established, that is, the mapping relationship between the first mapping area and the first shared memory is established.

可以理解,第三进程虚拟地址空间与第一共享内存之间的映射关系的建立过程,即第二映射区域与第一共享内存之间的映射关系的建立过程,与第一映射区域与第一共享内存的映射关系的建立过程相类似,此处不再展开赘述。其中,第二映射区域为第三进程虚拟地址空间中与第一共享内存建立映射关系的区域,为了便于描述,在本申请接下来的实施例中,以第一映射关系表示第一映射区域与第一共享内存之间的映射关系,以第二映射关系表示第二映射区域与第一共享内存之间的映射关系。It can be understood that the process of establishing the mapping relationship between the virtual address space of the third process and the first shared memory, that is, the process of establishing the mapping relationship between the second mapping area and the first shared memory, is the same as the process of establishing the mapping relationship between the first mapping area and the first shared memory. The process of establishing the mapping relationship of the shared memory is similar, and will not be repeated here. The second mapping area is an area in the virtual address space of the third process that establishes a mapping relationship with the first shared memory. For ease of description, in the following embodiments of the present application, the first mapping relationship is used to represent the first mapping area and the first shared memory. The mapping relationship between the first shared memories is represented by the second mapping relationship between the second mapping area and the first shared memory.

S102、第二进程将第一数据写入第一映射区域,并根据第一映射关系,将第一数据从第一映射区域映射至第一共享内存,第三进程根据第二映射关系,将第一数据从第一共享内存映射至第二映射区域,从而实现第二进程向第三进程的单向通信。S102. The second process writes the first data into the first mapping area, and maps the first data from the first mapping area to the first shared memory according to the first mapping relationship, and the third process maps the first data to the first shared memory according to the second mapping relationship A piece of data is mapped from the first shared memory to the second mapping area, thereby realizing one-way communication from the second process to the third process.

在第一映射关系和第二映射关系建立之后,第二进程将第一数据写入第一映射区域,然后根据第一映射关系,将第一数据从第一映射区域映射至第一共享内存,可以理解为,第二进程对第一共享内存进行写操作,将第一数据写入了第一共享内存。第三进程根据第二映射关系,将第一共享内存中的第一数据映射至第一映射区域,可以理解为,第三进程对第一共享内存进行读操作,从第一共享内存中读取了第一数据。After the first mapping relationship and the second mapping relationship are established, the second process writes the first data into the first mapping area, and then maps the first data from the first mapping area to the first shared memory according to the first mapping relationship, It can be understood that the second process performs a write operation on the first shared memory, and writes the first data into the first shared memory. The third process maps the first data in the first shared memory to the first mapping area according to the second mapping relationship. It can be understood that the third process performs a read operation on the first shared memory and reads from the first shared memory the first data.

在一个更具体的实施例中,第二进程可以包括写线程,用于在第一映射区域写入第一数据,第三进程可以包括读线程,用于从第二映射区域读取第一数据。写线程在第二进程有第一数据要写时才被激活,以通知第三进程有第一数据需要读取。读线程在第三进程有第一数据需要读取时才被激活,以读取第一数据,并在读取完毕后,通知第二进程第一数据读取完毕。In a more specific embodiment, the second process may include a writing thread for writing the first data in the first mapping area, and the third process may include a reading thread for reading the first data from the second mapping area . The writing thread is activated when the second process has the first data to be written, so as to notify the third process that the first data needs to be read. The read thread is activated when the third process has the first data to be read, so as to read the first data, and after the reading is completed, notifies the second process that the first data is read.

需要说明的是,第一数据写入第一共享内存后,会改变第一共享内存中的内容,但是第一数据并不会被立即写回到第一共享文件改变第一共享文件中的内容,而是有一段时间的延迟,一定时间后,操作系统会自动回写包含了第一数据的脏页面到对应的第一共享文件,即完成了写入第一数据到第一共享文件的过程。若要实现第一共享文件的内容与第一共享内存中的内容同步,可以调用刷新变化(msync)函数强制同步。这样,第二进程所写的第一数据就可以同步保存到第一共享文件中。It should be noted that after the first data is written into the first shared memory, the content in the first shared memory will be changed, but the first data will not be immediately written back to the first shared file to change the content in the first shared file. , but there is a delay for a period of time. After a certain period of time, the operating system will automatically write back the dirty page containing the first data to the corresponding first shared file, that is, the process of writing the first data to the first shared file is completed. . To synchronize the content of the first shared file with the content in the first shared memory, a refresh change (msync) function may be called to force synchronization. In this way, the first data written by the second process can be stored in the first shared file synchronously.

在具体实现中,第二进程通过第一共享内存向第三进程进行单向通信时,并不总是读写少量数据后就解除第一映射关系,或者解除第二映射关系,而是一直保持各自的映射区域与第一共享内存的映射关系,直到通信完毕为止。这样,第一数据一直保存在第一共享内存中,并没有被第二进程回收。第一数据往往是在第二进程与第三进程通信完毕,才被第二进程回收的。In a specific implementation, when the second process performs one-way communication with the third process through the first shared memory, it does not always release the first mapping relationship or release the second mapping relationship after reading and writing a small amount of data, but keeps the The mapping relationship between the respective mapping areas and the first shared memory until the communication is completed. In this way, the first data is always stored in the first shared memory and is not reclaimed by the second process. The first data is often collected by the second process after the second process and the third process have finished communicating.

第二进程回收第一数据,表示第二进程与第三进程之间的通信结束,第二进程可以解除第一映射关系,第三进程可以解除第二映射关系。在映射关系解除之后,第一进程可以释放第一共享内存所占的资源。The second process reclaims the first data, indicating that the communication between the second process and the third process ends, the second process can cancel the first mapping relationship, and the third process can cancel the second mapping relationship. After the mapping relationship is released, the first process may release the resources occupied by the first shared memory.

为了进一步提升第二进程和第三进程之间通信的性能,在一种具体的实施方式中,第一共享内存中还包括破坏者(disruptor)队列,第二进程将第一数据写入第一共享内存中,第三进程从第一共享内存中读取第一数据的具体过程为:In order to further improve the performance of communication between the second process and the third process, in a specific implementation manner, the first shared memory further includes a disruptor queue, and the second process writes the first data into the first shared memory. In the shared memory, the specific process for the third process to read the first data from the first shared memory is as follows:

A1、第二进程将第一数据写入第一映射区域。A1. The second process writes the first data into the first mapping area.

A2、第二进程将第一数据从第一映射区域映射至disruptor队列。A2. The second process maps the first data from the first mapping area to the disruptor queue.

具体地,第二进程根据第一映射关系,将第一数据从第一映射区域映射至第一共享内存中的disruptor队列。Specifically, the second process maps the first data from the first mapping area to the disruptor queue in the first shared memory according to the first mapping relationship.

A3、第三进程将第一数据从disruptor队列映射至第二映射区域。A3. The third process maps the first data from the disruptor queue to the second mapping area.

具体地,第三进程根据第二映射关系,将第一数据从disruptor队列映射至第二映射区域。Specifically, the third process maps the first data from the disruptor queue to the second mapping area according to the second mapping relationship.

可以看出,在本实施例中,第二进程向第三进程传输第一数据的过程是通过映射实现的,无需对第一数据进行数据拷贝,可以满足某些进程间通信场景需要进行零次数据拷贝的要求。It can be seen that, in this embodiment, the process of transmitting the first data from the second process to the third process is realized by mapping, and there is no need to copy the first data, which can meet the needs of zero-time communication in some inter-process communication scenarios. Data copy requirements.

在一种可能的实施方式中,第一共享内存中除了包括disruptor队列之外,还包括预设共享区域,该预设共享区域用于存储第一数据。第二进程将第一数据写入第一共享内存中,第三进程从第一共享内存中读取第一数据的具体过程为:In a possible implementation manner, in addition to the disruptor queue, the first shared memory further includes a preset shared area, where the preset shared area is used to store the first data. The second process writes the first data into the first shared memory, and the specific process for the third process to read the first data from the first shared memory is:

B1、第二进程将第一数据写入第一映射区域。B1. The second process writes the first data into the first mapping area.

B2、第二进程将第一数据从第一映射区域映射至预设共享区域。B2. The second process maps the first data from the first mapping area to the preset shared area.

具体地,第二进程根据第一映射关系,将第一数据从第一映射区域映射至第一共享内存中的预设共享区域。Specifically, the second process maps the first data from the first mapping area to a preset shared area in the first shared memory according to the first mapping relationship.

B2、第二进程将包含第一数据的存储信息在内的输出流从预设共享区域输出至disruptor队列,其中,第一数据的存储信息为第一数据在预设共享区域内的首地址信息和长度信息,或者,为第一数据在预设共享区域内的首地址信息和末地址信息。B2. The second process outputs the output stream including the storage information of the first data from the preset shared area to the disruptor queue, wherein the storage information of the first data is the first address information of the first data in the preset shared area and length information, or, the first address information and the last address information of the first data in the preset shared area.

B3、第三进程将第一数据的存储信息从disruptor映射至第二映射区域,B3. The third process maps the storage information of the first data from the disruptor to the second mapping area,

B4、第三进程根据第一数据的地址信息和长度信息从预设共享区域读取第一数据。B4. The third process reads the first data from the preset shared area according to the address information and the length information of the first data.

具体地,第三进程根据第二映射关系,将第一数据从disruptor队列映射至第二映射区域。Specifically, the third process maps the first data from the disruptor queue to the second mapping area according to the second mapping relationship.

可以看出,在本实施例中,在第二进程向第三进程传输第一数据的过程中,需要第二进程将包含第一数据的存储信息在内的输出流从预设共享内存区域输出至disruptor队列中,需要进行一次数据拷贝,可以满足某些进程间通信场景需要进行一次数据拷贝的要求。It can be seen that in this embodiment, in the process of transmitting the first data from the second process to the third process, the second process needs to output the output stream including the storage information of the first data from the preset shared memory area To the disruptor queue, a data copy needs to be performed, which can meet the requirement of a data copy in some inter-process communication scenarios.

接下来,对disruptor队列的工作原理进行介绍。Next, the working principle of the disruptor queue is introduced.

如图5所示,在图5中,线程A表示具有读写权限的第二进程中的写线程,线程B和线程C表示具有只读权限的两个进程中的读线程。As shown in FIG. 5 , in FIG. 5 , thread A represents a write thread in a second process with read-write permission, and thread B and thread C represent read threads in two processes with read-only permission.

如图5所示,在disruptor队列中,有一个环形缓冲区(ring buffer),该环形缓冲区的上面有很多个称为槽的位置,如图5中的槽1、槽2、槽3…槽10、槽11、槽12。环形缓冲区里的每一个数据都有一个序列号(sequence number)来索引,在具体实施中,可以设置序列号与槽的位置号相同。环形缓冲区维护当前最新写入的数据的序列号,这个序列号一直递增,例如,槽10和槽11已经被写入数据,环形缓冲区维护的最新写入的数据的序列号是11,事件处理器在接收到线程A要写入的数据A后,从环形缓冲区可以获知接下来要写入数据A的槽位是12,则线程A将要写入的数据A写入到12号槽位。写入之后,环形缓冲区才会将最后写入的数据A的序列号更新为12,并通知线程B和线程C可以读取序列号为12的数据。线程B和线程C在接收到通知后,根据接收到的数据A的序列号12来读取数据A。在线程B和线程C未接收到通知的情况下,线程B和线程C会进入等待状态,等待数据A被线程A写入环形缓冲区,并且,一旦数据A被写入环形缓冲区,线程B和线程C便会接到通知,然后线程B和线程C便可以根据接收的序列号12去读取数据A。As shown in Figure 5, in the disruptor queue, there is a ring buffer (ring buffer), and there are many positions called slots above the ring buffer, such as slot 1, slot 2, slot 3 in Figure 5... Slot 10, Slot 11, Slot 12. Each data in the ring buffer is indexed by a sequence number. In a specific implementation, the sequence number can be set to be the same as the slot position number. The ring buffer maintains the sequence number of the latest written data. This sequence number is always increasing. For example, data has been written to slot 10 and slot 11. The sequence number of the latest written data maintained by the ring buffer is 11. Event After receiving the data A to be written by thread A, the processor can learn from the ring buffer that the next slot to write data A is 12, then thread A writes the data A to be written to slot 12. . After writing, the ring buffer will update the sequence number of the last written data A to 12, and notify thread B and thread C that the data with sequence number 12 can be read. After receiving the notification, thread B and thread C read data A according to the sequence number 12 of the received data A. In the case that thread B and thread C do not receive the notification, thread B and thread C will enter the waiting state, waiting for data A to be written to the ring buffer by thread A, and, once data A is written to the ring buffer, thread B And thread C will be notified, and then thread B and thread C can read data A according to the received sequence number 12.

需要说明的是,线程B和线程C并不是直接去读取环形缓冲区的数据A的,而是根据接收到的序列号12调用序号栅栏(sequence barrier)对象的等待(wait for)方法,向sequence barrier对象传递它所需要访问的序列号12,由sequence barrier对象根据序列号12去获取环形缓冲区中的12号槽位的数据A之后再返回给线程B和线程C。It should be noted that thread B and thread C do not directly read the data A of the ring buffer, but call the wait for method of the sequence barrier object according to the received sequence number 12, The sequence barrier object transmits the sequence number 12 it needs to access, and the sequence barrier object obtains the data A in slot 12 in the ring buffer according to the sequence number 12, and then returns it to thread B and thread C.

可以理解,线程B和线程C是在接收到环形缓冲区的通知后才去读取数据A,在线程A未写完数据A的情况下,线程B和线程C不会读取数据A,因此可以防止线程A进行写操作的同时,线程B或者线程C进行读操作。同时,线程B和线程C是根据接收到的序列号12去sequence barrier对象读取数据A的,线程B和线程C并没有直接操作环形缓冲区,因此,对环形缓冲区来说,线程B和线程C不会产生资源竞争,也就无需对线程B和线程C进行加锁。It can be understood that thread B and thread C read data A after receiving the notification from the ring buffer. If thread A has not finished writing data A, thread B and thread C will not read data A, so It can prevent thread A from performing a write operation while thread B or thread C performs a read operation. At the same time, thread B and thread C read data A from the sequence barrier object according to the received sequence number 12. Thread B and thread C do not directly operate the ring buffer. Therefore, for the ring buffer, thread B and thread C do not directly operate the ring buffer. Thread C will not generate resource contention, so there is no need to lock thread B and thread C.

在具体应用中,环形缓冲区中槽的数量是可以允许自定义和设置的,环形缓冲区中的槽的数量为2n,其中,n为正整数。所以环形缓冲区可以是几万到几千万数量不等的数据缓冲规模,因此,在第一共享内存中使用disruptor队列,还可以避免线程A的写操作速度过快时,内存溢出的问题。In a specific application, the number of slots in the ring buffer can be customized and set, and the number of slots in the ring buffer is 2 n , where n is a positive integer. Therefore, the ring buffer can have a data buffer size ranging from tens of thousands to tens of millions. Therefore, using the disruptor queue in the first shared memory can also avoid the problem of memory overflow when the write operation speed of thread A is too fast.

本申请实施例提供的进程间通信方法,通过在第一共享内存中使用disruptor队列,可以确保在第二进程对第一共享内存进行写操作的时候,第三进程不能对第一共享内存进行读操作,而且也避免多个进程对第一共享内存进行读操作时锁的使用。同时,本申请在第一共享内存中使用disruptor队列,支持进程对第一共享内存的反复读写,能够有效地提高进程间信息的吞吐量,能够进一步提升进程间通信的性能。In the inter-process communication method provided by the embodiment of the present application, by using the disruptor queue in the first shared memory, it can be ensured that when the second process writes the first shared memory, the third process cannot read the first shared memory operation, and also avoids the use of locks when multiple processes perform read operations on the first shared memory. At the same time, the present application uses a disruptor queue in the first shared memory to support repeated reading and writing of the first shared memory by a process, which can effectively improve the throughput of inter-process information and further improve the performance of inter-process communication.

另外,还可以看出,本申请实施例提供的进程间通信方法,在第一共享内存中只包括disruptor队列的情况下,第二进程向第三进程的单向通信过程不需要进行数据拷贝;在第一共享内除了包括disruptor队列之外,还包括预设共享区域的情况下,第二进程向第三进程的单向通信过程需要进行一次数据拷贝。本申请实施例提供的进程间通信方法,不仅适用于需要进行零次数据拷贝的进程间通信场景,还适用于需要进行一次数据拷贝的进程间通信场景,灵活性佳。In addition, it can also be seen that, in the inter-process communication method provided by the embodiment of the present application, when the first shared memory only includes the disruptor queue, the one-way communication process from the second process to the third process does not require data copying; When the first share includes a preset shared area in addition to the disruptor queue, the one-way communication process from the second process to the third process needs to perform a data copy. The inter-process communication method provided by the embodiment of the present application is not only applicable to the inter-process communication scenario that requires zero data copies, but also to the inter-process communication scenario that requires one data copy, and has good flexibility.

为了进一步提升第二进程和第三进程之间通信的安全可靠性,在一种具体的实施方式中,在第二进程将第一数据写入第一映射区域之前,第二进程需要分配第一堆内存,第一堆内存用于存储第二进程的控制信息,然后第二进程从第一堆内存中获取第二进程的控制信息,以使得第二进程可以根据第二进程的控制信息将第一数据写入第一映射区域。在第三进程将第一数据从第一共享内存映射至第二映射区域之前,第三进程需要分配第二堆内存,第二堆内存用于存储第三进程的控制信息,然后第三进程从第二堆内存中获取第三进程的控制信息,以使得第三进程可以根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。In order to further improve the security reliability of the communication between the second process and the third process, in a specific implementation manner, before the second process writes the first data into the first mapping area, the second process needs to allocate the first Heap memory, the first heap memory is used to store the control information of the second process, and then the second process obtains the control information of the second process from the first heap memory, so that the second process can store the control information of the second process according to the control information of the second process. A data is written into the first mapping area. Before the third process maps the first data from the first shared memory to the second mapping area, the third process needs to allocate the second heap memory, and the second heap memory is used to store the control information of the third process, and then the third process needs to The control information of the third process is obtained from the second heap memory, so that the third process can map the first data from the first shared memory to the second mapping area according to the control information of the third process.

其中,第二进程的控制信息可以包括与第二进程进行通信的进程为第三进程的信息,第二进程的控制信息还可以包括第二进程具有的第一共享内存的读写权限信息等,此处不作具体限定。第三进程的控制信息可以包括与第三进程进行通信的进程为第二进程的信息,第三进程的控制信息还可以包括第三进程具有的第一共享内存的只读权限信息等,此处不作具体限定。Wherein, the control information of the second process may include information that the process communicating with the second process is the third process, and the control information of the second process may also include read and write permission information of the first shared memory owned by the second process, etc., There is no specific limitation here. The control information of the third process may include information that the process communicating with the third process is the second process, and the control information of the third process may also include read-only permission information of the first shared memory owned by the third process, etc. Here There is no specific limitation.

上述实施例中,通过将第二进程的控制信息存储在第一堆内存(第一进程的堆内存),将第三进程的控制信息存储在第二堆内存(第三进程的堆内存),在第二进程与第三进程进行通信时,第二进程和第三进程可以直接从自己的堆内存中获取自己的控制信息进行通信,进程间通信的效率高。同时,通过将第二进程的控制信息存储在第一堆内存,将第三进程的控制信息存储在第二堆内存,还可以实现第二进程的控制信息和第三进程的控制信息的分开存放,有利于第二进程和第三进程在运行时相互独立、互不干扰,在第二进程和第三进程中的其中一个进程出现故障时,故障不会扩散至另外一个进程。In the above embodiment, by storing the control information of the second process in the first heap memory (the heap memory of the first process), and storing the control information of the third process in the second heap memory (the heap memory of the third process), When the second process communicates with the third process, the second process and the third process can directly obtain their own control information from their own heap memory for communication, and the efficiency of inter-process communication is high. At the same time, by storing the control information of the second process in the first heap memory and storing the control information of the third process in the second heap memory, the control information of the second process and the control information of the third process can also be stored separately. , which is beneficial for the second process and the third process to be independent and non-interfering with each other during operation. When one of the second process and the third process fails, the failure will not spread to the other process.

在实际应用中,第二进程和第三进程可以调用内存分配(memory allocation,简写为malloc)函数或者创建(new)函数分配得到对应的第一堆内存和第二堆内存,第二进程可以决定分配的第一堆内存的大小,第三进程可以决定分配的第二堆内存的大小,第二进程和第三进程还可以通过释放(free)函数或者删除(delete)函数释放对应的第一堆内存和第二堆内存。In practical applications, the second process and the third process can call the memory allocation (memory allocation, abbreviated as malloc) function or create (new) function to allocate the corresponding first heap memory and second heap memory, and the second process can decide The size of the allocated first heap memory, the third process can decide the size of the allocated second heap memory, the second process and the third process can also release the corresponding first heap through the release (free) function or the delete (delete) function memory and the second heap.

需要说明的是,进程分配堆内存,是从进程自己的虚拟地址空间中分配的。例如,第二进程分配的第一堆内存,是第二进程自己的虚拟地址空间中的内存。如图6所示,从图6可以看出,第二进程虚拟地址空间包括堆内存、栈内存和共享内存映射区域(第一映射区域)等。从图6可以看出,第一映射区域位于第二进程的堆内存(第一堆内存)和第二进程的栈内存之间。也就是说,第一堆内存与第一映射区域为第二进程虚拟地址空间中不同的两块内存。It should be noted that the heap memory allocated by the process is allocated from the virtual address space of the process itself. For example, the first heap memory allocated by the second process is the memory in the virtual address space of the second process itself. As shown in FIG. 6 , it can be seen from FIG. 6 that the virtual address space of the second process includes heap memory, stack memory, shared memory mapping area (first mapping area), and the like. It can be seen from FIG. 6 that the first mapping area is located between the heap memory (the first heap memory) of the second process and the stack memory of the second process. That is to say, the first heap memory and the first mapping area are two different pieces of memory in the virtual address space of the second process.

其中,第一映射区域存储的是第二进程的共享内存信息,第二进程的共享内存信息可以包括第一映射关系或者第二进程写入第一映射区域的第一数据等信息,此处不作具体限定。第二映射区域存储的是第三进程的共享内存信息,第三进程的共享内存信息可以包括第二映射关系或者从第一共享内存中读取的第一数据等信息,此处不作具体限定。The first mapping area stores the shared memory information of the second process, and the shared memory information of the second process may include information such as the first mapping relationship or the first data written into the first mapping area by the second process, which will not be described here. Specific restrictions. The second mapping area stores the shared memory information of the third process, and the shared memory information of the third process may include information such as the second mapping relationship or the first data read from the first shared memory, which is not specifically limited here.

上述实施例中,通过将第二进程的控制信息存储在第一堆内存,将第二进程的共享内存信息存储在第一映射区域,第一堆内存与第一映射区域为不同的内存,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同理,将第三进程的控制信息存储在第二堆内存,将第三进程的共享内存信息存储在第二映射区域,第二堆内存与第二映射区域为不同的内存,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above embodiment, by storing the control information of the second process in the first heap memory, and storing the shared memory information of the second process in the first mapping area, the first heap memory and the first mapping area are different memories, so Implementing separate storage of the control information of the second process and the shared memory information of the second process is beneficial to ensure the security of the shared memory information of the second process. Similarly, the control information of the third process is stored in the second heap memory, and the shared memory information of the third process is stored in the second mapping area, and the second heap memory and the second mapping area are different memories, so that the third The separate storage of the control information of the process and the shared memory information of the third process is beneficial to ensure the safety of the shared memory information of the third process.

在另一种具体的实施方式中,在第二进程将第一数据写入第一映射区域之前,第一进程需要分配第三堆内存,第三堆内存用于存储第二进程的控制信息,然后第二进程向第一进程发送第一获取信息请求,请求从第三堆内存中获取第二进程的控制信息,第二进程在获取到第一进程返回的第二进程的控制信息之后,第二进程可以根据第二进程的控制信息将第一数据写入第一映射区域。在第三进程将第一数据从第一共享内存映射至第二映射区域之前,第三进程需要分配第二堆内存,第二堆内存用于存储第三进程的控制信息,然后第三进程从第二堆内存中获取第三进程的控制信息,以使得第三进程根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。In another specific implementation, before the second process writes the first data into the first mapping area, the first process needs to allocate a third heap memory, and the third heap memory is used to store the control information of the second process, Then the second process sends a first information acquisition request to the first process, requesting to acquire the control information of the second process from the third heap memory. After the second process acquires the control information of the second process returned by the first process, the first process The second process may write the first data into the first mapping area according to the control information of the second process. Before the third process maps the first data from the first shared memory to the second mapping area, the third process needs to allocate the second heap memory, and the second heap memory is used to store the control information of the third process, and then the third process needs to The control information of the third process is obtained from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.

上述实施例中,通过将第二进程的控制信息存储在第一进程的堆内存,将第三进程的控制信息存储在第三进程的堆内存,在第二进程与第三进程进行通信时,第二进程需要先向第一进程发送获取控制信息的请求,然后获得第一进程返回的第二进程的控制信息,第三进程可以从自己的堆内存中获取自己的控制信息,可以看出,第二进程的通信效率低于第三进程的通信效率。也就是说,将进程的控制信息存储在进程自己的堆内存的方法,适用于性能要求高的进程;将进程的控制信息存储在第一进程的堆内存的方法,适用于性能要求低一些的进程。可以看出,上述方法不仅适用于通信性能要求高的进程,还适用于通信性能要求低的进程,灵活性高。同时,通过将第二进程的控制信息存储在第一进程的堆内存,将第三进程的控制信息存储在第三进程的堆内存,还可以实现第二进程的控制信息和第三进程的控制信息的分开存放,有利于第二进程和第三进程在运行时相互独立、互不干扰,在第二进程和第三进程中的其中一个进程出现故障时,故障不会扩散至另外一个进程。In the above embodiment, by storing the control information of the second process in the heap memory of the first process, and storing the control information of the third process in the heap memory of the third process, when the second process communicates with the third process, The second process needs to first send a request for obtaining control information to the first process, and then obtain the control information of the second process returned by the first process. The third process can obtain its own control information from its own heap memory. It can be seen that, The communication efficiency of the second process is lower than that of the third process. That is to say, the method of storing process control information in the process's own heap memory is suitable for processes with high performance requirements; the method of storing process control information in the heap memory of the first process is suitable for lower performance requirements. process. It can be seen that the above method is not only suitable for processes with high communication performance requirements, but also suitable for processes with low communication performance requirements, and has high flexibility. At the same time, by storing the control information of the second process in the heap memory of the first process, and storing the control information of the third process in the heap memory of the third process, the control information of the second process and the control of the third process can also be realized. The separate storage of information is beneficial for the second process and the third process to be independent of each other and not interfere with each other during operation. When one of the second process and the third process fails, the failure will not spread to the other process.

由上述实施例还可以获知,第三堆内存为第一进程虚拟地址空间中的内存,第一映射区域为第二进程虚拟地址空间中的内存,第一进程与第二进程之间存在进程隔离,所以,可以理解,第三堆内存与第一映射区域也为两块不同的内存。另外,由上述实施例可知,第二堆内存与第二映射区域为第三进程虚拟地址空间中不同的两块内存。It can also be known from the above embodiment that the third heap memory is the memory in the virtual address space of the first process, the first mapping area is the memory in the virtual address space of the second process, and there is process isolation between the first process and the second process. , so it can be understood that the third heap memory and the first mapping area are also two different pieces of memory. In addition, it can be known from the above embodiment that the second heap memory and the second mapping area are two different pieces of memory in the virtual address space of the third process.

上述方案中,将第二进程的控制信息存储在第三堆内存,将第二进程的共享信息存储在第一映射区域,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同时,将第三进程的控制信息存储在第二堆内存,将第三进程的共享内存信息存储在第二映射区域,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above solution, the control information of the second process is stored in the third heap memory, and the shared information of the second process is stored in the first mapping area, so that the control information of the second process and the shared memory information of the second process can be separated. The storage is beneficial to ensure the safety of the shared memory information of the second process. At the same time, the control information of the third process is stored in the second heap memory, and the shared memory information of the third process is stored in the second mapping area, so that the control information of the third process and the shared memory information of the third process can be stored separately. , which is beneficial to ensure the security of the shared memory information of the third process.

在又一种具体的实施方式中,在第二进程将第一数据写入第一映射区域之前,第二进程需要分配第一堆内存,第一堆内存用于存储第二进程的控制信息,然后第二进程从第一堆内存中获取第二进程的控制信息,以使得第二进程根据第二进程的控制信息将第一数据写入第一映射区域。在第三进程将第一数据从第一共享内存映射至第二映射区域之前,第一进程需要分配第三堆内存,第三堆内存用于存储第三进程的控制信息,然后第三进程向第一进程发送第二获取信息请求,请求从第三堆内存中获取第三进程的控制信息,第三进程在获取到第一进程返回的第三进程的控制信息之后,第三进程可以根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。In another specific implementation manner, before the second process writes the first data into the first mapping area, the second process needs to allocate a first heap memory, and the first heap memory is used to store the control information of the second process, Then the second process acquires the control information of the second process from the first heap memory, so that the second process writes the first data into the first mapping area according to the control information of the second process. Before the third process maps the first data from the first shared memory to the second mapping area, the first process needs to allocate a third heap memory, and the third heap memory is used to store the control information of the third process, and then the third process sends the The first process sends a second request for obtaining information, requesting to obtain the control information of the third process from the third heap memory. After the third process obtains the control information of the third process returned by the first process, the third process can The control information of the three processes maps the first data from the first shared memory to the second mapping area.

由上述实施例还可以获知,第三堆内存为第一进程虚拟地址空间中的内存,第二映射区域为第三进程虚拟地址空间中的内存,第一进程与第三进程之间存在进程隔离,所以,可以理解,第三堆内存与第二映射区域也为两块不同的内存。另外,还可以获知,第一堆内存与第一映射区域为第二进程虚拟地址空间中不同的两块内存。It can also be known from the above embodiment that the third heap memory is the memory in the virtual address space of the first process, the second mapping area is the memory in the virtual address space of the third process, and there is process isolation between the first process and the third process. , so it can be understood that the third heap memory and the second mapping area are also two different pieces of memory. In addition, it can also be known that the first heap memory and the first mapping area are two different pieces of memory in the virtual address space of the second process.

上述实施例中,将第二进程的控制信息存储在第一堆内存,将第二进程的共享信息存储在第一映射区域,可以实现第二进程的控制信息和第二进程的共享内存信息的分开存放,有利于保证第二进程的共享内存信息的安全。同时,将第三进程的控制信息存储在第三堆内存,将第三进程的共享内存信息存储在第二映射区域,可以实现第三进程的控制信息和第三进程的共享内存信息的分开存放,有利于保证第三进程的共享内存信息的安全。In the above embodiment, the control information of the second process is stored in the first heap memory, and the shared information of the second process is stored in the first mapping area, which can realize the control information of the second process and the shared memory information of the second process. Separate storage is beneficial to ensure the safety of the shared memory information of the second process. At the same time, the control information of the third process is stored in the third heap memory, and the shared memory information of the third process is stored in the second mapping area, so that the control information of the third process and the shared memory information of the third process can be stored separately. , which is beneficial to ensure the security of the shared memory information of the third process.

由上述实施例还可以看出,第一进程的堆内存、第二进程的堆内存、第三进程的堆内存、第一映射区域、第二映射区域、第一共享内存为不同的内存。也就是说,本申请实施例可以实现第二进程的控制信息、第三进程的控制信息、第二进程的共享内存信息、第三进程的共享内存信息与第一共享内存信息的分开存放,有利于保证第一共享内存信息的安全。It can also be seen from the above embodiment that the heap memory of the first process, the heap memory of the second process, the heap memory of the third process, the first mapping area, the second mapping area, and the first shared memory are different memories. That is to say, the embodiment of the present application can realize the separate storage of the control information of the second process, the control information of the third process, the shared memory information of the second process, and the shared memory information of the third process and the first shared memory information. It is beneficial to ensure the security of the first shared memory information.

其中,第一共享内存可以存储第一共享内存信息,第一共享内存信息可以包括第一共享内存与第一映射区域的映射关系、第一共享内存与第二映射区域的映射关系或者第二进程写入第一共享内存中的第一数据等信息,此处不作具体限定。The first shared memory may store first shared memory information, and the first shared memory information may include the mapping relationship between the first shared memory and the first mapping area, the mapping relationship between the first shared memory and the second mapping area, or the second process The information such as the first data written in the first shared memory is not specifically limited here.

为了进一步保障第二进程和第三进程通信的可靠性,作为本申请的一个实施例,第一进程中包括检测线程,用于在第二进程通过第一共享内存与第三进程进行通信时,实时检测第二进程和第三进程的状态,在第一进程检测到第二进程或第三进程出现复位的情况下,第一进程可以及时通知第二进程和第三进程中未出现复位的进程以使通信连接能够及时被终止,所占用的第一共享内存资源能够及时被释放。其中,进程复位表示进程的状态回到初始状态,进程等待再次执行。In order to further ensure the reliability of the communication between the second process and the third process, as an embodiment of the present application, the first process includes a detection thread, which is used for when the second process communicates with the third process through the first shared memory, Detect the status of the second process and the third process in real time. When the first process detects that the second process or the third process is reset, the first process can promptly notify the second process and the third process that no reset occurs. So that the communication connection can be terminated in time, and the occupied first shared memory resource can be released in time. Among them, the process reset means that the state of the process returns to the initial state, and the process waits to be executed again.

在本实施例中,针对第二进程与第三进程之间的单向通信,一旦第一进程检测到第二进程与第三进程的此次通信完毕,第一进程即可关闭第一共享内存,及时释放第一共享内存资源。In this embodiment, for the one-way communication between the second process and the third process, once the first process detects that the communication between the second process and the third process is completed, the first process can close the first shared memory , release the first shared memory resource in time.

为了进一步保障第二进程和第三进程通信的可靠性,作为本申请的一个实施例,第一进程还包括恢复共享内存线程,其中,恢复共享内存线程为第一进程包含的线程中运行级别最高的线程,在第二进程通过第一共享内存与第三进程进行通信时,若第一进程出现复位需要重启,则第一进程中最先运行的线程为恢复共享内存线程,第一进程中包含的其他线程,比如检测线程,在恢复共享内存线程运行时,检测线程的运行可以被延迟或者被暂停,把资源让给恢复共享内存线程,恢复共享内存线程可以以最快的速度恢复第一共享内存。In order to further ensure the reliability of the communication between the second process and the third process, as an embodiment of the present application, the first process further includes a recovery shared memory thread, wherein the recovery shared memory thread is the highest running level among the threads included in the first process When the second process communicates with the third process through the first shared memory, if the first process is reset and needs to be restarted, the thread that runs first in the first process is the recovery shared memory thread, and the first process includes other threads, such as the detection thread, when resuming the shared memory thread running, the detection thread can be delayed or suspended, giving resources to the recovery shared memory thread, and the recovery shared memory thread can resume the first shared memory at the fastest speed. Memory.

由上述实施例可以看出,本申请实施例提供的进程间通信方法,第一进程还可以实时检测第二进程或者第三进程是否出现复位,在第二进程或者第三进程出现复位的情况下,及时终止第二进程和第三进程之间的通信连接,释放第一共享内存所占用的资源。同时,第一进程还可以设置恢复共享内存线程的运行级别为最高优先级,用于在第一进程需要重启时,快速恢复第一共享内存。It can be seen from the above embodiments that, in the inter-process communication method provided by the embodiments of the present application, the first process can also detect in real time whether the second process or the third process is reset, and in the case where the second process or the third process is reset. , terminate the communication connection between the second process and the third process in time, and release the resources occupied by the first shared memory. At the same time, the first process may also set the running level of the restoration shared memory thread to the highest priority, so as to quickly restore the first shared memory when the first process needs to be restarted.

由上述实施例还可以看出,本申请实施例提供的进程间通信方法采用的服务模式也是C/S模式。其中,第一进程作为服务端进程,为第二进程和第三进程提供第一共享内存,第二进程和第三进程作为客户端进程,使用第一进程提供的第一共享内存进行通信。It can also be seen from the above embodiments that the service mode adopted by the inter-process communication method provided by the embodiment of the present application is also the C/S mode. The first process, as a server process, provides a first shared memory for the second process and the third process, and the second process and the third process, as client processes, communicate using the first shared memory provided by the first process.

本申请实施例提供的进程间通信方法,通过第一进程创建第二进程以及第三进程之间的第一共享内存,第二进程将第一数据写入第一映射区域,并根据第一映射关系将第一数据从第一映射区域映射至第一共享内存,第三进程根据第二映射关系,将第一数据从第一共享内存映射至第二映射区域,从而实现第二进程向第三进程的单向通信。其中,第二进程向第三进程传输第一数据的过程,是通过第一映射关系以及第二映射关系进行映射实现的,无需通过系统调用内核陷入内核空间才能实现,无需进行数据拷贝,系统开销小,通信效率高。而且,本申请提供的进程间通信方法在应用于操作系统多样的嵌入式设备时,由于没有在第二进程向第三进程的单向通信过程中引入内核态,所以无需考虑单板和操作系统适配的问题,操作简单,移植性强。同时,除了第一进程之外,只有第二进程具有第一共享内存的读写权限,可以避免多个进程同时对第一共享内存进行写操作,造成第一共享内存中的数据混乱。另外,第二进程和第三进程均是在第一进程进行注册的进程,第一进程可以对第二进程和第三进程的身份进行校验,并且只有具有第一共享内存的读写权限或者只读权限的进程才可以访问第一共享内存,可以避免恶意程序篡改或者破坏第一共享内存中的数据,能够有效提升进程间通信的安全可靠性。In the inter-process communication method provided by the embodiment of the present application, the first shared memory between the second process and the third process is created by the first process, the second process writes the first data into the first mapping area, and according to the first mapping The relationship maps the first data from the first mapping area to the first shared memory, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relationship, so as to realize the second process to the third One-way communication between processes. Wherein, the process of transmitting the first data from the second process to the third process is realized by mapping the first mapping relationship and the second mapping relationship, and it can be realized without the system calling the kernel into the kernel space, without data copying and system overhead. Small, high communication efficiency. Moreover, when the inter-process communication method provided by the present application is applied to embedded devices with various operating systems, since the kernel state is not introduced in the one-way communication process from the second process to the third process, there is no need to consider the single board and the operating system. The problem of adaptation, the operation is simple, and the portability is strong. Meanwhile, in addition to the first process, only the second process has the read and write authority of the first shared memory, which can prevent multiple processes from simultaneously writing to the first shared memory, resulting in confusion of data in the first shared memory. In addition, the second process and the third process are both processes registered in the first process, the first process can verify the identities of the second process and the third process, and only has the read and write permissions of the first shared memory or Only processes with read-only permissions can access the first shared memory, which can prevent malicious programs from tampering with or destroy data in the first shared memory, and can effectively improve the security and reliability of inter-process communication.

上述实施例详细阐述了第二进程向第三进程的单向通信的实现过程,接下来,对本申请提供的进程间通信方法实现第三进程向第二进程的单向通信的过程进行阐述。The above embodiments describe in detail the implementation process of the one-way communication between the second process and the third process. Next, the process of implementing the one-way communication between the third process and the second process by the inter-process communication method provided by the present application is explained.

请参见图7,如图7所示,该方法包括:Please refer to Fig. 7. As shown in Fig. 7, the method includes:

S201、第一进程创建第二进程以及第三进程之间的第二共享内存。S201. The first process creates a second shared memory between the second process and the third process.

其中,第一进程对第二共享内存具有读写权限,第三进程对第二共享内存具有读写权限,第二进程对第二共享内存具有只读权限,第三映射区域与第二共享内存之间具有第三映射关系,第四映射区域与第三共享内存之间具有第四映射关系,第三映射区域为第三进程虚拟地址空间中与第二共享内存建立映射关系的区域,第四映射区域为第二进程虚拟地址空间中与第二共享内存建立映射关系的区域;The first process has read and write rights to the second shared memory, the third process has read and write rights to the second shared memory, the second process has read-only rights to the second shared memory, and the third mapping area and the second shared memory There is a third mapping relationship between them, there is a fourth mapping relationship between the fourth mapping area and the third shared memory, and the third mapping area is an area in the virtual address space of the third process that establishes a mapping relationship with the second shared memory, and the fourth The mapping area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

其中,第一进程创建第二进程与第三进程之间的第二共享内存的过程,与S01中描述的第一进程创建第二进程与第三进程之间的第一共享内存的过程相类似,第一进程为第二进程配置第二共享内存的只读权限的过程,以及为第三进程配置第二共享内存的读写权限的过程,与S101中第一进程为第二进程配置第一共享内存的读写权限,以及为第三进程配置第一共享内存的只读权限的过程相类似,具体可以参考S101中的相关描述,此处不再展开赘述。第三进程建立第三映射区域与第二共享内存之间的映射关系,以及第二进程建立第四映射区域与第二共享内存之间的映射关系,与S101中第二进程建立第一映射区域与第一共享内存之间的映射关系的过程相类似,此处不再展开赘述。The process of creating the second shared memory between the second process and the third process by the first process is similar to the process of creating the first shared memory between the second process and the third process by the first process described in S01 , the process of configuring the read-only permission of the second shared memory by the first process for the second process, and the process of configuring the read-write permission of the second shared memory for the third process is the same as the process of configuring the first process for the second process in S101. The read-write permission of the shared memory and the process of configuring the read-only permission of the first shared memory for the third process are similar. For details, please refer to the relevant description in S101, which will not be repeated here. The third process establishes the mapping relationship between the third mapping area and the second shared memory, and the second process establishes the mapping relationship between the fourth mapping area and the second shared memory, and establishes the first mapping area with the second process in S101 The process of the mapping relationship between the first shared memories is similar, and will not be repeated here.

S202、第三进程将第二数据写入第三映射区域,并根据第三映射关系,将第二数据从第三映射区域映射至第二共享内存,第二进程根据第四映射关系,将第二数据从第二共享内存映射至第四映射区域,从而实现第三进程向第二进程的单向通信。S202. The third process writes the second data into the third mapping area, and maps the second data from the third mapping area to the second shared memory according to the third mapping relationship, and the second process maps the second data to the second shared memory according to the fourth mapping relationship. The second data is mapped from the second shared memory to the fourth mapping area, thereby realizing one-way communication from the third process to the second process.

其中,第三进程将第二数据写入第三映射区域,并根据第三映射关系,将第二数据从第三映射区域映射至第二共享内存,可以理解为第三进程对第二共享内存进行写操作,将第二数据写入了第二共享内存。第二进程根据第四映射区域与第二共享内存之间的映射关系,将第二数据从第二共享内存映射至第四映射区域,可以理解为第二进程对第二共享内存进行读操作,从第二共享内存读取了第二数据。The third process writes the second data into the third mapping area, and maps the second data from the third mapping area to the second shared memory according to the third mapping relationship. It can be understood that the third process maps the second shared memory to the second shared memory A write operation is performed, and the second data is written into the second shared memory. According to the mapping relationship between the fourth mapping area and the second shared memory, the second process maps the second data from the second shared memory to the fourth mapping area. It can be understood that the second process reads the second shared memory. The second data is read from the second shared memory.

本申请实施例提供的进程间通信方法,通过第一进程创建第二进程和第三进程之间的第二共享内存,第三进程将第二数据写入第三映射区域,并根据第三映射关系将第二数据从第三映射区域映射至第二共享内存,第三进程根据第四映射关系,将第二数据从第二共享内存映射至第四映射区域。从而实现第三进程向第二进程的单向通信。其中,第三进程向第二进程传输第二数据的过程,是通过第三映射关系以及第四映射关系进行映射实现的,无需通过系统调用内核陷入内核空间才能实现,无需进行数据拷贝,系统开销小,通信效率高。而且,在应用于操作系统多样的嵌入式设备时,由于没有在第三进程向第二进程的单向通信过程中引入内核态,所以无需考虑单板和操作系统适配的问题,操作简单,移植性强。同时,除了第一进程之外,只有第三进程具有第二共享内存的读写权限,可以避免多个进程同时对第二共享内存进行写操作,造成第二共享内存中的数据混乱。另外,第二进程和第三进程均是在第一进程进行注册的进程,第一进程可以对第二进程和第三进程的身份进行校验,并且只有具有第二共享内存的读写权限或者只读权限的进程才可以访问第二共享内存,可以避免恶意程序篡改或者破坏第二共享内存中的数据,能够有效地提升进程间通信的安全可靠性。In the inter-process communication method provided by the embodiment of the present application, a second shared memory between the second process and the third process is created by the first process, the third process writes the second data into the third mapping area, and according to the third mapping The relationship maps the second data from the third mapping area to the second shared memory, and the third process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship. Thereby, one-way communication from the third process to the second process is realized. Wherein, the process of transmitting the second data from the third process to the second process is realized by mapping the third mapping relationship and the fourth mapping relationship, which can be realized without the system calling the kernel into the kernel space, without data copying and system overhead. Small, high communication efficiency. Moreover, when applied to embedded devices with various operating systems, since the kernel state is not introduced in the one-way communication process from the third process to the second process, there is no need to consider the problem of adapting the board and the operating system, and the operation is simple. Portable. Meanwhile, in addition to the first process, only the third process has read and write permissions to the second shared memory, which can prevent multiple processes from simultaneously writing to the second shared memory, resulting in confusion of data in the second shared memory. In addition, both the second process and the third process are processes registered in the first process, the first process can verify the identities of the second process and the third process, and only has the read and write permissions of the second shared memory or Only processes with read-only permissions can access the second shared memory, which can prevent malicious programs from tampering with or destroy data in the second shared memory, and can effectively improve the security and reliability of inter-process communication.

由上述实施例可知,通过S101和S102实现了第二进程向第三进程的单向通信,通过S201和S202实现了第三进程向第二进程的单向通信。可以理解,通过S101、S102、S201和S202可以实现第二进程和第三进程之间的双向通信,此处不再对实现第二进程和第三进程之间的双向通信的过程展开赘述。It can be seen from the above embodiments that the one-way communication between the second process and the third process is implemented through S101 and S102, and the one-way communication between the third process and the second process is implemented through S201 and S202. It can be understood that the two-way communication between the second process and the third process can be implemented through S101, S102, S201 and S202, and the process of implementing the two-way communication between the second process and the third process will not be repeated here.

为便于更好理解和实施本申请实施例的上述方案,下面通过一个更为具体的进程间通信的场景进行描述。In order to better understand and implement the above solutions of the embodiments of the present application, a more specific scenario of inter-process communication is described below.

请参见图8,图8为本申请实施例提供的一种进程间通信方法的交互示意图。如图8所示,该进程间通信方法可以包括:Please refer to FIG. 8 , which is a schematic diagram of interaction of an inter-process communication method provided by an embodiment of the present application. As shown in Figure 8, the inter-process communication method may include:

S301、第二进程向第一进程发送注册请求。S301. The second process sends a registration request to the first process.

其中,注册请求中可以包括第二进程的UID或者PID等信息,用于第一进程对第二进程的身份进行校验,并根据第二进程的UID或者PID等信息为第二进程进行注册。The registration request may include information such as the UID or PID of the second process, for the first process to verify the identity of the second process, and to register the second process according to the information such as the UID or PID of the second process.

S302、第一进程接收第二进程发送的注册请求,为第二进程进行注册得到注册信息,创建第一共享内存,并配置第二进程具有第一共享内存的读写权限。S302. The first process receives the registration request sent by the second process, registers the second process to obtain registration information, creates a first shared memory, and configures the second process to have read and write permissions of the first shared memory.

其中,第一进程可以根据第二进程的UID或者PID等信息创建第一共享内存,第一共享内存可以具有共享内存标识,第一共享内存标识可以根据第一共享内存的物理地址或者第二进程的UID或者PID等信息进行命名。The first process may create a first shared memory according to information such as the UID or PID of the second process, the first shared memory may have a shared memory identifier, and the first shared memory identifier may be based on the physical address of the first shared memory or the second process. UID or PID and other information to name.

S303、第一进程将注册信息发送至第二进程。S303. The first process sends the registration information to the second process.

S304、第二进程接收第一进程返回的注册信息,开辟第一映射区域,建立第一映射区域与第一共享内存之间的映射关系。S304. The second process receives the registration information returned by the first process, opens up a first mapping area, and establishes a mapping relationship between the first mapping area and the first shared memory.

其中,注册信息可以包括第一共享内存标识,用于第二进程建立第一映射区域与第一共享内存之间的映射关系。The registration information may include a first shared memory identifier, which is used by the second process to establish a mapping relationship between the first mapping area and the first shared memory.

S305、第二进程向第一进程发送连接请求,连接请求用于请求第一进程建立第二进程与第三进程的连接,其中,第三进程已在第一进程中注册。S305. The second process sends a connection request to the first process, where the connection request is used to request the first process to establish a connection between the second process and the third process, where the third process has been registered in the first process.

S306、第一进程接收连接请求,根据连接请求确定第三进程,并配置第三进程具有第一共享内存的只读权限。S306. The first process receives the connection request, determines the third process according to the connection request, and configures the third process to have the read-only permission of the first shared memory.

其中,第二进程发送的连接请求中可以包括第三进程的UID或者PID等信息,用于第一进程根据连接请求中包括的第三进程的标识信息查找第三进程。The connection request sent by the second process may include information such as the UID or PID of the third process, for the first process to search for the third process according to the identification information of the third process included in the connection request.

S307、第一进程将连接请求发送至第三进程。S307. The first process sends the connection request to the third process.

S308、第三进程接收连接请求,开辟第二映射区域,建立第二映射区域与第一共享内存之间的映射关系。S308. The third process receives the connection request, opens up a second mapping area, and establishes a mapping relationship between the second mapping area and the first shared memory.

其中,连接请求中还可以包括第一共享内存标识,用于第三进程建立第二映射区域与第一共享内存之间的映射关系。The connection request may further include a first shared memory identifier, which is used by the third process to establish a mapping relationship between the second mapping area and the first shared memory.

S309、第三进程向第一进程返回连接应答。S309. The third process returns a connection response to the first process.

S310、第一进程接收第三进程发送的连接应答,并将连接应答转发至第二进程。S310. The first process receives the connection response sent by the third process, and forwards the connection response to the second process.

S311、第二进程接收连接应答,从第一堆内存中获取第二进程的控制信息,并将第一数据通过第一映射区域写入第一共享内存中的disruptor队列。S311. The second process receives the connection response, obtains the control information of the second process from the first heap memory, and writes the first data into the disruptor queue in the first shared memory through the first mapping area.

S312、第三进程从第二堆内存中获取第三进程的控制信息,通过第二映射区域从第一共享内存中的disruptor队列读取第一数据。S312 , the third process acquires the control information of the third process from the second heap memory, and reads the first data from the disruptor queue in the first shared memory through the second mapping area.

上文详细阐述了本申请实施例的一种进程间通信方法,基于相同的发明构思,下面继续提供本申请实施例的一种进程间通信装置,本申请提供的进程间通信装置应用于需要进行进程间通信的各种终端设备,包括但不限于智能手机、平板电脑、膝上型便携计算机等电子设备。终端设备可以是基于android、linux或者windows等操作系统构建的电子设备,也可以根据需求基于其他系统构建,此处不作具体限定。An inter-process communication method according to an embodiment of the present application is described in detail above. Based on the same inventive concept, an inter-process communication device according to an embodiment of the present application is provided below. The inter-process communication device provided by the present application is used for Various terminal devices for inter-process communication, including but not limited to electronic devices such as smart phones, tablet computers, and laptop computers. The terminal device may be an electronic device constructed based on an operating system such as android, linux, or windows, or may be constructed based on other systems as required, which is not specifically limited here.

参见图9,图9是本申请实施例提供的一种进程间通信装置100的结构示意图,如图9所示,本申请实施例提供的进程间通信装置100至少包括:创建模块110和通信模块120,其中,Referring to FIG. 9 , FIG. 9 is a schematic structural diagram of an inter-process communication apparatus 100 provided by an embodiment of the present application. As shown in FIG. 9 , the inter-process communication apparatus 100 provided by an embodiment of the present application includes at least: a creation module 110 and a communication module 120, of which,

创建模块110,用于通过第一进程创建第二进程以及第三进程之间的第一共享内存,其中,第一进程、第二进程以及第三进程运行在用户空间,第一进程对第一共享内存具有读写权限,第二进程对第一共享内存具有读写权限,第三进程对第一共享内存具有只读权限,第二进程和第三进程均在第一进程中注册,第一映射区域与第一共享内存之间具有第一映射关系,第二映射区域与第一共享内存之间具有第二映射关系,第一映射区域为第二进程虚拟地址空间中与第一共享内存建立映射关系的区域,第二映射区域为第三进程虚拟地址空间中与第一共享内存建立映射关系的区域。The creation module 110 is configured to create a first shared memory between the second process and the third process through the first process, wherein the first process, the second process and the third process run in the user space, and the first process The shared memory has read and write permissions, the second process has read and write permissions to the first shared memory, the third process has read-only permissions to the first shared memory, the second process and the third process are both registered in the first process, and the first There is a first mapping relationship between the mapping area and the first shared memory, a second mapping relationship between the second mapping area and the first shared memory, and the first mapping area is established in the virtual address space of the second process with the first shared memory A mapping relationship area, and the second mapping area is an area in the virtual address space of the third process that establishes a mapping relationship with the first shared memory.

通信模块120,用于通过第二进程将第一数据写入第一映射区域,并根据第一映射关系将第一数据从第一映射区域映射至第一共享内存,还通过第三进程根据第二映射关系,将第一数据从第一共享内存映射至第二映射区域,从而实现第二进程向第三进程的单向通信。The communication module 120 is configured to write the first data into the first mapping area through the second process, map the first data from the first mapping area to the first shared memory according to the first mapping relationship, and also use the third process to map the first data from the first mapping area to the first shared memory according to the first mapping relationship. The second mapping relationship is used to map the first data from the first shared memory to the second mapping area, so as to realize one-way communication from the second process to the third process.

在本申请实施例中,创建模块110,还用于:In this embodiment of the present application, the creation module 110 is further configured to:

创建模块110通过第一进程配置第二进程具有第一共享内存的读写权限,以及,配置第三进程具有第一共享内存的只读权限。The creation module 110 configures, through the first process, the second process to have the read-write permission of the first shared memory, and configures the third process to have the read-only permission of the first shared memory.

在本申请实施例中,创建模块110具体用于:In this embodiment of the present application, the creation module 110 is specifically used for:

创建模块110通过第一进程通过访问控制列表ACL配置第二进程具有第一共享内存的读写权限,以及配置第三进程具有第一共享内存的只读权限。The creation module 110 configures the second process to have the read-write permission of the first shared memory through the access control list ACL, and configures the third process to have the read-only permission of the first shared memory through the first process.

在本申请实施例中,在第二进程将第一数据写入第一映射区域之前,装置还包括:分配模块130和获取模块140;In this embodiment of the present application, before the second process writes the first data into the first mapping area, the apparatus further includes: an allocation module 130 and an acquisition module 140;

分配模块130,用于通过第二进程分配第一堆内存,第一堆内存用于存储第二进程的控制信息。The allocation module 130 is configured to allocate the first heap memory by the second process, and the first heap memory is used to store the control information of the second process.

获取模块140,用于通过第二进程从第一堆内存中获取第二进程的控制信息,以使得第二进程根据第二进程的控制信息将第一数据写入第一映射区域。The obtaining module 140 is configured to obtain the control information of the second process from the first heap memory by the second process, so that the second process writes the first data into the first mapping area according to the control information of the second process.

在第三进程将第一数据从第一共享内存映射至第二映射区域之前,分配模块130,还用于通过第三进程分配第二堆内存,第二堆内存用于存储第三进程的控制信息。Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module 130 is further configured to allocate a second heap memory by the third process, and the second heap memory is used to store the control of the third process information.

获取模块140,还用于通过第三进程从第二堆内存中获取第三进程的控制信息,以使得第三进程根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。The obtaining module 140 is further configured to obtain the control information of the third process from the second heap memory through the third process, so that the third process maps the first data from the first shared memory to the second process according to the control information of the third process map area.

在本申请实施例中,第一堆内存与第一映射区域为不同的内存,第一映射区域用于存储第二进程的共享内存信息,第二堆内存与第二映射区域为不同的内存,第二映射区域用于存储第三进程的共享内存信息。In the embodiment of the present application, the first heap memory and the first mapping area are different memories, the first mapping area is used to store the shared memory information of the second process, and the second heap memory and the second mapping area are different memories, The second mapping area is used to store the shared memory information of the third process.

在本申请实施例中,在第二进程将第一数据写入第一映射区域之前,装置还包括发送模块160;In this embodiment of the present application, before the second process writes the first data into the first mapping area, the apparatus further includes a sending module 160;

分配模块130,用于通过第一进程分配第三堆内存,第三堆内存用于存储第二进程的控制信息。The allocation module 130 is configured to allocate a third heap memory by the first process, and the third heap memory is used to store control information of the second process.

获取模块140,用于通过第一进程接收第二进程发送的第一获取信息请求,其中,第一获取信息请求用于请求从第三堆内存中获取第二进程的控制信息。The obtaining module 140 is configured to receive, through the first process, a first obtaining information request sent by the second process, wherein the first obtaining information request is used to request to obtain control information of the second process from the third heap memory.

发送模块160,用于通过第一进程根据第一获取信息请求将第二进程的控制信息发送至第二进程,以使得第二进程根据第二进程的控制信息将第一数据写入第一映射区域。The sending module 160 is configured to send the control information of the second process to the second process through the first process according to the first acquisition information request, so that the second process writes the first data into the first map according to the control information of the second process area.

在第三进程将第一数据从第一共享内存映射至第二映射区域之前,分配模块130,还用于通过第三进程分配第二堆内存,第二堆内存用于存储第三进程的控制信息。Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module 130 is further configured to allocate a second heap memory by the third process, and the second heap memory is used to store the control of the third process information.

获取模块140,还用于通过第三进程从第二堆内存中获取第三进程的控制信息,以使得第三进程根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。The obtaining module 140 is further configured to obtain the control information of the third process from the second heap memory through the third process, so that the third process maps the first data from the first shared memory to the second process according to the control information of the third process map area.

在本申请实施例中,第三堆内存与第一映射区域为不同的内存,第一映射区域用于存储第二进程的共享内存信息,第二堆内存与第二映射区域为不同的内存,第二映射区域用于存储第三进程的共享内存信息。In this embodiment of the present application, the third heap memory and the first mapping area are different memories, the first mapping area is used to store the shared memory information of the second process, and the second heap memory and the second mapping area are different memories, The second mapping area is used to store the shared memory information of the third process.

在本申请实施例中,在第二进程将第一数据写入第一映射区域之前,分配模块130,用于通过第二进程分配第一堆内存,第一堆内存用于存储第二进程的控制信息。In this embodiment of the present application, before the second process writes the first data into the first mapping area, the allocation module 130 is configured to allocate the first heap memory by the second process, and the first heap memory is used to store the data of the second process. control information.

获取模块140,用于通过第二进程从第一堆内存中获取第二进程的控制信息,以使得第二进程根据第二进程的控制信息将第一数据写入第一映射区域。The obtaining module 140 is configured to obtain the control information of the second process from the first heap memory by the second process, so that the second process writes the first data into the first mapping area according to the control information of the second process.

在第三进程将第一数据从第一共享内存映射至第二映射区域之前,装置还包括接收模块150;Before the third process maps the first data from the first shared memory to the second mapping area, the apparatus further includes a receiving module 150;

分配模块130,用于通过第一进程分配第三堆内存,第三堆内存用于存储第三进程的控制信息。The allocation module 130 is configured to allocate a third heap memory by the first process, and the third heap memory is used to store control information of the third process.

接收模块150,用于通过第一进程接收第三进程发送的第二获取信息请求,其中,第二获取信息请求用于请求从第三堆内存中获取第三进程的控制信息。The receiving module 150 is configured to receive, through the first process, a second information acquisition request sent by the third process, where the second information acquisition request is used to request to acquire control information of the third process from the third heap memory.

发送模块160,用于通过第一进程根据第二获取信息请求将第三进程的控制信息发送至第三进程,以使得第三进程根据第三进程的控制信息将第一数据从第一共享内存映射至第二映射区域。The sending module 160 is configured to send the control information of the third process to the third process according to the second information acquisition request through the first process, so that the third process can send the first data from the first shared memory to the third process according to the control information of the third process. Mapped to the second mapping area.

在本申请实施例中,第一堆内存与第一映射区域为不同的内存,第一映射区域用于存储第二进程的共享内存信息,第三堆内存与第二映射区域为不同的内存,第二映射区域用于存储第三进程的共享内存信息。In the embodiment of the present application, the first heap memory and the first mapping area are different memories, the first mapping area is used to store the shared memory information of the second process, and the third heap memory and the second mapping area are different memories, The second mapping area is used to store the shared memory information of the third process.

在本申请实施例中,第一进程包括检测线程,装置还包括检测模块170,检测模块170具体用于:In this embodiment of the present application, the first process includes a detection thread, and the apparatus further includes a detection module 170, and the detection module 170 is specifically used for:

检测模块170通过检测线程检测第二进程或者第三进程是否出现复位,检测模块170在检测到第二进程或者第三进程出现复位的情况下,通过检测线程通知第二进程和第三进程中没有出现复位的进程终止通信,并释放第一共享内存。The detection module 170 detects whether the second process or the third process is reset by the detection thread. When the detection module 170 detects that the second process or the third process is reset, it notifies the second process and the third process that there is no reset by the detection thread. The process that occurs the reset terminates the communication and frees the first shared memory.

在本申请实施例中,第一进程包括恢复共享内存线程,恢复共享内存线程为第一进程包含的线程中运行级别最高的线程,装置还包括恢复模块180,恢复模块180具体用于:In the embodiment of the present application, the first process includes a recovery shared memory thread, and the recovery shared memory thread is the thread with the highest running level among the threads included in the first process. The device further includes a recovery module 180, and the recovery module 180 is specifically used for:

当第一进程重启时,恢复模块180通过恢复共享内存线程恢复第一共享内存。When the first process is restarted, the restoration module 180 restores the first shared memory by restoring the shared memory thread.

在本申请实施例中,第一共享内存中包括破坏者队列,通信模块120具体用于:In this embodiment of the present application, the first shared memory includes a destroyer queue, and the communication module 120 is specifically configured to:

通信模块120通过第二进程将第一数据从第一映射区域映射至破坏者队列中,然后通过第三进程将第一数据从破坏者队列映射至第二映射区域。The communication module 120 maps the first data from the first mapping area to the destroyer queue through the second process, and then maps the first data from the destroyer queue to the second mapping area through the third process.

在本申请实施例中,创建模块110,还用于通过第一进程创建第二进程以及第三进程之间的第二共享内存,其中,第一进程对第二共享内存具有读写权限,第三进程对第二共享内存具有读写权限,第二进程对第二共享内存具有只读权限,第三映射区域与第二共享内存之间具有第三映射关系,第四映射区域与第二共享内存之间具有第四映射关系,第三映射区域为第三进程虚拟地址空间中与第二共享内存建立映射关系的区域,第四映射区域为第二进程虚拟地址空间中与第二共享内存建立映射关系的区域;In this embodiment of the present application, the creation module 110 is further configured to create a second shared memory between the second process and the third process through the first process, wherein the first process has read and write permissions to the second shared memory, and the first process The three processes have read and write permissions to the second shared memory, the second process has read-only permissions to the second shared memory, a third mapping relationship exists between the third mapping area and the second shared memory, and the fourth mapping area and the second shared memory There is a fourth mapping relationship between the memories, the third mapping area is an area in the virtual address space of the third process that establishes a mapping relationship with the second shared memory, and the fourth mapping area is in the virtual address space of the second process. The area established with the second shared memory The area of the mapping relationship;

通信模块120,还用于通过第三进程将第二数据写入第三映射区域,并根据第三映射关系,将第二数据从第三映射区域映射至第二共享内存,第二进程根据第四映射关系,将第二数据从第二共享内存映射至第四映射区域,从而实现第三进程向第二进程的单向通信。The communication module 120 is further configured to write the second data into the third mapping area through the third process, and map the second data from the third mapping area to the second shared memory according to the third mapping relationship, and the second process according to the third mapping relationship. Four mapping relationships are used to map the second data from the second shared memory to the fourth mapping area, so as to realize one-way communication from the third process to the second process.

具体地,上述进程间通信装置100执行各种操作的具体实现可参照上述方法实施例中相关内容中的描述,为了说明书的简洁,这里不再赘述。Specifically, for the specific implementation of the various operations performed by the above-mentioned inter-process communication apparatus 100, reference may be made to the descriptions in the relevant contents in the above-mentioned method embodiments, which are not repeated here for the sake of brevity of the description.

应当理解,进程间通信装置100仅为本申请实施例提供的一个例子,并且,进程间通信装置100可具有比图9示出的部件更多或更少的部件,可以组合两个或更多个部件,或者可具有部件的不同配置实现。It should be understood that the inter-process communication apparatus 100 is only an example provided by the embodiment of the present application, and the inter-process communication apparatus 100 may have more or less components than those shown in FIG. 9 , and two or more components may be combined. components, or may be implemented with different configurations of components.

本申请实施例还提供另一种进程间通信装置,参见图10,图10为本申请提供的一种进程间通信装置200的结构示意图,该装置包括:处理器210、通信接口230以及存储器220,其中,处理器210、通信接口230和存储器220通过总线240进行耦合。其中,An embodiment of the present application further provides another inter-process communication device. Referring to FIG. 10 , FIG. 10 is a schematic structural diagram of an inter-process communication device 200 provided by the present application. The device includes: a processor 210 , a communication interface 230 and a memory 220 , wherein the processor 210 , the communication interface 230 and the memory 220 are coupled through the bus 240 . in,

处理器210可以包括一个或者多个通用处理器,其中,通用处理器可以是能够处理电子指令的任何类型的设备,包括CPU、微处理器、微控制器、主处理器、控制器以及专用集成电路(application specific integrated circuit,ASIC)等等。处理器210读取存储器220中存储的程序代码,与通信接口230配合执行本申请上述实施例中由进程间通信装置100执行的方法的部分或者全部步骤。Processor 210 may include one or more general-purpose processors, where a general-purpose processor may be any type of device capable of processing electronic instructions, including CPUs, microprocessors, microcontrollers, main processors, controllers, and application-specific integrated circuit (application specific integrated circuit, ASIC) and so on. The processor 210 reads the program code stored in the memory 220, and cooperates with the communication interface 230 to execute part or all of the steps of the method executed by the inter-process communication apparatus 100 in the above embodiments of the present application.

通信接口230可以为有线接口(例如以太网接口)或无线接口(例如蜂窝网络接口或使用无线局域网接口),用于与其他计算节点或装置进行通信。当通信接口230为有线接口时,通信接口230可以采用传输控制协议/网际协议(transmission control protocol/internet protocol,TCP/IP)之上的协议族,例如,远程函数调用(remote function call,RFC)协议、简单对象访问协议(simple object access protocol,SOAP)协议、简单网络管理协议(simple network management protocol,SNMP)协议、公共对象请求代理体系结构(common object request broker architecture,CORBA)协议以及分布式协议等等。Communication interface 230 may be a wired interface (eg, an Ethernet interface) or a wireless interface (eg, a cellular network interface or using a wireless local area network interface) for communicating with other computing nodes or devices. When the communication interface 230 is a wired interface, the communication interface 230 may use a protocol family above transmission control protocol/internet protocol (TCP/IP), for example, remote function call (RFC) protocol, simple object access protocol (SOAP) protocol, simple network management protocol (SNMP) protocol, common object request broker architecture (CORBA) protocol, and distributed protocols and many more.

存储器220可以存储有程序代码以及程序数据。其中,程序代码包括:创建模块110的代码、通信模块120的代码等,程序数据包括:第一数据、第二数据、第二进程的控制信息和第三进程的控制信息等等。在实际应用中,存储器220可以包括易失性存储器(volatilememory),例如随机存取存储器(random access memory,RAM);存储器也可以包括非易失性存储器(non-volatile memory),例如只读存储器(read-only memory,ROM)、快闪存储器(flash memory)、硬盘(hard disk drive,HDD)或固态硬盘(solid-state drive,SSD)存储器还可以包括上述种类的存储器的组合。The memory 220 may store program codes and program data. The program code includes: the code of the creation module 110, the code of the communication module 120, etc., and the program data includes: first data, second data, control information of the second process, control information of the third process, and so on. In practical applications, the memory 220 may include volatile memory (volatile memory), such as random access memory (random access memory, RAM); the memory may also include non-volatile memory (non-volatile memory), such as read-only memory (read-only memory, ROM), flash memory (flash memory), hard disk drive (HDD) or solid-state drive (solid-state drive, SSD) memory may also include a combination of the above-mentioned types of memory.

总线240可以是外设部件互连标准(peripheral component interconnect,PCI)总线或扩展工业标准结构(extended industry standard architecture,简称EISA)总线等。总线240可以分为地址总线、数据总线、控制总线等。为便于表示,图10中仅用一条粗线表示,但并不表示仅有一根总线或一种类型的总线。The bus 240 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (extended industry standard architecture, EISA for short) bus or the like. The bus 240 can be divided into an address bus, a data bus, a control bus, and the like. For ease of presentation, only one thick line is used in FIG. 10, but it does not mean that there is only one bus or one type of bus.

应当理解,进程间通信装置200仅为本申请实施例提供的一个例子,并且,进程间通信装置200可具有比图10示出的部件更多或更少的部件,可以组合两个或更多个部件,或者可具有部件的不同配置实现。It should be understood that the inter-process communication apparatus 200 is only an example provided by the embodiment of the present application, and the inter-process communication apparatus 200 may have more or less components than those shown in FIG. 10 , and two or more components may be combined. components, or may be implemented with different configurations of components.

本申请实施例还提供一种计算机存储介质,计算机存储介质中存储有指令,当其在处理器上运行时,可以实现上述方法实施例中的方法步骤,计算机存储介质的处理器在执行上述方法步骤的具体实现可参照上述方法实施例的具体操作,在此不再赘述。Embodiments of the present application further provide a computer storage medium, where instructions are stored in the computer storage medium, and when the computer storage medium runs on a processor, the method steps in the foregoing method embodiments can be implemented, and the processor of the computer storage medium is executing the foregoing method. For the specific implementation of the steps, reference may be made to the specific operations of the foregoing method embodiments, which will not be repeated here.

本申请实施例还提供一种计算机程序产品,当计算机程序产品被计算机读取并执行时,以实现上述方法实施例中记载的进程间通信方法的部分或者全部步骤。Embodiments of the present application further provide a computer program product, which, when the computer program product is read and executed by a computer, implements some or all of the steps of the inter-process communication method described in the above method embodiments.

在上述实施例中,可以全部或部分地通过软件、硬件或者其任意组合来实现。当使用软件实现时,可以全部或部分地以计算机程序产品的形式实现。所述计算机程序产品包括一个或多个计算机指令。在计算机上加载和执行所述计算机程序指令时,全部或部分地产生按照本申请实施例所述的流程或功能。所述计算机可以是通用计算机、专用计算机、计算机网络、或者其他可编程装置。所述计算机指令可以存储在计算机可读存储介质中,或者从一个计算机可读存储介质向另一个计算机可读存储介质传输,例如,所述计算机指令可以从一个网站站点、计算机、服务器或数据中心通过有线(例如同轴电缆、光纤、数字用户线(digital subscriberline,DSL))或无线(例如红外、无线、微波等)方式向另一个网站站点、计算机、服务器或数据中心进行传输。所述计算机可读存储介质可以是计算机能够存取的任何可用介质或者是包含一个或多个可用介质集成的服务器、数据中心等数据存储设备。所述可用介质可以是磁性介质(例如软盘、硬盘、磁带)、光介质(例如数字通用光盘(digital video disc,DVD))、或者半导体介质(例如SSD)等。In the above embodiments, it may be implemented in whole or in part by software, hardware or any combination thereof. When implemented in software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of the present application are generated. The computer may be a general purpose computer, special purpose computer, computer network, or other programmable device. The computer instructions may be stored in or transmitted from one computer readable storage medium to another computer readable storage medium, for example, the computer instructions may be downloaded from a website site, computer, server or data center Transmission to another website site, computer, server, or data center is by wire (eg, coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (eg, infrared, wireless, microwave, etc.). The computer-readable storage medium may be any available medium that can be accessed by a computer or a data storage device such as a server, data center, etc. that includes an integration of one or more available media. The usable media may be magnetic media (eg, floppy disks, hard disks, magnetic tapes), optical media (eg, digital video discs (DVDs)), or semiconductor media (eg, SSDs), and the like.

本申请实施例方法中的步骤可以根据实际需要进行顺序调整、合并或删减;本申请实施例装置中的模块可以根据实际需要进行划分、合并或删减。The steps in the method of the embodiment of the present application may be sequentially adjusted, combined or deleted according to actual needs; the modules in the device of the embodiment of the present application may be divided, combined or deleted according to actual needs.

以上对本申请实施例进行了详细介绍,本文中应用了具体个例对本申请的原理及实施方式进行了阐述,以上实施例的说明只是用于帮助理解本申请的方法及其核心思想;同时,对于本领域的一般技术人员,依据本申请的思想,在具体实施方式及应用范围上均会有改变之处,综上所述,本说明书内容不应理解为对本申请的限制。The embodiments of the present application have been introduced in detail above, and the principles and implementations of the present application are described in this paper by using specific examples. The descriptions of the above embodiments are only used to help understand the methods and core ideas of the present application; at the same time, for Persons of ordinary skill in the art, based on the idea of the present application, will have changes in the specific implementation manner and application scope. In summary, the contents of this specification should not be construed as limitations on the present application.

Claims (27)

1. An inter-process communication method, the method comprising:
a first process creates a first shared memory between a second process and a third process, wherein the first process, the second process and the third process run in user space, the first process has read-write permission to the first shared memory, the second process has read-write permission to the first shared memory, the third process has read-only permission for the first shared memory, the second process and the third process are both registered in the first process, a first mapping relation is formed between a first mapping area and the first shared memory, a second mapping relation is formed between a second mapping area and the first shared memory, the first mapping region is a region in the second process virtual address space that establishes a mapping relationship with the first shared memory, the second mapping area is an area in the third process virtual address space, which establishes a mapping relation with the first shared memory;
the second process writes first data into the first mapping area, maps the first data from the first mapping area to the first shared memory according to the first mapping relation, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relation, so that one-way communication from the second process to the third process is realized.
2. The method of claim 1, wherein after the first process creates the first shared memory between the second process and the third process, the method further comprises:
and the first process configures the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory.
3. The method of claim 2, wherein the first process configuring the second process with read-write permission of the first shared memory and configuring the third process with read-only permission of the first shared memory comprises:
and the first process configures the second process with the read-write permission of the first shared memory through an Access Control List (ACL), and configures the third process with the read-only permission of the first shared memory.
4. The method of any of claims 1 to 3, wherein before the second process writes the first data to the first mapping region, the method further comprises:
the second process allocates a first bank memory, and the first bank memory is used for storing the control information of the second process;
the second process acquires the control information of the second process from the first stack memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the third process allocates a second heap memory, and the second heap memory is used for storing control information of the third process;
the third process acquires the control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping region according to the control information of the third process.
5. The method of claim 4, wherein the first heap memory and the first mapping region are different memories, the first mapping region is used for storing shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used for storing shared memory information of the third process.
6. The method of any of claims 1 to 3, wherein before the second process writes the first data to the first mapping region, the method further comprises:
the first process allocates a third heap memory, and the third heap memory is used for storing control information of the second process;
the first process receives a first information acquisition request sent by the second process, wherein the first information acquisition request is used for requesting to acquire control information of the second process from the third heap memory;
the first process sends the control information of the second process to the second process according to the first information acquisition request, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the third process allocates a second heap memory, and the second heap memory is used for storing control information of the third process;
the third process acquires the control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping region according to the control information of the third process.
7. The method of claim 6, wherein the third heap memory and the first mapping region are different memories, the first mapping region is used for storing shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used for storing shared memory information of the third process.
8. The method of any of claims 1 to 3, wherein before the second process writes the first data to the first mapping region, the method further comprises:
the second process allocates a first bank memory, and the first bank memory is used for storing the control information of the second process;
the second process acquires the control information of the second process from the first stack memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the first process allocates a third heap memory, and the third heap memory is used for storing control information of the third process;
the first process receives a second information acquisition request sent by the third process, wherein the second information acquisition request is used for requesting to acquire control information of the third process from the third heap memory;
and the first process sends the control information of the third process to the third process according to the second information acquisition request, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
9. The method of claim 8, wherein the first heap memory and the first mapping region are different memories, the first mapping region is used for storing shared memory information of the second process, the third heap memory and the second mapping region are different memories, and the second mapping region is used for storing shared memory information of the third process.
10. The method of claim 1, wherein the first process comprises detecting a thread, the method further comprising:
the detection thread detects whether the second process or the third process is reset or not;
and under the condition that the second process or the third process is detected to be reset, the detection thread informs the second process and the third process that the reset process does not occur to terminate communication, and releases the first shared memory.
11. The method of claim 1, wherein the first process comprises a resume shared memory thread, and wherein the resume shared memory thread is a thread with a highest running level among threads included in the first process, the method further comprising:
and when the first process is restarted, the shared memory recovery thread recovers the first shared memory.
12. The method of any of claims 1 to 11, wherein the first shared memory includes a destroyer queue, and wherein the second process maps the first data from the first mapping region to the first shared memory, including:
the second process mapping the first data from the first mapping region to the destroyer queue;
the third process maps the first data from the first shared memory to the second mapping region, including:
the third process maps the first data from the destroyer queue to the second mapping region.
13. The method of claim 1, further comprising:
the first process creates a second shared memory between the second process and the third process, wherein the first process has read-write permission for the second shared memory, the third process has read-write permission for the second shared memory, the second process has read-only permission for the second shared memory, a third mapping region has a third mapping relationship with the second shared memory, a fourth mapping relationship has a fourth mapping relationship with the second shared memory, the third mapping region is a region in the virtual address space of the third process establishing a mapping relationship with the second shared memory, and the fourth mapping region is a region in the virtual address space of the second process establishing a mapping relationship with the second shared memory;
the third process writes the second data into the third mapping area, and maps the second data from the third mapping area to the second shared memory according to the third mapping relationship, and the second process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship, thereby realizing one-way communication from the third process to the second process.
14. An interprocess communication apparatus, comprising:
a creating module, configured to create a first shared memory between a second process and a third process through a first process, where the first process, the second process, and the third process operate in a user space, the first process has a read-write permission for the first shared memory, the second process has a read-write permission for the first shared memory, the third process has a read-only permission for the first shared memory, the second process and the third process are both registered in the first process, a first mapping relationship is provided between a first mapping region and the first shared memory, a second mapping relationship is provided between a second mapping region and the first shared memory, the first mapping region is a region in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping region is a region in a virtual address space of the third process that establishes a mapping relationship with the first shared memory An area;
a communication module, configured to write first data into the first mapping region through the second process, map the first data from the first mapping region to the first shared memory according to the first mapping relationship, and map the first data from the first shared memory to the second mapping region through the third process according to the second mapping relationship, so as to implement unidirectional communication from the second process to the third process.
15. The apparatus of claim 14, wherein after the first process creates the first shared memory between the second process and the third process, the creating module is further configured to:
the creating module configures, through the first process, the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory.
16. The apparatus of claim 15, wherein the creation module is specifically configured to:
and the creating module configures the second process to have the read-write permission of the first shared memory through the first process by an Access Control List (ACL), and configures the third process to have the read-only permission of the first shared memory.
17. The apparatus according to any one of claims 14 to 16, wherein before the second process writes the first data to the first mapping region, the apparatus further comprises: a distribution module and an acquisition module;
the allocation module is configured to allocate a first bank memory through the second process, where the first bank memory is used to store control information of the second process;
the obtaining module is configured to obtain, by the second process, control information of the second process from the first heap memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the allocating module is further configured to allocate a second heap memory through the third process, where the second heap memory is used to store control information of the third process;
the obtaining module is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
18. The apparatus of claim 17, wherein the first heap memory and the first mapping region are different memories, the first mapping region is configured to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is configured to store shared memory information of the third process.
19. The apparatus according to any one of claims 14 to 16, wherein before the second process writes the first data to the first mapping region, the apparatus further comprises a sending module;
the allocation module is configured to allocate a third heap of memory through the first process, where the third heap of memory is used to store control information of the second process;
the obtaining module is configured to receive, by the first process, a first information obtaining request sent by the second process, where the first information obtaining request is used to request to obtain control information of the second process from the third heap memory;
the sending module is configured to send, by the first process according to the first information acquisition request, the control information of the second process to the second process, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the allocating module is further configured to allocate a second heap memory through the third process, where the second heap memory is used to store control information of the third process;
the obtaining module is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
20. The apparatus of claim 19, wherein the third heap memory and the first mapping region are different memories, the first mapping region is configured to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is configured to store shared memory information of the third process.
21. The apparatus according to any one of claims 14 to 16, wherein before the second process writes the first data into the first mapping region, the allocating module is configured to allocate, by the second process, a first bank memory, the first bank memory being configured to store control information of the second process;
the obtaining module is configured to obtain, by the second process, control information of the second process from the first heap memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the apparatus further includes a receiving module;
the allocation module is configured to allocate a third heap of memory through the first process, where the third heap of memory is used to store control information of the third process;
the receiving module is configured to receive, through the first process, a second information acquisition request sent by the third process, where the second information acquisition request is used to request to acquire control information of the third process from the third heap memory;
the sending module is configured to send, by the first process according to the second information acquisition request, the control information of the third process to the third process, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
22. The apparatus of claim 21, wherein the first heap memory and the first mapping region are different memories, the first mapping region is configured to store shared memory information of the second process, the third heap memory and the second mapping region are different memories, and the second mapping region is configured to store shared memory information of the third process.
23. The apparatus according to claim 14, wherein the first process comprises a detection thread, and the apparatus further comprises a detection module, and the detection module is specifically configured to:
the detection module detects whether the second process or the third process is reset or not through the detection thread;
and under the condition that the detection module detects that the second process or the third process is reset, the detection module informs the second process and a process which is not reset in the third process to terminate communication through the detection thread, and releases the first shared memory.
24. The apparatus according to claim 14, wherein the first process includes a resume shared memory thread, and the resume shared memory thread is a thread with a highest running level among threads included in the first process, and the apparatus further includes a resume module, and the resume module is specifically configured to:
when the first process is restarted, the recovery module recovers the first shared memory through the recovery shared memory thread.
25. The apparatus according to any one of claims 14 to 24, wherein the first shared memory comprises a destroyer queue, and wherein the communication module is specifically configured to:
the communication module maps the first data from the first mapping region to the destroyer queue through the second process;
the communication module maps the first data from the destroyer queue to the second mapping region through the third process.
26. The apparatus of claim 14, wherein the creating module is further configured to create a second shared memory between the second process and the third process via the first process, wherein the first process has read-write permission to the second shared memory, the third process has read-write permission to the second shared memory, the second process has read-only permission to the second shared memory, a third mapping relation is formed between a third mapping area and the second shared memory, a fourth mapping relation is formed between a fourth mapping area and the second shared memory, the third mapping region is a region in the third process virtual address space that establishes a mapping relationship with the second shared memory, the fourth mapping region is a region in the second process virtual address space, which establishes a mapping relationship with the second shared memory;
the communication module is further configured to write the second data into the third mapping region through the third process, and map the second data from the third mapping region to the second shared memory according to the third mapping relationship, and the second process maps the second data from the second shared memory to the fourth mapping region according to the fourth mapping relationship, so that unidirectional communication from the third process to the second process is achieved.
27. A computer storage medium, characterized in that it stores a computer program for inter-process communication, which computer program, when being executed by a processor, carries out the method according to any one of claims 1 to 13.
CN202010262408.1A 2020-04-03 2020-04-03 An inter-process communication method and related device Active CN113495795B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010262408.1A CN113495795B (en) 2020-04-03 2020-04-03 An inter-process communication method and related device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010262408.1A CN113495795B (en) 2020-04-03 2020-04-03 An inter-process communication method and related device

Publications (2)

Publication Number Publication Date
CN113495795A true CN113495795A (en) 2021-10-12
CN113495795B CN113495795B (en) 2025-02-21

Family

ID=77995204

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010262408.1A Active CN113495795B (en) 2020-04-03 2020-04-03 An inter-process communication method and related device

Country Status (1)

Country Link
CN (1) CN113495795B (en)

Cited By (14)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114217982A (en) * 2021-11-25 2022-03-22 蚂蚁区块链科技(上海)有限公司 A method and device for sharing memory between processes
CN114296929A (en) * 2021-12-29 2022-04-08 成都三零瑞通移动通信有限公司 High-speed encryption and decryption method based on replaceable bounded lock-free queue and cipher machine
CN114528124A (en) * 2022-02-17 2022-05-24 福建天晴在线互动科技有限公司 Protocol-based process big data memory sharing method and system
CN115835047A (en) * 2022-08-24 2023-03-21 武汉中仪物联技术股份有限公司 Cross-network data transmission method and device, electronic equipment and storage medium
WO2023093675A1 (en) * 2021-11-25 2023-06-01 北京字节跳动网络技术有限公司 Bidirectional communication method, apparatus and device, and medium
WO2023134411A1 (en) * 2022-01-17 2023-07-20 闻泰通讯股份有限公司 Information transmission method, terminal apparatus, and storage medium
WO2023160253A1 (en) * 2022-02-25 2023-08-31 京东方科技集团股份有限公司 Service processing method and apparatus
CN116881026A (en) * 2023-09-06 2023-10-13 成都谐盈科技有限公司 Shared memory transmission system and method based on CORBA middleware
WO2023198103A1 (en) * 2022-04-14 2023-10-19 华为技术有限公司 Inter-process communication method and electronic device
CN117215803A (en) * 2022-06-02 2023-12-12 中移物联网有限公司 Process communication method and device, electronic equipment and readable storage medium
WO2024088194A1 (en) * 2022-10-28 2024-05-02 华为技术有限公司 Communication method and apparatus, and intelligent driving device
CN118093230A (en) * 2024-04-22 2024-05-28 深圳华锐分布式技术股份有限公司 Cross-process communication method, device, equipment and storage medium based on shared memory
CN118113495A (en) * 2024-04-29 2024-05-31 山东乾云启创信息科技股份有限公司 Safe inter-process data identification method and system based on shared memory
CN118964320A (en) * 2024-10-16 2024-11-15 深圳砺驰半导体科技有限公司 SOC-based file sharing method, system, storage medium and chip

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20020062401A1 (en) * 1998-06-16 2002-05-23 Marc Alan Auslander Technique for efficiently transferring moderate amounts of data across address space boundary
CN104123265A (en) * 2013-04-26 2014-10-29 华为技术有限公司 Method and system for communication among multiple kernels
CN104572313A (en) * 2013-10-22 2015-04-29 华为技术有限公司 Inter-process communication method and device
CN104881330A (en) * 2015-05-22 2015-09-02 大唐移动通信设备有限公司 Multi-process data sharing method and device
CN105868028A (en) * 2015-01-23 2016-08-17 华为技术有限公司 Method and device for sharing data between processes, and terminal

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20020062401A1 (en) * 1998-06-16 2002-05-23 Marc Alan Auslander Technique for efficiently transferring moderate amounts of data across address space boundary
CN104123265A (en) * 2013-04-26 2014-10-29 华为技术有限公司 Method and system for communication among multiple kernels
CN104572313A (en) * 2013-10-22 2015-04-29 华为技术有限公司 Inter-process communication method and device
CN105868028A (en) * 2015-01-23 2016-08-17 华为技术有限公司 Method and device for sharing data between processes, and terminal
CN104881330A (en) * 2015-05-22 2015-09-02 大唐移动通信设备有限公司 Multi-process data sharing method and device

Non-Patent Citations (3)

* Cited by examiner, † Cited by third party
Title
LLU´IS VILANOVA ET.AL: "Direct Inter-Process Communication (dIPC): Repurposing the CODOMs Architecture to Accelerate IPC", 《ACM.》, 30 December 2017 (2017-12-30), pages 16 - 31 *
VICTORY_523: "进程间通信——共享内存(Shared Memory)", Retrieved from the Internet <URL:https://blog.csdn.net/ypt523/article/details/79958188> *
马陈燕: "基于共享内存的多处理器系统间通信技术研究", 《福建电脑》, no. 7, 31 July 2010 (2010-07-31), pages 42 - 76 *

Cited By (17)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN114217982A (en) * 2021-11-25 2022-03-22 蚂蚁区块链科技(上海)有限公司 A method and device for sharing memory between processes
WO2023093675A1 (en) * 2021-11-25 2023-06-01 北京字节跳动网络技术有限公司 Bidirectional communication method, apparatus and device, and medium
CN114296929A (en) * 2021-12-29 2022-04-08 成都三零瑞通移动通信有限公司 High-speed encryption and decryption method based on replaceable bounded lock-free queue and cipher machine
WO2023134411A1 (en) * 2022-01-17 2023-07-20 闻泰通讯股份有限公司 Information transmission method, terminal apparatus, and storage medium
CN114528124A (en) * 2022-02-17 2022-05-24 福建天晴在线互动科技有限公司 Protocol-based process big data memory sharing method and system
WO2023160253A1 (en) * 2022-02-25 2023-08-31 京东方科技集团股份有限公司 Service processing method and apparatus
WO2023198103A1 (en) * 2022-04-14 2023-10-19 华为技术有限公司 Inter-process communication method and electronic device
CN117215803A (en) * 2022-06-02 2023-12-12 中移物联网有限公司 Process communication method and device, electronic equipment and readable storage medium
CN115835047A (en) * 2022-08-24 2023-03-21 武汉中仪物联技术股份有限公司 Cross-network data transmission method and device, electronic equipment and storage medium
WO2024088194A1 (en) * 2022-10-28 2024-05-02 华为技术有限公司 Communication method and apparatus, and intelligent driving device
CN116881026A (en) * 2023-09-06 2023-10-13 成都谐盈科技有限公司 Shared memory transmission system and method based on CORBA middleware
CN116881026B (en) * 2023-09-06 2023-11-21 成都谐盈科技有限公司 A shared memory transmission system and method based on CORBA middleware
CN118093230A (en) * 2024-04-22 2024-05-28 深圳华锐分布式技术股份有限公司 Cross-process communication method, device, equipment and storage medium based on shared memory
CN118113495A (en) * 2024-04-29 2024-05-31 山东乾云启创信息科技股份有限公司 Safe inter-process data identification method and system based on shared memory
CN118113495B (en) * 2024-04-29 2024-07-16 山东乾云启创信息科技股份有限公司 Safe inter-process data identification method and system based on shared memory
CN118964320A (en) * 2024-10-16 2024-11-15 深圳砺驰半导体科技有限公司 SOC-based file sharing method, system, storage medium and chip
CN118964320B (en) * 2024-10-16 2025-03-07 深圳砺驰半导体科技有限公司 SOC-based file sharing method, system, storage medium and chip

Also Published As

Publication number Publication date
CN113495795B (en) 2025-02-21

Similar Documents

Publication Publication Date Title
CN113495795A (en) Inter-process communication method and related equipment
US9519795B2 (en) Interconnect partition binding API, allocation and management of application-specific partitions
US6647423B2 (en) Direct message transfer between distributed processes
CN114756388B (en) A method for on-demand shared memory between cluster system nodes based on RDMA
WO2021217529A1 (en) Method and system for inter-process communication
US8484307B2 (en) Host fabric interface (HFI) to perform global shared memory (GSM) operations
US7877436B2 (en) Mechanism to provide reliability through packet drop detection
US7797588B2 (en) Mechanism to provide software guaranteed reliability for GSM operations
WO2017008675A1 (en) Method and device for transmitting data in virtual environment
US7966454B2 (en) Issuing global shared memory operations via direct cache injection to a host fabric interface
CN112035272A (en) Method, apparatus and computer equipment for interprocess communication
CN111897666A (en) Method, apparatus and system for communication between multiple processes
US20150370582A1 (en) At least one user space resident interface between at least one user space resident virtual appliance and at least one virtual data plane
US8255913B2 (en) Notification to task of completion of GSM operations by initiator node
US20090199182A1 (en) Notification by Task of Completion of GSM Operations at Target Node
CN114371811A (en) Method, electronic device and computer program product for storage management
US8275947B2 (en) Mechanism to prevent illegal access to task address space by unauthorized tasks
US8214604B2 (en) Mechanisms to order global shared memory operations
WO2025002060A1 (en) Method and apparatus for pcie device to pass through to virtual machine, and related device
US8146094B2 (en) Guaranteeing delivery of multi-packet GSM messages
Mahar et al. Telepathic Datacenters: Fast RPCs using Shared CXL Memory
US8200910B2 (en) Generating and issuing global shared memory operations via a send FIFO
CN118069386A (en) Resource access method and device
US7051158B2 (en) Single computer distributed memory computing environment and implementation thereof
CN119065873B (en) Process communication method, device, electronic equipment and readable storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination
GR01 Patent grant
GR01 Patent grant