An inter-process communication method and related device

By creating shared memory between the second and third processes in the first process and using mapping relationships to achieve one-way communication, the problems of low communication performance and low security in the prior art are solved, and high efficiency is achieved , secure inter-process communication.

CN113495795BActive Publication Date: 2025-02-21HUAWEI TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202010262408.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-04-03
Publication Date
2025-02-21
Estimated Expiration
2040-04-03

AI Technical Summary

Technical Problem

The existing inter-process communication mechanism has problems of low performance and low security.

Method used

The first process creates shared memory between the second process and the third process, and realizes one-way communication between processes through mapping relationships, avoiding system calls and data copying, and improving communication efficiency and security.

Benefits of technology

It realizes efficient and secure inter-process communication, improves performance and enhances security and reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113495795B_ABST
    Figure CN113495795B_ABST
Patent Text Reader

Abstract

The present application provides an inter-process communication method and related equipment, the method comprising: 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 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process. The above method can not only improve the security and reliability of inter-process communication, but also effectively improve the performance of inter-process communication.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of communication technology, and in particular to an inter-process communication (IPC) method and related equipment. Background Art

[0002] IPC is a communication method for sharing messages between one process and another, and is mainly used for message transmission 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. In other words, a process can only access its own virtual address space and cannot access the virtual address space of other processes. In order to enable different processes to access each other and coordinate work, the operating system provides a variety of IPC mechanisms.

[0003] However, existing IPC mechanisms have problems such as low performance or low security. Summary of the invention

[0004] The embodiments of the present application disclose an inter-process communication method and related equipment, which can not only improve the security and reliability of inter-process communication, but also effectively improve the performance of inter-process communication.

[0005] In a first aspect, the present application provides an inter-process communication method, the method comprising:

[0006] 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 a 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, 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, a first mapping area has a first mapping relationship with the first shared memory, a second mapping area has a second mapping relationship with the first shared memory, the first mapping area is an area in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is an area in a virtual address space of the third process that establishes a mapping relationship with the first shared memory;

[0007] 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

[0008] The inter-process communication method provided by the present application creates a first shared memory between the second process and the third process through the first process, 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process. Among them, 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 is not necessary to fall into the kernel space through the system call kernel to realize it, and it is not necessary to copy data, the system overhead is small, and the communication efficiency is high. Moreover, when the inter-process communication method provided by the present application is applied to embedded devices with diverse 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 problem of single board and operating system adaptation, and the operation is simple and the portability is strong. At the same time, in addition to the first process, only the second process has the read and write permissions of the first shared memory, which can avoid multiple processes from writing to the first shared memory at the same time, causing data confusion in the first shared memory. In addition, the second process and the third process are both processes registered with the first process. The first process can verify the identities of the second process and the third process, and only processes with read and write permissions or read-only permissions to the first shared memory can access the first shared memory, which can prevent malicious programs from tampering with or destroying data in the first shared memory, and can effectively improve the security and reliability of inter-process communication.

[0009] In a specific implementation, after the first process creates a first shared memory between the second process and the third process, the method further includes:

[0010] The first process configures the second process to have read and write permissions for the first shared memory, and configures the third process to have read-only permissions for the first shared memory.

[0011] In the above scheme, the first process configures the second process to have read and write permissions for the first shared memory, and the third process is configured to have read-only permissions for the first shared memory, ensuring that only processes with read and write permissions for the first shared memory can write to the first shared memory, and only processes with read-only permissions for the first shared memory can read from the first shared memory, which can prevent malicious programs from performing read and write operations on the first shared memory and ensure that data in the first shared memory will not be modified or even destroyed by malicious programs.

[0012] In a specific implementation, the first process configures the second process to have read and write permissions for the first shared memory, and configures the third process to have read-only permissions for the first shared memory, including:

[0013] The first process configures the second process to have read and write permissions for the first shared memory through an access control list (ACL), and configures the third process to have read-only permissions for the first shared memory.

[0014] In a specific implementation, before the second process writes the first data into the first mapping area, the method further includes:

[0015] The second process allocates a first heap memory, where the first heap memory is used to store control information of the second process;

[0016] The second process obtains 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;

[0017] Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

[0018] The third process allocates a second heap memory, where the second heap memory is used to store control information of the third process;

[0019] The third process obtains 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.

[0020] In the above scheme, by storing the control information of the second process in the first heap memory (heap memory of the first process) and storing the control information of the third process in the second heap memory (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 memories 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 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 conducive to the second process and the third process being 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.

[0021] In a specific implementation, 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0022] In the above scheme, the control information of the second process is stored in the first heap memory, and the shared memory information of the second process is stored in the first mapping area. The first heap memory and the first mapping area are different memories, which can realize the separate storage of the control information of the second process and the shared memory information of the second process, which is conducive to ensuring 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. The second heap memory and the second mapping area are different memories, which can realize the separate storage of the control information of the third process and the shared memory information of the third process, which is conducive to ensuring the security of the shared memory information of the third process.

[0023] In a specific implementation, before the second process writes the first data into the first mapping area, the method further includes:

[0024] The first process allocates a third heap memory, and the third heap memory is used to store control information of the second process;

[0025] 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;

[0026] 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;

[0027] Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

[0028] The third process allocates a second heap memory, where the second heap memory is used to store control information of the third process;

[0029] The third process obtains 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.

[0030] In the above scheme, 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, when the second process communicates with the third process, the second process needs to first send a request to the first process to obtain the control information, 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. In other words, the method of storing the control information of the process in the process's own heap memory is suitable for processes with high performance requirements; the method of storing the control information of the process in the heap memory of the first process is suitable for processes with lower performance requirements. It can be seen that the above method is not only suitable for processes with high communication performance requirements, but also 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 information of the third process can also be stored separately, which is conducive to the second process and the third process being 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.

[0031] In a specific implementation, 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0032] In the above scheme, 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 stored separately, which is beneficial to ensuring the security 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 ensuring the security of the shared memory information of the third process.

[0033] In a specific implementation, before the second process writes the first data into the first mapping area, the method further includes:

[0034] The second process allocates a first heap memory, where the first heap memory is used to store control information of the second process;

[0035] The second process obtains 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;

[0036] Before the third process maps the first data from the first shared memory to the second mapping area, the method further includes:

[0037] The first process allocates a third heap memory, and the third heap memory is used to store control information of the third process;

[0038] The first process receives a second information acquisition request sent by the third process, wherein the second information acquisition request is used to request to acquire control information of the third process from the third heap memory;

[0039] 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.

[0040] In a specific implementation, 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, the third heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0041] In the above scheme, 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 stored separately, which is conducive to ensuring the security 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 conducive to ensuring the security of the shared memory information of the third process.

[0042] In a specific implementation, the first process includes a detection thread, and the method further includes:

[0043] The detection thread detects whether the second process or the third process is reset;

[0044] When it is detected that the second process or the third process is reset, the detection thread notifies the second process or the third process that is not reset to terminate communication and release the first shared memory.

[0045] In the above scheme, the first process can also detect whether the second process or the third process is reset. When it is detected 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 to release the resources occupied by the first shared memory, which is conducive to timely recovery of resources.

[0046] In a specific implementation, the first process includes a restored shared memory thread, the restored shared memory thread is a thread with the highest running level among the threads included in the first process, and the method further includes:

[0047] When the first process is restarted, the shared memory recovery thread recovers the first shared memory.

[0048] In the above solution, when the first process is restarted, the shared memory recovery thread with the highest running level in the first process can quickly recover the first shared memory, further improving the reliability of inter-process communication.

[0049] 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:

[0050] The second process maps the first data from the first mapping area to the destroyer queue;

[0051] The third process maps the first data from the first shared memory to the second mapping area, including:

[0052] The third process maps the first data from the destroyer queue to the second mapping area.

[0053] In the above solution, the first shared memory includes a destroyer queue, which supports repeated reading and writing of the first shared memory, can effectively improve the throughput of inter-process information, and further improve the performance of inter-process communication.

[0054] In a specific implementation, the method further includes:

[0055] 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 for the second shared memory, the third process has read and write permissions for the second shared memory, the second process has read-only permissions for the second shared memory, a third mapping area has a third mapping relationship with the second shared memory, a fourth mapping area has a fourth mapping relationship with 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 area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

[0056] 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.

[0057] In the above scheme, 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 maps the second data from the third mapping area to the second shared memory according to the third mapping relationship, and the third process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship. Thus, the one-way communication from the third process to the second process is realized. Among them, the process of the third process transmitting the second data to the second process is realized by mapping through the third mapping relationship and the fourth mapping relationship, which can be realized without falling into the kernel space through the system call kernel, and data copying is not required, the system overhead is small, and the communication efficiency is high. Moreover, when applied to embedded devices with diverse 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 single board and operating system adaptation, the operation is simple, and the portability is strong. At the same time, in addition to the first process, only the third process has the read and write permissions of the second shared memory, which can avoid multiple processes from writing to the second shared memory at the same time, or malicious programs tampering with or destroying the data of the second shared memory, causing data confusion in the second 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 processes with read and write permissions or read-only permissions for the second shared memory can access the second shared memory, which can effectively improve the security and reliability of inter-process communication.

[0058] In a second aspect, the present application provides an inter-process communication device, the device comprising:

[0059] A creation module, used for creating a first shared memory between a second process and a third process through a first process, wherein the first process, the second process and the third process run in a 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, 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, a first mapping area has a first mapping relationship with the first shared memory, a second mapping area has a second mapping relationship with the first shared memory, the first mapping area is an area in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is an area in a virtual address space of the third process that establishes a mapping relationship with the first shared memory;

[0060] A communication module is used to write the first data into the first mapping area through the second process, and map the first data from the first mapping area 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 area through the third process according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

[0061] In a specific implementation, the creation module is further used to:

[0062] The creation module configures the second process to have read and write permissions for the first shared memory through the first process, and configures the third process to have read-only permissions for the first shared memory.

[0063] In a specific implementation, the creation module is specifically used to:

[0064] The creation module configures the second process to have read and write permissions of the first shared memory through an access control list (ACL) through the first process, and configures the third process to have read-only permissions of the first shared memory.

[0065] In a specific implementation, before the second process writes the first data into the first mapping area, the device further includes: an allocation module and an acquisition module;

[0066] The allocation module is used to allocate a first heap memory through the second process, and the first heap memory is used to store control information of the second process;

[0067] The acquisition module is configured to acquire control information of the second process from the first heap memory through 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;

[0068] Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module is further used 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;

[0069] The acquisition module is further used to acquire 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 mapping area according to the control information of the third process.

[0070] In a specific implementation, 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0071] In a specific implementation, before the second process writes the first data into the first mapping area, the device further includes a sending module;

[0072] The allocation module is used to allocate a third heap memory through the first process, and the third heap memory is used to store control information of the second process;

[0073] The acquisition module is configured to receive, through the first process, a first acquisition information request sent by the second process, wherein the first acquisition information request is used to request to acquire control information of the second process from the third heap memory;

[0074] The sending module is used to send the control information of the second process to the second process according to the first information acquisition request through the first process, so that the second process writes the first data into the first mapping area according to the control information of the second process;

[0075] Before the third process maps the first data from the first shared memory to the second mapping area, the allocation module is further used 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;

[0076] The acquisition module is further used to acquire 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 mapping area according to the control information of the third process.

[0077] In a specific implementation, 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0078] In a specific implementation, before the second process writes the first data into the first mapping area,

[0079] The allocation module is used to allocate a first heap memory through the second process, and the first heap memory is used to store control information of the second process;

[0080] The acquisition module is configured to acquire control information of the second process from the first heap memory through 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;

[0081] Before the third process maps the first data from the first shared memory to the second mapping area, the device further includes a receiving module;

[0082] The allocation module is used to allocate a third heap memory through the first process, and the third heap memory is used to store control information of the third process;

[0083] The receiving module is used to receive, through the first process, a second information acquisition request sent by the third process, wherein the second information acquisition request is used to request to acquire control information of the third process from the third heap memory;

[0084] The sending module is used to send the control information of the third process to the third process through the first 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.

[0085] In a specific implementation, 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, the third heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

[0086] In a specific implementation, the first process includes a detection thread, and the device further includes a detection module, and the detection module is specifically configured to:

[0087] The detection module detects, through the detection thread, whether the second process or the third process is reset;

[0088] When the detection module detects that the second process or the third process is reset, it notifies the second process or the third process that is not reset to terminate communication through the detection thread and releases the first shared memory.

[0089] In a specific implementation, the first process includes a restored shared memory thread, the restored shared memory thread is a thread with the highest running level among the threads included in the first process, and the device further includes a restoration module, the restoration module is specifically used to:

[0090] When the first process is restarted, the recovery module recovers the first shared memory through the recovery shared memory thread.

[0091] In a specific implementation, the first shared memory includes a destroyer queue, and the communication module is specifically used for:

[0092] The communication module maps the first data from the first mapping area to the destroyer queue through the second process;

[0093] The communication module maps the first data from the destroyer queue to the second mapping area through the third process.

[0094] In a specific implementation, the creation module is further used 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, 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, a third mapping area has a third mapping relationship with the second shared memory, a fourth mapping area has a fourth mapping relationship with 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 area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

[0095] The communication module is also used 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 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.

[0096] In a third aspect, the present application provides an inter-process communication device, comprising: a processor, a communication interface and a memory; the memory is used to store instructions, the processor is used to execute the instructions, and the communication interface is used to receive or send data; wherein, when the processor executes the instructions, it performs the method described in the first aspect or any specific implementation of the first aspect.

[0097] In a fourth aspect, the present application provides a computer storage medium storing a computer program for inter-process communication, wherein the computer program, when executed by a processor, implements the method described in the first aspect or any specific implementation of the first aspect.

[0098] 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, it implements the method described in the above-mentioned first aspect or any specific implementation of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS

[0099] The following is an introduction to the drawings used in the embodiments of the present application.

[0100] Figure 1 It is a schematic diagram of an application scenario of the inter-process communication method provided in an embodiment of the present application;

[0101] Figure 2 is a schematic diagram of a data transmission process provided by an embodiment of the present application;

[0102] Figure 3 is a schematic diagram of an inter-process communication system based on a binder mechanism provided in an embodiment of the present application;

[0103] Figure 4 It is a flowchart of an inter-process communication method provided by an embodiment of the present application;

[0104] Figure 5 It is a schematic diagram of a destroyer queue provided in an embodiment of the present application;

[0105] Figure 6 is a schematic diagram of a process virtual address space provided in an embodiment of the present application;

[0106] Figure 7 It is a flowchart of an inter-process communication method provided by an embodiment of the present application;

[0107] Figure 8 It is an interactive schematic diagram of an inter-process communication method provided in an embodiment of the present application;

[0108] Fig. 9 It is a structural diagram of an inter-process communication device provided in an embodiment of the present application;

[0109] Fig.10 It is a structural diagram of another inter-process communication device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0110] The embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application.

[0111] Before introducing the method in the embodiments of the present application, in order to facilitate technical personnel in the field to understand the embodiments of the present application, related concepts involved in the embodiments of the present application are first introduced.

[0112] Process isolation is a technology designed to protect processes in the operating system from interfering with each other. This technology is to prevent process A from modifying the data in process B, so that process A does not affect the operation of process B, or even destroy the operation of process B. Processes are isolated through virtual address space. 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 the data in process B.

[0113] Virtual address space: Virtual address space does not really exist in the computer. Each process is assigned its own virtual address space and can only access the virtual address space that it is assigned to use. 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, thus achieving effective process isolation.

[0114] Physical address space: The physical address space is an entity that exists in the computer and is unique in each computer. It can also be called physical memory. For example, on a 32-bit machine, the size of the physical memory can theoretically reach 4GB, but if 512MB of memory is actually installed, then the real effective part of its physical address space is only 512MB, and the rest is invalid.

[0115] There is a mapping between the virtual address space and the physical address space. The mapping is done through the memory management unit (MMU). The process can indirectly operate the physical address space by operating its own virtual address space.

[0116] User space is the space where user programs run, and kernel space is the space where the system kernel runs. Current operating systems all use virtual memory. For a 32-bit system, its virtual address space is 2 to the power of 32, that is, 4GB. The core of the operating system is the kernel, which is independent of ordinary applications and can access protected virtual address spaces and underlying hardware devices. In order to protect user processes 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, called kernel space, and the lower 3GB bytes are used by each process, called user space. In other words, in the 4GB virtual address space of each process, the highest 1GB is the same, that is, kernel space. Only the remaining 3GB is used by the process itself. In other words, kernel space is shared by all processes.

[0117] User state: When a process runs in user space, it is in user state. At this time, the processor runs in the Ring3 user code with the lowest privilege level. The Ring3 state cannot access the address space of Ring0, including code and data.

[0118] Kernel state: When a process runs in kernel space, it is in kernel state. The process can execute system calls and fall into kernel space to run. At this time, the processor is executing in the Ring0 kernel code with the highest privilege level.

[0119] Although the operating system logically divides the user space and 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, and these operations need to be implemented with the help of system calls. System calls are the only way for processes in user space to access kernel space, ensuring that all operations are performed under the control of the kernel, avoiding unauthorized access of processes in user space to kernel space, and improving the security and stability of the system.

[0120] In user mode, a process can only access memory with restrictions and is not allowed to access peripheral devices such as hard disks and network cards. The ability to occupy the central processing unit (CPU) is deprived, and CPU resources can be obtained by other processes.

[0121] 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.

[0122] The application scenarios of IPC can be a process sending its data to another process, a process sending a message to another process to notify the other process that a certain event has occurred, or one of the two processes modifies shared data and the other process should see it immediately.

[0123] Here, taking a process sending its data to another process as an example, the application scenario of the inter-process communication method provided in the embodiment of the present application is introduced.

[0124] like Figure 1 As shown, both process A and process B are located in the 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 and there is process isolation, process B cannot directly use data A in process A. If process B is to be able to use data A in process A, a special communication mechanism must be adopted, namely IPC.

[0125] 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) model, and process B is the client process in the C / S model. Similarly, if process B provides its own data B to process A, process B is the server process in the C / S model, and process A is the client process in the C / S model. In the C / S model, the process running on the client is the client process, and the process running on the server is the server process.

[0126] Currently, in order to enable different processes to access each other and coordinate work, the operating system provides a variety of IPC mechanisms, such as sockets, pipes, message queues, shared memory, and binders.

[0127] Among them, the socket used for inter-process communication is an abstract 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 like a file. The socket can be regarded as an endpoint in the communication connection between two network processes when they communicate. During communication, process A writes the data A to be transmitted into the socket of the host where process A is located. The socket sends the data A to the socket of the host where process B is located through the transmission medium of the network interface card, so that the data A can be transmitted to process B, thereby realizing the communication between process A and process B. However, when the process uses the socket to communicate, the process needs to execute the system call and fall into the kernel space to perform data transmission, and the transmission efficiency is low. Therefore, socket is mainly used for inter-process communication on different hosts across the network and low-speed communication between processes on the same host.

[0128] Pipes used for inter-process communication are the oldest form of Unix IPC and are special files in memory. Pipes are divided into named pipes and anonymous pipes. Named pipes allow two unrelated processes to communicate, while anonymous pipes can only communicate in one direction and can only be used between related processes, often used for parent-child processes. For problems with pipes, see Figure 2 and related descriptions.

[0129] The message queue used for inter-process communication is a linked list of messages stored in the kernel and identified by a message queue identifier. By using a message queue, processes can read messages in order or prioritize messages. For problems with message queues, see Figure 2 and related descriptions.

[0130] like Figure 2 As shown in the figure, the pipes and message queues used for inter-process communication, when performing 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 area. Then, process A falls into the kernel space through a system call, and the kernel calls the copy from user state to kernel state (copy fromuser) function to copy data A from the memory cache area of ​​process A to the kernel cache area opened by the kernel. After the kernel copies data A to the kernel cache area, the kernel calls the copy from kernel state to user state (copy to user) function to copy data A from the kernel cache area to the memory cache area opened by process B. In this way, process A and process B complete the transmission of data A, that is, process A and process B complete an inter-process communication.

[0131] It can be seen that for the pipes and message queues used for inter-process communication, when performing data transmission, process A also needs to execute a system call and fall into the kernel space to perform data transmission, and the kernel needs to copy data A from the memory cache of process A to the kernel cache, and then from the kernel cache to the memory cache of process B. This process requires two data copies, and the efficiency of data transmission is low.

[0132] The shared memory used for inter-process communication is the same physical memory that is allowed to be accessed by process A and process B. Process A and process B can map the same physical memory to their own virtual address space, and then both process A and process B can access the physical memory.

[0133] 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, data A in process A's virtual address space is mapped to shared memory, and then, through the mapping relationship between process B's virtual address space and shared memory, data A in shared memory is mapped to the virtual address space of process B. In other words, when process A writes data A into 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, and thus can be read by process B in a timely manner.

[0134] It can be seen that the shared memory used for inter-process communication can realize data transmission only by mapping, without the need for data copying, so it can realize efficient communication between processes, and is a very effective way to share and transfer data between processes. However, the control of shared memory is complex. 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 write or read the shared memory at the same time, multiple processes compete for resources, which will cause data in the shared memory to be disordered. 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 resource competition caused by multiple processes reading or writing to the shared memory at the same time.

[0135] In addition, in the above-mentioned mechanisms for inter-process communication, when data is transmitted, process A and process B cannot obtain the reliable user identification (UID) or process identification (PID) of the other party, that is, process A and process B cannot reliably verify the identity of the other party. At the same time, the access points of the above-mentioned communication mechanisms are all open, and it is impossible to establish a private communication channel between process A and process B. Malicious programs can establish a connection with another process and steal messages by guessing the address of process A or process B. It can be seen that the above-mentioned mechanisms for inter-process communication still have the problem of low security.

[0136] Next, the binder mechanism, which is currently popular for inter-process communication, is introduced. The binder mechanism is an inter-process communication mechanism in the Android operating system and is commonly used in Android devices.

[0137] like Figure 3 As shown in the figure, an IPC system based on the binder mechanism usually consists of 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.

[0138] Server process (such as process A): a process that provides services (such as data A) for client processes to use.

[0139] Client process (such as process B): A process that wants to use the services (such as data A) in the server process.

[0140] 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.

[0141] Service management module: If the server process wants to provide services to the client process, the server process must first submit its own UID or PID to the service management module for registration, and register the services it can provide in the service management module so that the service management module knows what 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 to the service management module for registration, and then it can go to the service management module to obtain services.

[0142] from Figure 3 It can also be seen that shared memory is used in the binder mechanism for inter-process communication, and the shared memory is implemented through memory mapping.

[0143] The process of data transmission through shared memory in the binder mechanism is as follows:

[0144] Process A copies data A from the virtual address space of process A to the kernel cache by calling the copy from user function. Since there is a mapping between the kernel cache and the data receiving cache, data A in the kernel cache can be directly mapped to the data receiving cache. Since there is a mapping between the data receiving cache and the virtual address space of process B, data A in the data receiving cache is directly mapped to the virtual address space of process B. In this way, process A and process B complete a data transmission of A, 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, that is, this data transmission process requires a data copy.

[0145] In addition, from Figure 3 It can also be seen that the client process, server process and service management module run in user space, and the binder driver runs in kernel space. The interaction between the client process, server process and service management module is a dotted line, indicating that the client process, server process and service management module do not interact directly, but interact through the binder driver. In other words, the interaction between the client process, server process and service management module needs to fall into the kernel space through system calls and interact through the binder driver located in the kernel space.

[0146] From the above introduction to the binder mechanism for inter-process communication, it can be seen that when process A and process B communicate through the binder mechanism, both process A and process B need to register in the service management module, and then process A and process B can communicate. 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, the binder mechanism for inter-process communication introduces shared memory, which improves the efficiency of inter-process communication. However, the binder mechanism for inter-process communication also introduces kernel state during the communication process. When data is transmitted, a data copy is also required, which affects the performance of the operating system and cannot meet some inter-process communication scenarios that require zero data copy. At the same time, the binder mechanism is an IPC mechanism in the Android operating system and is not suitable for terminal devices built on operating systems such as Linux and Windows.

[0147] In addition, for the above-mentioned inter-process communication mechanisms except shared memory, when data is transferred between processes, the processes need to execute system calls and fall into the kernel space to transfer data, which introduces the kernel state. When the above-mentioned inter-process communication mechanisms except shared memory are applied to embedded devices with various operating systems, the single board and operating system of the embedded device need to be adapted, which is complicated to operate and has low 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.

[0148] 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 state, the present application provides an inter-process communication method, which can not only improve the security and reliability of inter-process communication, but also effectively improve the performance of inter-process communication. The inter-process communication method provided in the embodiment of the present application is not only applicable to terminal devices built based on Android operating system, but also to terminal devices built based on Linux, Windows and other operating systems, which are not specifically limited here.

[0149] The inter-process communication method provided in the embodiment of the present application is described in detail below.

[0150] See also Figure 4 , Figure 4 is a flowchart of an inter-process communication method provided by an embodiment of the present application, such as Figure 4 As shown, the method may include:

[0151] S101: A first process creates a first shared memory between a second process and a third process.

[0152] Among them, the first process, the second process and the third process run in the 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, the third process has read-only permissions to the first shared memory, and the second process and the third process are both registered in the first process.

[0153] The second process and the third process are both 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. Here, the process of registering the second process and the third process in the first process is described.

[0154] Taking the second process registering in the first process as an example, specifically, the first process and the second process can establish a connection through a domain socket communication mechanism, and after the first process and the second process establish a connection, the first process registers for the second process. The domain socket is an IPC mechanism developed on the framework of the socket, which is used for inter-process communication on the same host.

[0155] More specifically, the first process can monitor whether a process has sent a registration request to it through the listen function in the domain socket communication mechanism. When the first process monitors that the second process has sent a registration request to it, the first process can 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 and the second process establish a connection, the first process can register the second process according to the registration request. The second process can send a registration request to the first process through the connect function in the domain socket communication mechanism.

[0156] Among them, 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 to register the second process based on information such as the UID or PID of the second process.

[0157] 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. The specific implementation can refer to the process of registering the second process in the first process described in the above embodiment, which will not be elaborated here.

[0158] When the first process registers the second process, the first process may also create a first shared memory based on 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 unidirectional communication from the second process to other processes (such as the third process). Similarly, when the first process registers the third process, the first process may also create a second shared memory based on information such as the UID or PID of the third process, and the second shared memory is used to implement unidirectional communication from the third process to other processes (such as the second process).

[0159] In the embodiment of the present application, the first process creates the first shared memory by creating the first shared file, the first shared file is a file that actually exists in the physical memory, and the physical address of the first shared memory is the same as the physical address of the first shared file. Similarly, the first process creates the second shared memory by creating the second shared file, the second shared file is also a file that actually exists in the physical memory, and the physical address of the second shared memory is the same as the physical address of the second shared file.

[0160] Among them, the first shared memory and the second shared memory may also have a shared memory identifier, which is used to uniquely identify a shared memory, and the shared memory identifier may be named according to 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 the UID or PID of the second process, and the identifier of the second shared memory may be named according to the UID or PID of the third process. The first shared file and the second shared file may also have a shared file identifier, which is used to uniquely identify a shared file, and the shared file identifier may 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 the UID or PID of the second process, and the identifier of the second shared file may be named according to the UID or PID of the third process.

[0161] 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, and the second shared memory identifier, etc. When the first process needs to manage the second process or the third process, the first process may manage the process according to information such as the UID or PID of the process, thereby meeting the management requirements of the process; when the first process needs to manage the first shared memory or the second shared memory, the first process may find the corresponding shared memory according to the shared memory identifier for management, thereby meeting the management requirements of the shared memory.

[0162] In an embodiment of the present application, after the first process completes the creation of the first shared memory, the first process, as the creator of the first shared memory, that is, the owner, not only has the read and write permissions of the first shared memory, but also has the permission to configure the first shared memory for other processes. For example, the first process can configure the second process to have the read and write permissions of the first shared memory, and configure the third process to have the read-only permission of the first shared memory. Among them, the process has the read and write permissions of the first shared memory, which means that the process can 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 read the first shared memory and cannot write to it.

[0163] It can be seen from the above embodiments that the first shared memory corresponds to the first shared file. Therefore, the first process can configure the second process to have read and write permissions for the first shared file so that the second process has read and write permissions for the first shared memory, and can configure the third process to have read-only permissions for the first shared file so that the third process has read-only permissions for the first shared memory.

[0164] Specifically, the first process can configure the second process to have read and write permissions to the first shared memory, and configure the third process to have read-only permissions to the first shared memory through access control lists (ACL). Among them, ACL is an access control list for files / directories. ACL allows any user (process) to set access permissions for any file / directory, and it consists of a series of rules. Access permissions include 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.

[0165] Specifically, the first process can set the second process and the third process as other people of the first shared file through the command of setting file ACL permissions (setfacl) in ACL, and set the second process to have read and write permissions (rw-) for the first shared file, and set the third process to have read-only permissions (r--) for the first shared file. After the setting is completed, the first process can check whether the second process has read and write permissions (rw-) for the first shared file, and check whether the third process has read-only permissions (r--) for the first shared file through the command of viewing file ACL permissions (getfacl) in ACL. The second process has read and write permissions for the first shared file, which means that the second process has read and write permissions for the first shared memory. The third process has read-only permissions for the first shared file, which means that the third process has read-only permissions for the first shared memory.

[0166] After the second process has the read-write permission of the first shared memory, if the second process wants to perform read-write operations on the first shared memory, the second process also needs to establish a mapping relationship between its own virtual address space and the first shared memory. Only after the mapping relationship is established can the second process perform read-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 perform a read operation on the first shared memory, the third process also needs to establish a mapping relationship between its own virtual address space and the first shared memory. Only after the mapping relationship is established can the third process perform a read operation on the first shared memory.

[0167] In the embodiment of the present application, the second process and the third process can establish a mapping relationship between their own virtual address space and the first shared memory through a memory mapping (mmap) mechanism. Among them, mmap is a method of memory mapping files, that is, mapping a file to the virtual address space of a process, and realizing a one-to-one mapping relationship between the file address and the virtual address space of the process. After realizing such a mapping relationship, the process can use a pointer to perform read and write operations on the virtual address space, thereby realizing read and write operations on the file.

[0168] Next, a process in which the second process and the third process establish a mapping relationship between their own virtual address spaces and the first shared memory through the mmap mechanism is described in detail.

[0169] Taking the second process establishing a mapping relationship between its own virtual address space and the first shared memory through the mmap mechanism as an example, the process may include:

[0170] In the first step, the second process calls the library function mmap in user space.

[0171] In the second step, the second process opens up the first mapping area in its own virtual address space.

[0172] The first mapping area is an area in the second process virtual address space that establishes a mapping relationship with the first shared memory.

[0173] In the third step, the second process allocates a virtual area structure for the first mapping area, and then initializes each domain of the virtual area structure.

[0174] Step 4: The second process inserts the virtual area structure into the linked list or tree of the first mapping area of ​​the second process.

[0175] Step 5: 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 file structure of the first shared file in the kernel "open file set" through the file descriptor of the first shared file.

[0176] Step 6: The second process links to the file organization (fileoperations) module through the file structure of the first shared file and calls the kernel function mmap.

[0177] Step 7: The second process locates the first shared file address through the virtual file system index (inode) module.

[0178] Step 8. The second process creates a page table through the mapping function (remap_pfn_range) from device memory to user space address.

[0179] Through the above eight steps, a mapping relationship between the first mapping area and the first shared file is established, that is, a mapping relationship between the first mapping area and the first shared memory is established.

[0180] It can be understood that the process of establishing the mapping relationship between the third process virtual address space 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 similar to the process of establishing the mapping relationship between the first mapping area and the first shared memory, and will not be elaborated here. Among them, the second mapping area is an area in the third process virtual address space that establishes a mapping relationship with the first shared memory. For the convenience of description, in the following embodiments of the present application, the first mapping relationship represents the mapping relationship between the first mapping area and the first shared memory, and the second mapping relationship represents the mapping relationship between the second mapping area and the first shared memory.

[0181] 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

[0182] 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, which can be understood as 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, which can be understood as the third process performs a read operation on the first shared memory and reads the first data from the first shared memory.

[0183] In a more specific embodiment, the second process may include a write thread for writing the first data in the first mapping area, and the third process may include a read thread for reading the first data from the second mapping area. The write thread is activated only when the second process has the first data to write, so as to notify the third process that the first data needs to be read. The read thread is activated only when the third process has the first data to read, so as to read the first data, and after the reading is completed, notify the second process that the reading of the first data is completed.

[0184] 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. Instead, there will be a delay. 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. If the content of the first shared file is to be synchronized with the content in the first shared memory, the refresh change (msync) function can be called to force synchronization. In this way, the first data written by the second process can be synchronously saved in the first shared file.

[0185] In a specific implementation, when the second process performs unidirectional communication with the third process through the first shared memory, it does not always release the first mapping relationship or the second mapping relationship after reading and writing a small amount of data, but always maintains 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 recovered by the second process. The first data is often recovered by the second process only after the second process completes the communication with the third process.

[0186] The second process reclaims the first data, indicating that the communication between the second process and the third process ends, the second process can release the first mapping relationship, and the third process can release the second mapping relationship. After the mapping relationship is released, the first process can release the resources occupied by the first shared memory.

[0187] In order to further improve the communication performance between the second process and the third process, in a specific implementation manner, the first shared memory also includes a disruptor queue, the second process writes the first data into the first shared memory, and the third process reads the first data from the first shared memory in the following specific process:

[0188] A1. The second process writes the first data into the first mapping area.

[0189] A2. The second process maps the first data from the first mapping area to the disruptor queue.

[0190] 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.

[0191] A3. The third process maps the first data from the disruptor queue to the second mapping area.

[0192] Specifically, the third process maps the first data from the disruptor queue to the second mapping area according to the second mapping relationship.

[0193] It can be seen that in this embodiment, the process of transmitting the first data from the second process to the third process is implemented by mapping, and there is no need to copy the first data, which can meet the requirement of zero data copying in certain inter-process communication scenarios.

[0194] In a possible implementation, the first shared memory includes a preset shared area in addition to the disruptor queue, and the preset shared area is used to store the first data. The specific process of the second process writing the first data into the first shared memory and the third process reading the first data from the first shared memory is:

[0195] B1. The second process writes the first data into the first mapping area.

[0196] B2. The second process maps the first data from the first mapping area to the preset shared area.

[0197] 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.

[0198] B2. The second process outputs an output stream including storage information of the first data from a preset shared area to a disruptor queue, wherein the storage information of the first data is the first address information and length information of the first data in the preset shared area, or the first address information and last address information of the first data in the preset shared area.

[0199] B3, the third process maps the storage information of the first data from the disruptor to the second mapping area,

[0200] B4. The third process reads the first data from the preset shared area according to the address information and length information of the first data.

[0201] Specifically, the third process maps the first data from the disruptor queue to the second mapping area according to the second mapping relationship.

[0202] 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, and a data copy is required, which can meet the requirement of performing a data copy in certain inter-process communication scenarios.

[0203] Next, the working principle of the disruptor queue is introduced.

[0204] like Figure 5 As shown, in Figure 5In the figure, thread A represents the writing thread in the second process with read-write permissions, and thread B and thread C represent the reading threads in the two processes with read-only permissions.

[0205] like Figure 5 As shown in the figure, in the disruptor queue, there is a ring buffer with many positions called slots, such as Figure 5 Slot 1, slot 2, slot 3... slot 10, slot 11, slot 12 in the ring buffer. Each data in the ring buffer is indexed by a sequence number. In the 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, which is always increasing. For example, slots 10 and 11 have been written with data, and the sequence number of the latest written data maintained by the ring buffer is 11. After the event processor receives the data A to be written by thread A, it can be learned from the ring buffer that the slot to be written next is 12, so 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 threads B and C that they can read the data with sequence number 12. After receiving the notification, threads B and C read data A according to the sequence number 12 of the received data A. If thread B and thread C do not receive the notification, thread B and thread C will enter a waiting state, waiting for data A to be written into the ring buffer by thread A. Once data A is written into the ring buffer, thread B and thread C will receive the notification, and then thread B and thread C can read data A according to the received sequence number 12.

[0206] It should be noted that thread B and thread C do not directly read data A in the ring buffer. Instead, they call the wait for method of the sequence barrier object based on the received sequence number 12, and pass the sequence number 12 that it needs to access to the sequence barrier object. The sequence barrier object obtains data A in slot 12 in the ring buffer based on sequence number 12 and then returns it to thread B and thread C.

[0207] It can be understood that threads B and C read data A only after receiving notification from the ring buffer. If thread A has not finished writing data A, thread B and thread C will not read data A, thus preventing thread B or thread C from performing read operations while thread A is performing write operations. At the same time, threads B and thread C read data A from the sequence barrier object based on the received sequence number 12. Threads B and thread C do not directly operate the ring buffer. Therefore, for the ring buffer, threads B and thread C will not have resource competition, and there is no need to lock threads B and thread C.

[0208] In specific applications, the number of slots in the ring buffer can be customized and set. 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.

[0209] The inter-process communication method provided by the embodiment of the present application can ensure that when the second process writes to the first shared memory, the third process cannot read the first shared memory by using the disruptor queue in the first shared memory, and also avoids the use of locks when multiple processes read the first shared memory. At the same time, the present application uses the disruptor queue in the first shared memory to support the repeated reading and writing of the process to the first shared memory, which can effectively improve the throughput of information between processes and further improve the performance of inter-process communication.

[0210] In addition, it can 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 shared memory includes a preset shared area in addition to the disruptor queue, the one-way communication process from the second process to the third process requires one data copy. The inter-process communication method provided by the embodiment of the present application is not only applicable to inter-process communication scenarios that require zero data copying, but also to inter-process communication scenarios that require one data copying, and has good flexibility.

[0211] In order to further improve the security and reliability of communication between the second process and the third process, in a specific implementation, before the second process writes the first data into the first mapping area, the second process needs to allocate a 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 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 a second heap memory, the second heap memory is used to store the control information of the third process, and then the third process obtains the control information of the third process 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.

[0212] The control information of the second process may include information that the process communicating with the second process is a third process, and the control information of the second process may also include information about the read-write permission of the first shared memory possessed by the second process, etc., which is not specifically limited 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 information about the read-only permission of the first shared memory possessed by the third process, etc., which is not specifically limited here.

[0213] In the above embodiment, by storing the control information of the second process in the first heap memory (heap memory of the first process) and storing the control information of the third process in the second heap memory (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 memories 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 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 conducive to the second process and the third process being independent and non-interfering with each other during operation. When a fault occurs in one of the second process and the third process, the fault will not spread to the other process.

[0214] In actual applications, the second process and the third process can call a memory allocation (malloc) function or a create (new) function to allocate the corresponding first heap memory and second heap memory. The second process can determine the size of the allocated first heap memory, and the third process can determine the size of the allocated second heap memory. The second process and the third process can also release the corresponding first heap memory and second heap memory through a free function or a delete function.

[0215] It should be noted that the heap memory allocated by a 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. Figure 6 As shown, from Figure 6 It can be seen that the second process virtual address space includes heap memory, stack memory and shared memory mapping area (first mapping area). Figure 6 It can be seen that the first mapping area is located between the heap memory (first heap memory) of the second process and the stack memory of the second process. In other words, the first heap memory and the first mapping area are two different memory blocks in the virtual address space of the second process.

[0216] The first mapping area stores the shared memory information of the second process, and the shared memory information of the second process may include the first mapping relationship or the first data written by the second process to the first mapping area, etc., which is not specifically limited here. The second mapping area stores the shared memory information of the third process, and the shared memory information of the third process may include the second mapping relationship or the first data read from the first shared memory, etc., which is not specifically limited here.

[0217] In the above embodiment, by storing the control information of the second process in the first heap memory, storing the shared memory information of the second process in the first mapping area, and the first heap memory and the first mapping area are different memories, the control information of the second process and the shared memory information of the second process can be stored separately, which is conducive to ensuring the security of the shared memory information of the second process. Similarly, by storing the control information of the third process in the second heap memory, and storing the shared memory information of the third process in the second mapping area, the second heap memory and the second mapping area are different memories, the control information of the third process and the shared memory information of the third process can be stored separately, which is conducive to ensuring the security of the shared memory information of the third process.

[0218] 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, the third heap memory is used to store the control information of the second process, and then the second process sends a first information acquisition request to the first process, requesting to obtain the control information of the second process from the third heap memory, and after the second process obtains the control information of the second process returned by the first process, the second process can 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 a second heap memory, the second heap memory is used to store the control information of the third process, and then the third process obtains 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 area according to the control information of the third process.

[0219] 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 to the first process to obtain the control information, 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. In other words, the method of storing the control information of the process in the heap memory of the process itself is suitable for processes with high performance requirements; the method of storing the control information of the process in the heap memory of the first process is suitable for processes with lower performance requirements. It can be seen that the above method is not only suitable for processes with high communication performance requirements, but also 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 information of the third process can also be stored separately, which is conducive to the second process and the third process being 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.

[0220] 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 memories. In addition, it can be known from the above embodiment that the second heap memory and the second mapping area are two different memories in the virtual address space of the third process.

[0221] In the above scheme, 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 stored separately, which is conducive to ensuring the security 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 conducive to ensuring the security of the shared memory information of the third process.

[0222] In another specific implementation, before the second process writes the first data into the first mapping area, the second process needs to allocate a 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 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, the third heap memory is used to store the control information of the third process, and then the third process sends a second information acquisition request to the first process, 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 map the first data from the first shared memory to the second mapping area according to the control information of the third process.

[0223] 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 memories. In addition, it can also be known that the first heap memory and the first mapping area are two different memories in the virtual address space of the second process.

[0224] 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, so that the control information of the second process and the shared memory information of the second process can be stored separately, which is conducive to ensuring the security 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 conducive to ensuring the security of the shared memory information of the third process.

[0225] It can also be seen from the above embodiments 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. In other words, 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, the shared memory information of the third process and the first shared memory information, which is conducive to ensuring the security of the first shared memory information.

[0226] Among them, the first shared memory can store first shared memory information, and the first shared memory information can include a mapping relationship between the first shared memory and the first mapping area, a mapping relationship between the first shared memory and the second mapping area, or first data written into the first shared memory by the second process, etc., which is not specifically limited here.

[0227] In order to further ensure the reliability of 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 to detect the status of the second process and the third process in real time when the second process communicates with the third process through the first shared memory. 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 have not been reset so that the communication connection can be terminated in time and the occupied first shared memory resources can be released in time. Among them, process reset means that the state of the process returns to the initial state, and the process waits to be executed again.

[0228] 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 and release the first shared memory resources in time.

[0229] In order to further ensure the reliability of communication between the second process and the third process, as an embodiment of the present application, the first process also includes a recovery shared memory thread, wherein the recovery shared memory thread is the thread with 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 first thread running in the first process is the recovery shared memory thread. When other threads included in the first process, such as the detection thread, are running, the running of the detection thread can be delayed or paused to give resources to the recovery shared memory thread. The recovery shared memory thread can recover the first shared memory at the fastest speed.

[0230] 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 of the second process or the third process being reset, the communication connection between the second process and the third process is terminated in time to release the resources occupied by the first shared memory. At the same time, the first process can also set the running level of the shared memory recovery thread to the highest priority, so as to quickly restore the first shared memory when the first process needs to be restarted.

[0231] It can also be seen from the above embodiments that the service mode adopted by the inter-process communication method provided in the embodiments of the present application is also a C / S mode. In which, the first process acts as a server process and provides a first shared memory for the second process and the third process, and the second process and the third process act as client processes and use the first shared memory provided by the first process to communicate.

[0232] The inter-process communication method provided by the embodiment of the present application creates a first shared memory between the second process and the third process through the first process, 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process. Among them, 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 is not necessary to fall into the kernel space through the system call kernel to realize it, and it is not necessary to copy data, the system overhead is small, and the communication efficiency is high. Moreover, when the inter-process communication method provided by the present application is applied to embedded devices with diverse 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 problem of single board and operating system adaptation, and the operation is simple and the portability is strong. At the same time, in addition to the first process, only the second process has the read and write permissions of the first shared memory, which can avoid multiple processes from writing to the first shared memory at the same time, causing data confusion in the first shared memory. In addition, the second process and the third process are both processes registered with the first process. The first process can verify the identities of the second process and the third process, and only processes with read and write permissions or read-only permissions to the first shared memory can access the first shared memory, which can prevent malicious programs from tampering with or destroying data in the first shared memory, and can effectively improve the security and reliability of inter-process communication.

[0233] The above embodiment describes in detail the implementation process of the one-way communication from the second process to the third process. Next, the process of implementing the one-way communication from the third process to the second process by the inter-process communication method provided by the present application is described.

[0234] See also Figure 7 ,like Figure 7 As shown, the method includes:

[0235] S201: A first process creates a second shared memory between a second process and a third process.

[0236] The first process has read and write permissions for the second shared memory, the third process has read and write permissions for the second shared memory, the second process has read-only permissions for the second shared memory, a third mapping relationship exists between the third mapping area and the second shared memory, a fourth mapping relationship exists between the fourth mapping area and the third 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 area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

[0237] Among them, the process of the first process creating the second shared memory between the second process and the third process is similar to the process of the first process creating the first shared memory between the second process and the third process described in S01. The process of the first process configuring the read-only permission of the second shared memory for the second process and the process of configuring the read-write permission of the second shared memory for the third process are similar to the process of the first process configuring the read-write permission of the first shared memory for the second process and the process of configuring the read-only permission of the first shared memory for the third process in S101. For details, please refer to the relevant description in S101, which will not be elaborated here. The third process establishes a mapping relationship between the third mapping area and the second shared memory, and the second process establishes a mapping relationship between the fourth mapping area and the second shared memory, which is similar to the process of the second process establishing a mapping relationship between the first mapping area and the first shared memory in S101, which will not be elaborated here.

[0238] 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 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.

[0239] 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, which can be understood as the third process performs a write operation on the second shared memory and writes the second data into the second shared memory. The second process maps the second data from the second shared memory to the fourth mapping area according to the mapping relationship between the fourth mapping area and the second shared memory, which can be understood as the second process performs a read operation on the second shared memory and reads the second data from the second shared memory.

[0240] The inter-process communication method provided by the embodiment of the present application creates a second shared memory between the second process and the third process through the first 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, and the third process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship. Thus, the one-way communication from the third process to the second process is realized. Among them, the process of the third process transmitting the second data to the second process is realized by mapping the third mapping relationship and the fourth mapping relationship, which can be realized without falling into the kernel space through the system call kernel, without data copying, low system overhead, and high communication efficiency. Moreover, when applied to embedded devices with diverse 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 single board and operating system adaptation, simple operation, and strong portability. At the same time, in addition to the first process, only the third process has the read and write permissions of the second shared memory, which can avoid multiple processes from writing to the second shared memory at the same time, causing data confusion in the second shared memory. In addition, the second process and the third process are both processes registered with the first process. The first process can verify the identities of the second process and the third process, and only processes with read and write permissions or read-only permissions to the second shared memory can access the second shared memory, which can prevent malicious programs from tampering with or destroying data in the second shared memory, and can effectively improve the security and reliability of inter-process communication.

[0241] It can be seen from the above embodiment that one-way communication from the second process to the third process is realized through S101 and S102, and one-way communication from the third process to the second process is realized through S201 and S202. It can be understood that two-way communication between the second process and the third process can be realized through S101, S102, S201 and S202, and the process of realizing two-way communication between the second process and the third process will not be described in detail here.

[0242] To facilitate a better understanding and implementation of the above-mentioned solution of the embodiment of the present application, a more specific inter-process communication scenario is described below.

[0243] See also Figure 8 , Figure 8 The following is a schematic diagram of an interaction method of an inter-process communication method provided in an embodiment of the present application. Figure 8 As shown, the inter-process communication method may include:

[0244] S301: The second process sends a registration request to the first process.

[0245] The registration request 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 register the second process according to the information such as the UID or PID of the second process.

[0246] S302: The first process receives a 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 for the first shared memory.

[0247] Among them, the first process can create a first shared memory according to the UID or PID of the second process and other information. The first shared memory can have a shared memory identifier, and the first shared memory identifier can be named according to the physical address of the first shared memory or the UID or PID of the second process and other information.

[0248] S303: The first process sends the registration information to the second process.

[0249] 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.

[0250] 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.

[0251] 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 a third process, wherein the third process has been registered in the first process.

[0252] S306: The first process receives the connection request, determines the third process according to the connection request, and configures the third process to have read-only permission for the first shared memory.

[0253] The connection request sent by the second process may include information such as the UID or PID of the third process, so that the first process can search for the third process according to the identification information of the third process included in the connection request.

[0254] S307: The first process sends a connection request to the third process.

[0255] 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.

[0256] 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.

[0257] S309: The third process returns a connection response to the first process.

[0258] S310: The first process receives a connection response sent by the third process, and forwards the connection response to the second process.

[0259] S311, the second process receives a connection response, obtains control information of the second process from the first heap memory, and writes the first data into a disruptor queue in the first shared memory through the first mapping area.

[0260] S312: The third process obtains control information of the third process from the second heap memory, and reads first data from the disruptor queue in the first shared memory through the second mapping area.

[0261] The above describes in detail an inter-process communication method of an embodiment of the present application. Based on the same inventive concept, the following provides an inter-process communication device of an embodiment of the present application. The inter-process communication device provided by the present application is applied to various terminal devices that need to perform inter-process communication, including but not limited to electronic devices such as smart phones, tablet computers, and laptop portable computers. The terminal device can be an electronic device built based on an operating system such as Android, Linux, or Windows, or can be built based on other systems as required, which is not specifically limited here.

[0262] See also Fig. 9 , Fig. 9 is a schematic diagram of the structure of an inter-process communication device 100 provided in an embodiment of the present application, such as Fig. 9 As shown, the inter-process communication device 100 provided in the embodiment of the present application at least includes: a creation module 110 and a communication module 120, wherein:

[0263] A creation module 110 is used to create a first shared memory between a second process and a third process through a first process, wherein the first process, the second process and the third process run in a 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, 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, a first mapping relationship exists between a first mapping area and the first shared memory, a second mapping relationship exists between a second mapping area and the first shared memory, the first mapping area is an area in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is an area in a virtual address space of the third process that establishes a mapping relationship with the first shared memory.

[0264] The communication module 120 is used to write the first data into the first mapping area through the second process, and map the first data from the first mapping area to the first shared memory according to the first mapping relationship, and also map the first data from the first shared memory to the second mapping area through the third process according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

[0265] In the embodiment of the present application, the creation module 110 is further used to:

[0266] The creation module 110 configures the second process to have read and write permissions of the first shared memory through the first process, and configures the third process to have read-only permissions of the first shared memory.

[0267] In the embodiment of the present application, the creation module 110 is specifically used for:

[0268] The creation module 110 configures the second process to have read and write permissions of the first shared memory through the access control list ACL through the first process, and configures the third process to have read-only permissions of the first shared memory.

[0269] In the 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;

[0270] The allocation module 130 is used to allocate a first heap memory through a second process, where the first heap memory is used to store control information of the second process.

[0271] The acquisition module 140 is used to acquire the control information of the second process from the first heap memory through 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.

[0272] 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 through the third process, where the second heap memory is used to store control information of the third process.

[0273] The acquisition module 140 is further configured to acquire 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 mapping area according to the control information of the third process.

[0274] In an 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 shared memory information of the second process, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store shared memory information of the third process.

[0275] In the 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;

[0276] The allocation module 130 is used 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.

[0277] The acquisition module 140 is used to receive a first acquisition information request sent by the second process through the first process, wherein the first acquisition information request is used to request to obtain control information of the second process from the third heap memory.

[0278] The sending module 160 is used to send the control information of the second process to the second process according to the first information acquisition request through the first process, so that the second process writes the first data into the first mapping area according to the control information of the second process.

[0279] 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 through the third process, where the second heap memory is used to store control information of the third process.

[0280] The acquisition module 140 is further configured to acquire 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 mapping area according to the control information of the third process.

[0281] In an 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 shared memory information of the second process, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store shared memory information of the third process.

[0282] In the embodiment of the present application, before the second process writes the first data into the first mapping area, the allocation module 130 is used to allocate a first heap memory through the second process, and the first heap memory is used to store control information of the second process.

[0283] The acquisition module 140 is used to acquire the control information of the second process from the first heap memory through 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.

[0284] 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;

[0285] The allocation module 130 is used 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.

[0286] The receiving module 150 is used to receive a second information acquisition request sent by a third process through the first process, wherein the second information acquisition request is used to request to acquire control information of the third process from a third heap memory.

[0287] The sending module 160 is used to send the control information of the third process to the third process through the first 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.

[0288] In an 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 shared memory information of the second process, the third heap memory and the second mapping area are different memories, and the second mapping area is used to store shared memory information of the third process.

[0289] In the embodiment of the present application, the first process includes a detection thread, and the device further includes a detection module 170, which is specifically used to:

[0290] The detection module 170 detects whether the second process or the third process is reset through 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 has not been reset to terminate communication and release the first shared memory.

[0291] In the embodiment of the present application, the first process includes restoring a shared memory thread, and the restored shared memory thread is a thread with the highest running level among the threads included in the first process. The device also includes a recovery module 180, and the recovery module 180 is specifically used to:

[0292] When the first process is restarted, the recovery module 180 recovers the first shared memory by recovering the shared memory thread.

[0293] In the embodiment of the present application, the first shared memory includes a destroyer queue, and the communication module 120 is specifically used for:

[0294] 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.

[0295] In an embodiment of the present application, the creation module 110 is further used 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, 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, a third mapping area has a third mapping relationship with the second shared memory, a fourth mapping area has a fourth mapping relationship with 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 area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory;

[0296] The communication module 120 is also used 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 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.

[0297] Specifically, the specific implementation of various operations performed by the above-mentioned inter-process communication device 100 can refer to the description in the relevant content of the above-mentioned method embodiment, and for the sake of brevity of the specification, it will not be repeated here.

[0298] It should be understood that the inter-process communication device 100 is only an example provided in the embodiment of the present application, and the inter-process communication device 100 may have more Fig. 9 The components shown may be more or less than the components, two or more components may be combined, or may be implemented with a different configuration of components.

[0299] The present application embodiment also provides another inter-process communication device, see Fig.10 , Fig.10 The present application provides a schematic diagram of the structure of an inter-process communication device 200, which 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 via a bus 240.

[0300] The processor 210 may include one or more general-purpose processors, wherein the general-purpose processor may be any type of device capable of processing electronic instructions, including a CPU, a microprocessor, a microcontroller, a main processor, a controller, and an application specific integrated circuit (ASIC), etc. 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 device 100 in the above embodiment of the present application.

[0301] The communication interface 230 may be a wired interface (e.g., an Ethernet interface) or a wireless interface (e.g., a cellular network interface or 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 the transmission control protocol / internet protocol (TCP / IP), such as a remote function call (RFC) protocol, a simple object access protocol (SOAP) protocol, a simple network management protocol (SNMP) protocol, a common object request broker architecture (CORBA) protocol, and a distributed protocol, etc.

[0302] The memory 220 may store program codes and program data. The program codes include: codes of the creation module 110, codes of the communication module 120, etc., and the program data include: first data, second data, control information of the second process, and control information of the third process, etc. In practical applications, the memory 220 may include volatile memory, such as random access memory (RAM); the memory may also include non-volatile memory, such as read-only memory (ROM), flash memory, hard disk drive (HDD) or solid-state drive (SSD). The memory may also include a combination of the above-mentioned types of memory.

[0303] The bus 240 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus 240 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Fig.10 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0304] It should be understood that the inter-process communication device 200 is only an example provided in the embodiment of the present application, and the inter-process communication device 200 may have more Fig.10 The components shown may be more or less than the components, two or more components may be combined, or may be implemented with a different configuration of components.

[0305] The embodiment of the present application also provides a computer storage medium, in which instructions are stored. When the computer storage medium is executed on a processor, the method steps in the above method embodiment can be implemented. The specific implementation of the processor of the computer storage medium in executing the above method steps can refer to the specific operations of the above method embodiment, which will not be repeated here.

[0306] The embodiment of the present application also provides a computer program product, which, when read and executed by a computer, implements part or all of the steps of the inter-process communication method recorded in the above method embodiment.

[0307] In the above embodiments, it can be implemented in whole or in part by software, hardware or any combination thereof. When implemented using 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, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (digital subscriber line, DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a digital versatile disc (digital video disc, DVD)), or a semiconductor medium (e.g., an SSD), etc.

[0308] The steps in the method of the embodiment of the present application can be adjusted in order, combined or deleted according to actual needs; the modules in the device of the embodiment of the present application can be divided, combined or deleted according to actual needs.

[0309] The embodiments of the present application are introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of ​​the present application. At the same time, for general technical personnel in this field, according to the idea of ​​the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.

Claims

1. An inter-process communication method, characterized in that: The method comprises: 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 a 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, 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, a first mapping area has a first mapping relationship with the first shared memory, a second mapping area has a second mapping relationship with the first shared memory, the first mapping area is an area in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is an area in a virtual address space of the third process that establishes a mapping relationship with the first shared memory; 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 from the first shared memory to the second mapping area according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

2. The method according to claim 1, characterized in that: After the first process creates a 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 for the first shared memory, and configures the third process to have read-only permissions for the first shared memory.

3. The method according to claim 2, characterized in that The first process configures the second process to have read and write permissions for the first shared memory, and configures the third process to have read-only permissions for the first shared memory, including: The first process configures the second process to have read and write permissions for the first shared memory through an access control list (ACL), and configures the third process to have read-only permissions for the first shared memory.

4. The method according to any one of claims 1 to 3, characterized in that: 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, where the first heap memory is used to store control information of the second process; The second process obtains 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 method further includes: The third process allocates a second heap memory, where the second heap memory is used to store control information of the third process; The third process obtains 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.

5. The method according to claim 4, characterized in that 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, 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.

6. The method according to any one of claims 1 to 3, characterized in that: 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 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 area, the method further includes: The third process allocates a second heap memory, where the second heap memory is used to store control information of the third process; The third process obtains 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.

7. The method according to claim 6, characterized in that 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

8. The method according to any one of claims 1 to 3, characterized in that: 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, where the first heap memory is used to store control information of the second process; The second process obtains 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 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, wherein 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 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 according to claim 8, characterized in that The first heap memory and the first mapping area are different memories, and the first mapping area is used to store the shared memory information of the second process. The third heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

10. The method according to claim 1, characterized in that 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; When it is detected that the second process or the third process is reset, the detection thread notifies the second process or the third process that is not reset to terminate communication and release the first shared memory.

11. The method according to claim 1, characterized in that: The first process includes a restored shared memory thread, and the restored shared memory thread is a thread with the highest running level among the threads included in the first process. The method further includes: When the first process is restarted, the shared memory recovery thread recovers the first shared memory.

12. The method according to claim 1, characterized in that 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 destroyer queue to the second mapping area.

13. The method according to claim 1, characterized in that The method further comprises: 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 for the second shared memory, the third process has read and write permissions for the second shared memory, the second process has read-only permissions for the second shared memory, a third mapping area has a third mapping relationship with the second shared memory, a fourth mapping area has a fourth mapping relationship with 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 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, 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 inter-process communication device, characterized in that: include: A creation module, used for creating a first shared memory between a second process and a third process through a first process, wherein the first process, the second process and the third process run in a 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, 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, a first mapping area has a first mapping relationship with the first shared memory, a second mapping area has a second mapping relationship with the first shared memory, the first mapping area is an area in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping area is an area in a virtual address space of the third process that establishes a mapping relationship with the first shared memory; A communication module is used to write the first data into the first mapping area through the second process, and map the first data from the first mapping area 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 area through the third process according to the second mapping relationship, thereby realizing one-way communication from the second process to the third process.

15. The device according to claim 14, characterized in that After the first process creates the first shared memory between the second process and the third process, the creation module is further used to: The creation module configures the second process to have read and write permissions for the first shared memory through the first process, and configures the third process to have read-only permissions for the first shared memory.

16. The device according to claim 15, characterized in that The creation module is specifically used for: The creation module configures the second process to have read and write permissions of the first shared memory through an access control list (ACL) through the first process, and configures the third process to have read-only permissions of the first shared memory.

17. The device according to any one of claims 14 to 16, characterized in that Before the second process writes the first data into the first mapping area, the device further includes: an allocation module and an acquisition module; The allocation module is used to allocate a first heap memory through the second process, and the first heap memory is used to store control information of the second process; The acquisition module is configured to acquire control information of the second process from the first heap memory through 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 area, the allocation module is further used 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 acquisition module is further used to acquire 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 mapping area according to the control information of the third process.

18. The device according to claim 17, characterized in that 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, 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.

19. The device according to any one of claims 14 to 16, characterized in that Before the second process writes the first data into the first mapping area, the device further includes a sending module; an allocation module, configured to allocate a third heap memory through the first process, wherein the third heap memory is used to store control information of the second process; an acquisition module, configured to receive, through the first process, a first acquisition information request sent by the second process, wherein the first acquisition information request is used to request to acquire control information of the second process from the third heap memory; The sending module is used to send the control information of the second process to the second process according to the first information acquisition request through the first 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 area, the allocation module is further used 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 acquisition module is further used to acquire 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 mapping area according to the control information of the third process.

20. The device according to claim 19, characterized in that 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, the second heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

21. The device according to any one of claims 14 to 16, characterized in that Before the second process writes the first data into the first mapping area, an allocation module is used 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; an acquisition module, configured to acquire control information of the second process from the first heap memory through 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 area, the device further includes a receiving module; The allocation module is used to allocate a third heap memory through the first process, and 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 information acquisition request sent by the third process, wherein the second information acquisition request is used to request to acquire control information of the third process from the third heap memory; A sending module is used to send the control information of the third process to the third process through the first 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.

22. The device according to claim 21, characterized in that The first heap memory and the first mapping area are different memories, and the first mapping area is used to store the shared memory information of the second process. The third heap memory and the second mapping area are different memories, and the second mapping area is used to store the shared memory information of the third process.

23. The device according to claim 14, characterized in that The first process includes a detection thread, and the device also includes a detection module, and the detection module is specifically used 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, it notifies the second process or the third process that is not reset to terminate communication through the detection thread and releases the first shared memory.

24. The device according to claim 14, characterized in that The first process includes a restored shared memory thread, where the restored shared memory thread is a thread with the highest running level among threads included in the first process. The device also includes a restoration module, where the restoration module is specifically used to: When the first process is restarted, the recovery module recovers the first shared memory through the recovery shared memory thread.

25. The device according to claim 14, characterized in that The first shared memory includes a destroyer queue, and the communication module is specifically used for: 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 destroyer queue to the second mapping area through the third process.

26. The device according to claim 14, characterized in that The creation module is further used 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, 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, a third mapping area has a third mapping relationship with the second shared memory, a fourth mapping area has a fourth mapping relationship with 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 area is an area in the virtual address space of the second process that establishes a mapping relationship with the second shared memory; The communication module is also used 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 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.

27. A computer storage medium, characterized in that The computer storage medium stores a computer program for inter-process communication, and when the computer program is executed by a processor, the method according to any one of claims 1 to 13 is implemented.

Citation Information

Patent Citations

  • Method and system for communication among multiple kernels

    CN104123265A

  • Inter-process communication method and device

    CN104572313A