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

Inter-process communication method and related equipment Download PDF

Info

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

Links

Images

Classifications

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

Landscapes

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

Abstract

The application provides an interprocess communication method and related equipment, wherein the method comprises the following steps: the first process creates a first shared memory between the second process and the third process; the second process writes first data into the first mapping area, maps the first data from the first mapping area to the first shared memory according to the first mapping relation, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relation, so that one-way communication from the second process to the third process is realized. The method can not only improve the safety and reliability of the interprocess communication, but also effectively improve the performance of the interprocess communication.

Description

Inter-process communication method and related equipment
Technical Field
The present application relates to the field of communications technologies, and in particular, to an inter-process communications (IPC) method and a related device.
Background
IPC is a communication method for sharing messages between one process and another process, and is mainly used for message passing between different processes in a computer system. The process is the minimum unit of computer resource allocation, each process has its own virtual address space and is isolated from the virtual address spaces of other processes, that is, one process can only access its own virtual address space and cannot access the virtual address spaces of other processes. To enable different processes to access and work in coordination with each other, the operating system provides a variety of IPC mechanisms.
However, the existing IPC mechanism has the problems of low performance or low security.
Disclosure of Invention
The embodiment of the application discloses an interprocess communication method and related equipment, which not only can improve the safety and reliability of interprocess communication, but also can effectively improve the performance of interprocess communication.
In a first aspect, the present application provides a method for interprocess communication, where the method includes:
a first process creates a first shared memory between a second process and a third process, wherein the first process, the second process and the third process run in user space, the first process has read-write permission to the first shared memory, the second process has read-write permission to the first shared memory, the third process has read-only permission for the first shared memory, the second process and the third process are both registered in the first process, a first mapping relation is formed between a first mapping area and the first shared memory, a second mapping relation is formed between a second mapping area and the first shared memory, the first mapping region is a region in the second process virtual address space that establishes a mapping relationship with the first shared memory, the second mapping area is an area in the third process virtual address space, which establishes a mapping relation with the first shared memory;
the second process writes first data into the first mapping area, maps the first data from the first mapping area to the first shared memory according to the first mapping relation, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relation, so that one-way communication from the second process to the third process is realized.
According to the inter-process communication method, the first shared memory between the second process and the third process is established through the first process, the second process writes the first data into the first mapping area, the first data are mapped to the first shared memory from the first mapping area according to the first mapping relation, the third process maps the first data to the second mapping area from the first shared memory according to the second mapping relation, and therefore one-way communication from the second process to the third process is achieved. The process of transmitting the first data to the third process by the second process is realized by mapping through the first mapping relation and the second mapping relation, can be realized without trapping a system call kernel into a kernel space, does not need to copy data, and has low system overhead and high communication efficiency. Moreover, when the inter-process communication method provided by the application is applied to embedded equipment with various operating systems, because a kernel state is not introduced in the one-way communication process from the second process to the third process, the problem of adaptation between a single board and the operating system does not need to be considered, and the inter-process communication method is simple to operate and strong in portability. Meanwhile, except for the first process, only the second process has the read-write permission of the first shared memory, so that the situation that a plurality of processes write the first shared memory at the same time to cause data chaos in the first shared memory can be avoided. In addition, the second process and the third process are processes registered in the first process, the first process can check the identities of the second process and the third process, and only the process with the read-write permission or the read-only permission of the first shared memory can access the first shared memory, so that malicious programs can be prevented from tampering or damaging data in the first shared memory, and the safety and reliability of inter-process communication can be effectively improved.
In a specific implementation manner, after the first process creates the first shared memory between the second process and the third process, the method further includes:
and the first process configures the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory.
In the above scheme, the first process configures the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory, so that it is ensured that the process with the read-write permission of the first shared memory can perform write operation on the first shared memory, and the process with the read-only permission of the first shared memory can perform read operation on the first shared memory, thereby avoiding malicious programs from performing read-write operation on the first shared memory, and ensuring that data in the first shared memory cannot be modified or even damaged by malicious programs.
In a specific implementation manner, the configuring, by the first process, that the second process has the read-write permission of the first shared memory, and configuring, by the first process, that the third process has the read-only permission of the first shared memory includes:
and the first process configures the second process with the read-write permission of the first shared memory through an Access Control List (ACL), and configures the third process with the read-only permission of the first shared memory.
In a specific implementation manner, before the second process writes the first data into the first mapping region, the method further includes:
the second process allocates a first bank memory, and the first bank memory is used for storing the control information of the second process;
the second process acquires the control information of the second process from the first stack memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the third process allocates a second heap memory, and the second heap memory is used for storing control information of the third process;
the third process acquires the control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping region according to the control information of the third process.
In the above scheme, the control information of the second process is stored in the first heap memory (heap memory of the first process), and the control information of the third process is stored in the second heap memory (heap memory of the third process), so that when the second process communicates with the third process, the second process and the third process can directly obtain their control information from their heap memories for communication, and the efficiency of communication between processes is high. Meanwhile, the control information of the second process is stored in the first heap memory, and the control information of the third process is stored in the second heap memory, so that the control information of the second process and the control information of the third process can be separately stored, the second process and the third process are independent and do not interfere with each other during operation, and when one of the second process and the third process breaks down, the fault cannot be diffused to the other process.
In a specific implementation manner, the first heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
In the above scheme, the control information of the second process is stored in the first heap memory, the shared memory information of the second process is stored in the first mapping region, and the first heap memory and the first mapping region are different memories, so that the control information of the second process and the shared memory information of the second process can be separately stored, and the security of the shared memory information of the second process can be ensured. Similarly, the control information of the third process is stored in the second heap memory, the shared memory information of the third process is stored in the second mapping area, and the second heap memory and the second mapping area are different memories, so that the control information of the third process and the shared memory information of the third process can be separately stored, and the safety of the shared memory information of the third process can be ensured.
In a specific implementation manner, before the second process writes the first data into the first mapping region, the method further includes:
the first process allocates a third heap memory, and the third heap memory is used for storing control information of the second process;
the first process receives a first information acquisition request sent by the second process, wherein the first information acquisition request is used for requesting to acquire control information of the second process from the third heap memory;
the first process sends the control information of the second process to the second process according to the first information acquisition request, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the third process allocates a second heap memory, and the second heap memory is used for storing control information of the third process;
the third process acquires the control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping region according to the control information of the third process.
In the above scheme, the control information of the second process is stored in the third heap memory (the heap memory of the first process), and the control information of the third process is stored in the heap memory of the third process, when the second process communicates with the third process, the second process needs to send a request for obtaining the control information to the first process first, and then obtains 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, and it can be seen that the communication efficiency of the second process is lower than that of the third process. That is, the method of storing the control information of the process in the heap memory of the process is suitable for the process with high performance requirement; the method for storing the control information of the process in the heap memory of the first process is suitable for the processes with lower performance requirements. Therefore, the method is not only suitable for the process with high communication performance requirement, but also suitable for the process with low communication performance requirement, and has high flexibility. Meanwhile, the control information of the second process is stored in the heap memory of the first process, and the control information of the third process is stored in the heap memory of the third process, so that the control information of the second process and the control information of the third process can be separately stored, the second process and the third process are independent and do not interfere with each other during operation, and when one of the second process and the third process fails, the failure cannot be diffused to the other process.
In a specific implementation manner, the third heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
In the above scheme, the control information of the second process is stored in the third heap memory (storing the shared information of the second process in the first mapping region can realize separate storage of the control information of the second process and the shared memory information of the second process, which is beneficial to ensuring the security of the shared memory information of the second process.
In a specific implementation manner, before the second process writes the first data into the first mapping region, the method further includes:
the second process allocates a first bank memory, and the first bank memory is used for storing the control information of the second process;
the second process acquires the control information of the second process from the first stack memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the method further includes:
the first process allocates a third heap memory, and the third heap memory is used for storing control information of the third process;
the first process receives a second information acquisition request sent by the third process, wherein the second information acquisition request is used for requesting to acquire control information of the third process from the third heap memory;
and the first process sends the control information of the third process to the third process according to the second information acquisition request, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In a specific implementation manner, the first heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the third heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
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 region, so that the control information of the second process and the shared memory information of the second process can be stored separately, and the security of the shared memory information of the second process is favorably ensured. Meanwhile, 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, and the security of the shared memory information of the third process is guaranteed.
In a specific implementation manner, 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 or not;
and under the condition that the second process or the third process is detected to be reset, the detection thread informs the second process and the third process that the reset process does not occur to terminate communication, and releases the first shared memory.
In the above scheme, the first process may further detect whether the second process or the third process is reset, and when it is detected that the second process or the third process is reset, the communication between the second process and the third process may be terminated in time, and the resource occupied by the first shared memory is released, which is beneficial to the timely recovery of the resource.
In a specific implementation manner, the first process includes recovering a shared memory thread, where the recovering the shared memory thread is a thread with a highest running level in threads included in the first process, and the method further includes:
and when the first process is restarted, the shared memory recovery thread recovers the first shared memory.
In the above scheme, 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, so that the reliability of inter-process communication is further improved.
In a specific implementation manner, the first shared memory includes a destroyer queue, and the mapping, by the second process, the first data from the first mapping region to the first shared memory includes:
the second process mapping the first data from the first mapping region to the destroyer queue;
the third process maps the first data from the first shared memory to the second mapping region, including:
the third process maps the first data from the destroyer queue to the second mapping region.
In the above scheme, the first shared memory includes a destroyer queue, which supports repeated read-write of the first shared memory, and can effectively improve throughput of information between processes, and further improve performance of communication between processes.
In a specific implementation, the method further includes:
the first process creates a second shared memory between the second process and the third process, wherein the first process has read-write permission for the second shared memory, the third process has read-write permission for the second shared memory, the second process has read-only permission for the second shared memory, a third mapping region has a third mapping relationship with the second shared memory, a fourth mapping relationship has a fourth mapping relationship with the second shared memory, the third mapping region is a region in the virtual address space of the third process establishing a mapping relationship with the second shared memory, and the fourth mapping region is a region in the virtual address space of the second process establishing a mapping relationship with the second shared memory;
the third process writes the second data into the third mapping area, and maps the second data from the third mapping area to the second shared memory according to the third mapping relationship, and the second process maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship, thereby realizing one-way communication from the third process to the second process.
In the above scheme, a second shared memory between a second process and a third process is created through a first process, the third process writes second data into a third mapping region, and maps the second data from the third mapping region to the second shared memory according to a third mapping relationship, and the third process maps the second data from the second shared memory to a fourth mapping region according to a fourth mapping relationship. Thereby realizing the one-way communication from the third process to the second process. The process of transmitting the second data from the third process to the second process is realized by mapping through the third mapping relation and the fourth mapping relation, the process can be realized without trapping a system call kernel into a kernel space, data copying is not needed, the system overhead is small, and the communication efficiency is high. Moreover, when the method is applied to embedded equipment with various operating systems, as the kernel state is not introduced in the one-way communication process from the third process to the second process, the problem of adaptation of the single board and the operating system does not need to be considered, the operation is simple, and the portability is strong. Meanwhile, except for the first process, only the third process has the read-write permission of the second shared memory, so that the situation that a plurality of processes write the second shared memory at the same time, or malicious programs tamper with or damage data of the second shared memory to cause data confusion in the second shared memory can be avoided. In addition, the second process and the third process are processes registered in the first process, the first process can verify the identities of the second process and the third process, and only the process with the read-write permission or the read-only permission of the second shared memory can access the second shared memory, so that the safety and reliability of the inter-process communication can be effectively improved.
In a second aspect, the present application provides an inter-process communication apparatus, the apparatus comprising:
a creating module, configured to create a first shared memory between a second process and a third process through a first process, where the first process, the second process, and the third process operate in a user space, the first process has a read-write permission for the first shared memory, the second process has a read-write permission for the first shared memory, the third process has a read-only permission for the first shared memory, the second process and the third process are both registered in the first process, a first mapping relationship is provided between a first mapping region and the first shared memory, a second mapping relationship is provided between a second mapping region and the first shared memory, the first mapping region is a region in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping region is a region in a virtual address space of the third process that establishes a mapping relationship with the first shared memory An area;
a communication module, configured to write first data into the first mapping region through the second process, map the first data from the first mapping region to the first shared memory according to the first mapping relationship, and map the first data from the first shared memory to the second mapping region through the third process according to the second mapping relationship, so as to implement unidirectional communication from the second process to the third process.
In a specific implementation manner, the creating module is further configured to:
the creating module configures, through the first process, the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory.
In a specific implementation manner, the creating module is specifically configured to:
and the creating module configures the second process to have the read-write permission of the first shared memory through the first process by an Access Control List (ACL), and configures the third process to have the read-only permission of the first shared memory.
In a specific implementation manner, before the second process writes the first data into the first mapping region, the apparatus further includes: a distribution module and an acquisition module;
the allocation module is configured to allocate a first bank memory through the second process, where the first bank memory is used to store control information of the second process;
the obtaining module is configured to obtain, by the second process, control information of the second process from the first heap memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the allocating module is further configured to allocate a second heap memory through the third process, where the second heap memory is used to store control information of the third process;
the obtaining module is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In a specific implementation manner, the first heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
In a specific implementation manner, before the second process writes the first data into the first mapping region, the apparatus further includes a sending module;
the allocation module is configured to allocate a third heap of memory through the first process, where the third heap of memory is used to store control information of the second process;
the obtaining module is configured to receive, by the first process, a first information obtaining request sent by the second process, where the first information obtaining request is used to request to obtain control information of the second process from the third heap memory;
the sending module is configured to send, by the first process according to the first information acquisition request, the control information of the second process to the second process, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the allocating module is further configured to allocate a second heap memory through the third process, where the second heap memory is used to store control information of the third process;
the obtaining module is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In a specific implementation manner, the third heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
In a particular implementation, before the second process writes the first data to the first mapping region,
the allocation module is configured to allocate a first bank memory through the second process, where the first bank memory is used to store control information of the second process;
the obtaining module is configured to obtain, by the second process, control information of the second process from the first heap memory, so that the second process writes the first data into the first mapping area according to the control information of the second process;
before the third process maps the first data from the first shared memory to the second mapping region, the apparatus further includes a receiving module;
the allocation module is configured to allocate a third heap of memory through the first process, where the third heap of memory is used to store control information of the third process;
the receiving module is configured to receive, through the first process, a second information acquisition request sent by the third process, where the second information acquisition request is used to request to acquire control information of the third process from the third heap memory;
the sending module is configured to send, by the first process according to the second information acquisition request, the control information of the third process to the third process, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In a specific implementation manner, the first heap memory and the first mapping region are different memories, the first mapping region is used to store shared memory information of the second process, the third heap memory and the second mapping region are different memories, and the second mapping region is used to store shared memory information of the third process.
In a specific implementation manner, the first process includes a detection thread, and the apparatus further includes a detection module, where the detection module is specifically configured to:
the detection module detects whether the second process or the third process is reset or not through the detection thread;
and under the condition that the detection module detects that the second process or the third process is reset, the detection module informs the second process and a process which is not reset in the third process to terminate communication through the detection thread, and releases the first shared memory.
In a specific implementation manner, the first process includes a shared memory restoring thread, where the shared memory restoring thread is a thread with a highest running level in threads included in the first process, and the apparatus further includes a restoring module, where the restoring module is specifically configured to:
when the first process is restarted, the recovery module recovers the first shared memory through the recovery shared memory thread.
In a specific implementation manner, the first shared memory includes a destroyer queue, and the communication module is specifically configured to:
the communication module maps the first data from the first mapping region to the destroyer queue through the second process;
the communication module maps the first data from the destroyer queue to the second mapping region through the third process.
In a specific implementation manner, the creating module is further configured to create, by the first process, a second shared memory between the second process and the third process, wherein the first process has read-write permission to the second shared memory, the third process has read-write permission to the second shared memory, the second process has read-only permission to the second shared memory, a third mapping relation is formed between a third mapping area and the second shared memory, a fourth mapping relation is formed between a fourth mapping area and the second shared memory, the third mapping region is a region in the third process virtual address space that establishes a mapping relationship with the second shared memory, the fourth mapping region is a region in the second process virtual address space, which establishes a mapping relationship with the second shared memory;
the communication module is further configured to write the second data into the third mapping region through the third process, and map the second data from the third mapping region to the second shared memory according to the third mapping relationship, and the second process maps the second data from the second shared memory to the fourth mapping region according to the fourth mapping relationship, so that unidirectional communication from the third process to the second process is achieved.
In a third aspect, the present application provides an inter-process communication apparatus, comprising: a processor, a communication interface, and a memory; the memory is configured to store instructions, the processor is configured to execute the instructions, and the communication interface is configured to receive or transmit data; wherein the processor executes the instructions to perform the method as described in the first aspect or any specific implementation manner of the first aspect.
In a fourth aspect, the present application provides a computer storage medium storing a computer program for inter-process communication, which when executed by a processor, implements a method as described in the first aspect above or any specific implementation of the first aspect.
In a fifth aspect, the present application provides a computer program product, which, when being read and executed by a computer, implements the method as described in the first aspect or any specific implementation manner of the first aspect.
Drawings
The drawings used in the embodiments of the present application are described below.
Fig. 1 is a schematic application scenario diagram of an inter-process communication method provided in an embodiment of the present application;
fig. 2 is a schematic diagram of a data transmission process provided in an embodiment of the present application;
FIG. 3 is a schematic diagram of an interprocess communication system based on a binder mechanism according to an embodiment of the application;
FIG. 4 is a flowchart illustrating an inter-process communication method according to an embodiment of the present application;
FIG. 5 is a schematic diagram of a breaker queue provided by an embodiment of the present application;
FIG. 6 is a diagram illustrating a process virtual address space provided by an embodiment of the present application;
FIG. 7 is a flowchart illustrating an inter-process communication method according to an embodiment of the present application;
FIG. 8 is an interaction diagram of an interprocess communication method according to an embodiment of the present application;
FIG. 9 is a block diagram of an interprocess communication device according to an embodiment of the present application;
fig. 10 is a schematic structural diagram of another interprocess communication device provided in the embodiment of the present application.
Detailed Description
The embodiments of the present application will be described below with reference to the drawings.
Before describing the methods in the embodiments of the present application, in order to facilitate understanding of the embodiments of the present application by those skilled in the art, the related concepts related to the embodiments of the present application will be described.
Process isolation is a technique designed to protect processes in an operating system from interfering with each other. The technology is used for avoiding the situation that the process A modifies the data in the process B, so that the process A is prevented from influencing the operation of the process B, and even the process A can damage the operation of the process B. The process is isolated by a virtual address space, and specifically, the virtual address space of the process a is different from the virtual address space of the process B, so that the process a can be prevented from modifying data in the process B.
Virtual address space: the virtual address space does not actually exist in the computer. Each process is assigned its own virtual address space and can only access its own assigned virtual address space. Theoretically, the virtual address space is limited by the size of the physical memory, and if the physical memory is 4GB, the address range of the virtual address space should be 0x00000000 to 0 xfffffff. Each process has an independent virtual address space, so that each process can access the virtual address space of the process, and effective process isolation is achieved.
Physical address space: the physical address space is an entity existing in a computer, and is maintained as a unique independence in each computer, which may also be referred to as a physical memory. For example, on a 32-bit machine, the physical memory size could theoretically reach 4GB, but if 512MB of memory is actually loaded, the real valid portion of the physical address space is only 512MB, and the rest is invalid.
There is a mapping between the virtual address space and the physical address space, the mapping is accomplished through a Memory Management Unit (MMU), and the process can indirectly operate the physical address space by operating its own virtual address space.
The user space (user space) is a space in which a user program runs, and the kernel space (kernel space) is a space in which a system kernel runs. The current operating system is a virtual memory, and for a 32-bit system, the virtual address space is 32 powers of 2, namely 4 GB. The kernel of the operating system is a kernel, which is independent of the normal application program, and can access the protected virtual address space and also access the underlying hardware devices. In order to protect the kernel which can not be directly operated by the user process and ensure the safety of the kernel, the virtual address space is logically divided into a user space and a kernel space by the operating system. For the Linnax (linux) operating system, the top 1GB byte is used by the kernel, called kernel space, and the lower 3GB byte is used by each process, called user space. That is, the highest 1GB of the 4GB virtual address space of each process is the same, i.e., kernel space. Only the remaining 3GB is used by the process itself. In other words, the kernel space is shared by all processes.
User mode: when a process runs in user space, it is in user state, where the processor runs in Ring3 user code with the lowest privilege level, Ring3 state cannot access Ring 0's address space, including code and data.
Kernel mode: when the process is in the kernel mode when running in the kernel space, the process can execute the system call and run in the kernel space, and the processor is executed in the Ring0 kernel code with the highest privilege level.
Although the operating system logically divides the user space and the kernel space, there are inevitable processes running in the user space that need to access resources of the kernel space, such as file operations or operations of accessing a network, and these operations need to be implemented by means of system calls. The system call is the only mode that the process of the user space accesses the kernel space, ensures that all operations are carried out under the control of the kernel, avoids unauthorized access of the process of the user space to the kernel space, and improves the safety and stability of the system.
In the user mode, the process can only access the memory in a limited manner, and does not allow access to peripheral devices, such as a hard disk, a network card, and the like, the capacity of occupying a Central Processing Unit (CPU) is deprived, and the CPU resource can be acquired by other processes.
In the kernel mode, the CPU can access all data of the memory, including peripheral devices, and can also switch itself from one process to another.
The application scenario of IPC may be that one process sends its data to another process, that one process sends a message to another process to notify the other process that some event occurs, that one of the two processes modifies shared data, that the other process should see it immediately, etc.
Here, an application scenario of the inter-process communication method provided in the embodiment of the present application is described by taking an example in which one process sends its data to another process.
As shown in fig. 1, a process a and a process B are both located in a user space, and the process a and the process B are two different processes, where the process a includes data a, and when the process B also wants to use the data a in the process a, the process a and the process B are different processes and process isolation exists, so the process B cannot directly use the data a in the process a, and if it is to be realized that the process B can use the data a in the process a, a special communication mechanism, i.e., IPC, must be used.
It will be appreciated that in the case of process a providing data a to process B, process a is a server process in client/server (C/S) mode and process B is a client process in C/S mode. Similarly, if the process B provides its own data B to the process a, the process B is a server process in the C/S mode, and the process a is a client process in the C/S mode. In the C/S mode, the process running on the client is a client process, and the process running on the server is a server process.
Currently, in order to enable different processes to access and work in coordination with each other, the operating system provides multiple IPC mechanisms, such as sockets (sockets), pipes, message queues, shared memories, and binders (binders).
The socket for interprocess communication is an abstraction layer, the process A and the process B can send or receive data through the socket, and the process A and the process B can perform operations such as opening, reading, writing, closing and the like on the socket like a file. A socket can be considered as an endpoint in the respective communication connection when two network processes are communicating. During communication, the process A writes the data A to be transmitted into the socket of the host where the process A is located, the socket sends the data A into the socket of the host where the process B is located through a transmission medium of the network interface card, and the data A can be transmitted to the process B, so that communication between the process A and the process B is achieved. However, in the process of using the socket to communicate, the process needs to execute a system call to trap in the kernel space to perform data transmission, and the transmission efficiency is low. Thus, sockets are primarily used for inter-process communication on different hosts across a network and for low-speed communication between processes on the same host.
The pipe for interprocess communication is the oldest form of the UNIX (unix) IPC, a special file in memory. The pipeline is divided into a named pipeline and an anonymous pipeline, the named pipeline allows communication between two unrelated processes, and the anonymous pipeline can only realize one-way communication and can only be used between the processes with genetic relationship and is commonly used for parent and child processes. The problem with the pipe is described with reference to fig. 2 and the related description.
The message queue used for interprocess communication is a linked list of messages, stored in the kernel and identified by a message queue identifier. By using a message queue, processes can read information in order, or prioritize messages. The problem with message queues is also shown in fig. 2 and the associated description.
As shown in fig. 2, when data transmission is performed, a process a usually creates a memory cache region in its own virtual address space, the process a stores data a in the memory cache region, and then the process a sinks into the kernel space through system call, and the kernel calls a copy from user mode to kernel mode (copy from user) function to copy data a from the memory cache region of the process a to the kernel cache region created by the kernel. After the kernel copies the data A to the kernel cache region, the kernel calls a function copied from the kernel mode to the user mode (copy to user) to copy the data A from the kernel cache region to a memory cache region opened by the process B. Thus, process a and process B complete one transmission of data a, that is, process a and process B complete one inter-process communication.
It can be seen that, in the pipeline and the message queue for inter-process communication, when data transmission is performed, the process a also needs to execute a system call to trap in the kernel space for data transmission, and the kernel needs to copy the data a from the kernel cache region of the process a to the kernel cache region and then from the kernel cache region to the kernel cache region of the process B, and this process needs to perform data copying 2 times, and the efficiency of data transmission is low.
Shared memory for interprocess communication is the same physical memory that process a and process B are allowed to access, process a and process B can map the same physical memory to their own virtual address space, and then both process a and process B can access the physical memory.
When the process A transmits the data A to the process B through the shared memory, the process A writes the data A into the virtual address space of the process A, the data A in the virtual address space of the process A is mapped to the shared memory through the mapping relation between the virtual address space of the process A and the shared memory, and then the data A in the shared memory is mapped to the virtual address space of the process B through the mapping relation between the virtual address space of the process B and the shared memory. That is, the process a writes the data a in its own virtual address space, and the data a is immediately mapped to the virtual address space of the process B that can access the same shared memory, and thus is read by the process B in time.
It can be seen that the shared memory for inter-process communication can realize data transmission only through mapping without data copying, so that efficient inter-process communication can be realized, and the method is a very effective way for sharing and transferring data among processes. However, the shared memory is complex to control, for example, when the process a performs write operation on the shared memory, there is no automatic mechanism to prevent the process B from performing read operation on the shared memory, and meanwhile, if there are multiple processes simultaneously performing write operation or read operation on the shared memory, resources are contended among the multiple processes, which may cause data confusion in the shared memory. Therefore, in the prior art, it is usually necessary to use other mechanisms such as a read-write lock or a mutex lock, etc. to ensure that, when the process a performs a write operation on the shared memory, the process B cannot perform the read operation on the shared memory, and to prevent resource contention caused by simultaneous read operation or write operation on the shared memory by multiple processes.
In addition, with the above mechanisms for inter-process communication, when data is transmitted, the process a and the process B cannot obtain a User Identification (UID) or a Process Identification (PID) of the other party, that is, the process a and the process B cannot reliably verify the identity of the other party. Meanwhile, access points of the communication mechanisms are all open, a private communication channel cannot be established between the process A and the process B, and a malicious program can establish connection with another process by guessing the address of the process A or the address of the process B to steal messages. It can be seen that the above mechanisms for interprocess communication also have the problem of low security.
Next, a description is given of a currently popular binder mechanism for inter-process communication, which is an inter-process communication mechanism in an android operating system and is commonly used for android devices.
As shown in FIG. 3, an IPC system based on binder mechanism generally includes four parts: the system comprises a client, a server, a binder driver and a service management module, and can be seen that a service mode adopted by the binder mechanism is also a C/S mode. Wherein the content of the first and second substances,
server process (e.g., process a): a service (e.g., data a) is provided to the process used by the client process.
Client process (as process B): a process that wants to use a service (e.g., data a) in a server process.
binder driving: the client process can acquire a certain service in the server process through the binder driver, and then the client can call the service acquisition result.
A service management module: if the server process needs to provide services for the client process, the server process needs to submit a UID or PID of the server process for registration in the service management module, and register the services which can be provided by the server process in the service management module, so that the service management module can know which services can be provided for the client process by the server process, and when the client process needs to use the services in the server process, the server process needs to submit the UID or PID of the server process for registration in the service management module, and then can go to the service management module to obtain the services.
As can also be seen from fig. 3, the binder mechanism for interprocess communication uses shared memory, which is implemented by memory mapping.
The process of data transmission realized by sharing the memory in the binder mechanism is as follows:
the process A copies the data A from the virtual address space of the process A to the kernel cache region by calling a copy from user function, the data A of the kernel cache region can be directly mapped to the data receiving cache region because of the mapping between the kernel cache region and the data receiving cache region, and the data A of the data receiving cache region is directly mapped to the virtual address space of the process B because of the mapping between the data receiving cache region and the virtual address space of the process B, so that the process A and the process B complete the transmission of the data A once, namely, the process A and the process B complete the inter-process communication once. It can be seen that the kernel needs to execute a system call to copy the data a from the virtual address space of the process a to the kernel cache, that is, the data transfer process needs to perform a data copy.
In addition, as can be seen from fig. 3, the client process, the server process, and the service management module run in the user space, and the binder driver runs in the kernel space. The interaction among the client process, the server process and the service management module is a dotted line, which means that the client process, the server process and the service management module are not directly interacted but are interacted through a binder driver, that is, the interaction among the client process, the server process and the service management module needs to be trapped in a kernel space through system call and is interacted through the binder driver positioned in the kernel space.
As can be seen from the introduction of the binder mechanism for inter-process communication, when the process a and the process B communicate through the binder mechanism, both the process a and the process B need to register in the service management module, and then the process a and the process B can communicate with each other. In addition, a shared memory is introduced into a binder mechanism for interprocess communication, so that the interprocess communication efficiency is improved. However, the binder mechanism for interprocess communication also introduces a kernel state in the communication process, and needs to perform one-time data copy during data transmission, which affects the performance of the operating system and cannot meet some interprocess communication scenarios that need to perform zero-time data copy. Meanwhile, the binder mechanism is an IPC mechanism in an android operating system, and is not suitable for terminal equipment constructed based on operating systems such as linux and windows.
In addition, when the above several inter-process communication mechanisms except the shared memory are applied to various embedded devices of the operating system, the single board and the operating system of the embedded device need to be adapted, so that the operation is complex and the portability is low. Moreover, if the kernel state becomes faulty, new interprocess communication will be out of kernel state and become chaotic.
In order to solve the problems of low safety and reliability existing in the interprocess communication mechanism or low performance caused by introducing a kernel mode, the application provides the interprocess communication method, which not only can improve the safety and reliability of interprocess communication, but also can effectively improve the performance of interprocess communication. The inter-process communication method provided by the embodiment of the application is not only suitable for terminal equipment constructed based on an android operating system, but also suitable for terminal equipment constructed based on operating systems such as linux and windows, and is not specifically limited herein.
The inter-process communication method provided by the embodiment of the present application is explained in detail below.
Referring to fig. 4, fig. 4 is a flowchart illustrating an inter-process communication method according to an embodiment of the present application, and as shown in fig. 4, the method may include:
s101, the first process creates a first shared memory between the second process and the third process.
The first process, the second process and the third process run in a user space, the first process has read-write permission for the first shared memory, the second process has read-write permission for the first shared memory, the third process has read-only permission for the first shared memory, and the second process and the third process are registered in the first process.
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, a process in which the second process and the third process are registered in the first process is described.
Taking the second process as an example of registering in the first process, specifically, the first process and the second process may establish a connection through a domain socket (domain socket) communication mechanism, and after the first process and the second process establish a connection, the first process registers for the second process. The domain socket develops an IPC mechanism on a socket framework and is used for interprocess communication of the same host.
More specifically, the first process may monitor, through a monitoring (listen) function in the domain socket communication mechanism, whether any process has sent a registration request to the first process, and in a case that the first process monitors that the second process has sent a registration request to the first process, the first process may receive the registration request through a receiving (accept) function in the domain socket mechanism, and establish a connection with the second process. After the first process establishes a connection with the second process, the first process may register for the second process according to the registration request. Wherein, the second process sends the registration request to the first process, and the registration request can be sent to the first process through a connection function in the domain socket communication mechanism.
The registration request sent by the second process may include information such as a UID or PID of the second process, which is used by the first process to verify the identity of the second process, and may also be used by the first process to register for the second process according to the information such as the UID or PID of the second process.
It can be understood that, for a specific implementation, reference may be made to the process of registering a second process in a first process described in the foregoing embodiment, and details are not repeated here.
In the process of registering the first process as the second process, the first process may also create a first shared memory according to information, such as the UID or PID, of the second process included in the registration request, where the first shared memory is used to implement one-way communication from the second process to another process (e.g., a third process). Similarly, in the process of registering the first process as the third process, the first process may also create a second shared memory according to 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 (e.g., the second process).
In this embodiment of the present application, the first process creates the first shared memory by creating a first shared file through the first process, where the first shared file is a file actually existing in the physical memory, and a physical address of the first shared memory is the same as a physical address of the first shared file. Similarly, the first process creates the second shared memory, which is also realized by creating the second shared file through the first process, the second shared file is also a file actually existing 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.
The first shared memory and the second shared memory may also have a shared memory identifier, the shared memory identifier is used to uniquely identify one shared memory, and the shared memory identifier may be named according to a physical address of the shared memory or information such as UID or PID of a process. For example, the identifier of the first shared memory may be named according to information such as UID or PID of the second process, and the identifier of the second shared memory may be named according to information such as UID or PID of the third process. The first shared file and the second shared file may also have a shared file identifier, the shared file identifier is used to uniquely identify one shared file, and the shared file identifier may be named according to information such as a physical address of the shared file or UID or PID of a process. For example, the identifier of the first shared file may be named according to information such as the UID or PID of the second process, and the identifier of the second shared file may be named according to information such as the UID or PID of the third process.
In a specific implementation, the first process may further store information such as a UID or PID of the second process, information such as a UID or PID of the third process, the first shared memory identifier, the second shared memory identifier, and the like. When the first process needs to manage the second process or the third process, the first process can manage the processes according to information such as UID (user identification) or PID (proportion integration differentiation) of the processes, and the management requirements of the processes can be further met; when the first process needs to manage the first shared memory or the second shared memory, the first process can find the corresponding shared memory for management according to the shared memory identifier, and further can meet the management requirement of the shared memory.
In this embodiment of the application, after the first process completes creation of the first shared memory, the first process is used as a creator, that is, an owner, of the first shared memory, and the first process not only has a read-write permission of the first shared memory, but also has a permission to configure the first shared memory for other processes. For example, a first process may configure a second process to have read-write permission of a first shared memory, and configure a third process to have read-only permission of the first shared memory. The process has the read-write permission of the first shared memory and indicates that the process can perform read operation and write operation on the first shared memory. The process has the read-only permission of the first shared memory, and the read-only permission indicates that the process can only carry out read operation on the first shared memory and can not carry out write operation.
As can be seen from the foregoing embodiments, the first shared memory corresponds to the first shared file, and therefore, the first process may have the read-write permission of the first shared file by configuring the second process, so that the second process has the read-write permission of the first shared memory, and may have the read-only permission of the first shared file by configuring the third process, so that the third process has the read-only permission of the first shared memory.
Specifically, the first process may configure, through an Access Control List (ACL), that the second process has the read-write permission of the first shared memory, and configure that the third process has the read-only permission of the first shared memory. Where an ACL is an access control list for files/directories, an ACL allows any user (process) to be set with access rights for any file/directory, and it consists of a series of rules. The access rights include three rights of readable r (read), writable w (write), or executable x (execute). Readable r represents the actual content of the file that can be read; writable w represents the actual content of a file that can be edited, modified, newly added, deleted, but not deleted; executable x indicates that the file has rights that can be executed by the system.
Specifically, the first process may set the second process and the third process as others of the first shared file through a set file ACL permission (setfacl) command in the ACL, set the second process to have a read-write permission (rw-) of the first shared file, and set the third process to have a read-only permission (r-) of the first shared file. After the setting is completed, the first process can check whether the second process has the read-write permission (rw-) of the first shared file and check whether the third process has the read-only permission (r-) of the first shared file through a check file ACL permission (getfacl) command in the ACL. The second process has the read-write permission of the first shared file, and the read-write permission of the first shared memory is represented by the second process. The third process has the read-only permission of the first shared file, which indicates that the third process has the read-only permission of the first shared memory.
After the second process has the read-write permission of the first shared memory, if the second process needs to perform read-write operation on the first shared memory, the second process needs to establish a mapping relationship between the virtual address space of the second process and the first shared memory. After the mapping relationship is established, the second process can perform read-write operation on the first shared memory. Similarly, after the third process has the read-only permission of the first shared memory, if the third process needs 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, and after the mapping relationship is established, the third process can perform the read operation on the first shared memory.
In this embodiment of the application, the second process and the third process may establish a mapping relationship between their own virtual address spaces and the first shared memory through a memory map (mmap) mechanism. Mmap is a method for mapping files in a memory, that is, a file is mapped to a virtual address space of a process, so that a one-to-one mapping relation between a file address and the virtual address space of the process is realized. After the mapping relation is realized, the process can adopt a pointer mode to perform read-write operation on the virtual address space, so that the read-write operation on the file is realized.
Next, a process of establishing a mapping relationship between the virtual address space of the second process and the first shared memory by the mmap mechanism in the second process and the third process will be described in detail.
Taking as an example that the second process establishes a mapping relationship between its own virtual address space and the first shared memory through the mmap mechanism, the process may include:
the first step, the second process calls the library function mmap in the user space.
And step two, the second process opens up a first mapping area in the virtual address space of the second process.
The first mapping area is an area in the second process virtual address space, which establishes a mapping relation with the first shared memory.
And thirdly, allocating a virtual area structure for the first mapping area by the second process, and then initializing each domain of the virtual area structure.
And fourthly, the second process inserts the virtual area structure into a linked list or a tree of the first mapping area of the second process.
And fifthly, 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 body of the first shared file in the 'opened file set' of the kernel through the file descriptor of the first shared file.
And sixthly, the second process is linked to a file organization (file operations) module through a file structure body of the first shared file, and the kernel function mmap is called.
And seventhly, positioning the second process to the first shared file address through a virtual file system index (inode) module.
And step eight, the second process establishes a page table through a mapping (remap _ pfn _ range) function from the device memory to the user space address.
Through the eight steps, the mapping relation between the first mapping area and the first shared file is established, that is, the mapping relation between the first mapping area and the first shared memory is established.
It can be understood that the process of establishing the mapping relationship between the third process virtual address space and the first shared memory, that is, the process of establishing the mapping relationship between the second mapping region and the first shared memory, is similar to the process of establishing the mapping relationship between the first mapping region and the first shared memory, and is not repeated here. For convenience of description, in the following embodiments of the present application, the mapping relationship between the first mapping region and the first shared memory is represented by a first mapping relationship, and the mapping relationship between the second mapping region and the first shared memory is represented by a second mapping relationship.
S102, the second process writes the first data into the first mapping area, maps the first data from the first mapping area to the first shared memory according to the first mapping relation, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relation, so that one-way communication from the second process to the third process is achieved.
After the first mapping relationship and the second mapping relationship are established, the second process writes the first data into the first mapping region, and then maps the first data from the first mapping region to the first shared memory according to the first mapping relationship. And 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 that the third process performs a read operation on the first shared memory to read the first data from the first shared memory.
In a more specific embodiment, the second process may include a write thread for writing the first data in the first mapped region, and the third process may include a read thread for reading the first data from the second mapped region. The write thread is activated when the second process has the first data to write to inform the third process that the first data needs to be read. The reading thread is activated when the third process has the first data to be read so as to read the first data, and after the reading is finished, the second process is informed that the reading of the first data is finished.
It should be noted that, after the first data is written into the first shared memory, the content in the first shared memory is changed, but the first data is not immediately written back to the first shared file to change the content in the first shared file, but there is a delay of a period of time, after a certain period of time, the operating system automatically writes back a dirty page including the first data to the corresponding first shared file, that is, the process of writing the first data into the first shared file is completed. If the content of the first shared file is synchronized with the content in the first shared memory, a refresh change (msync) function may be called to enforce synchronization. In this way, the first data written by the second process can be synchronously saved in the first shared file.
In a specific implementation, when the second process performs unidirectional communication to the third process through the first shared memory, the first mapping relationship is not always released after a small amount of data is read and written, or the second mapping relationship is released, but the mapping relationships between the respective mapping areas and the first shared memory are maintained until the communication is completed. Thus, the first data is always stored in the first shared memory and is not recycled by the second process. The first data is often recycled by the second process after the second process has communicated with the third process.
The second process reclaims the first data, which indicates that the communication between the second process and the third process is finished, the second process may release the first mapping relationship, and the third process may release the second mapping relationship. After the mapping is released, the first process may release the resources occupied by the first shared memory.
In order to further improve performance of communication between the second process and the third process, in a specific implementation manner, the first shared memory further includes a destroyer (disarmer) queue, the second process writes the first data into the first shared memory, and the specific process of the third process reading the first data from the first shared memory is as follows:
a1, the second process writes the first data into the first mapping area.
A2, the second process maps the first data from the first mapping region to the disaraptor queue.
Specifically, the second process maps the first data from the first mapping region to the disaraptor queue in the first shared memory according to the first mapping relationship.
A3, the third process maps the first data from the dispatcher queue to the second mapping region.
Specifically, the third process maps the first data from the dispatcher queue to the second mapping region according to the second mapping relationship.
It can be seen that, in this embodiment, the process of transmitting the first data from the second process to the third process is implemented by mapping, and data copying of the first data is not required, which can meet the requirement that zero data copying is required in some inter-process communication scenarios.
In a possible implementation manner, the first shared memory further includes a preset shared area in addition to the dispatch queue, where the preset shared area is used to store the first data. The second process writes the first data into the first shared memory, and the specific process of the third process reading the first data from the first shared memory is as follows:
b1, the second process writes the first data into the first mapping area.
And B2, the second process maps the first data from the first mapping area to the preset shared area.
Specifically, the second process maps the first data from the first mapping region to a preset shared region in the first shared memory according to the first mapping relationship.
And B2, the second process outputs the output stream containing the storage information of the first data from the preset sharing area to the disaraptor queue, wherein the storage information of the first data is the first address information and the length information of the first data in the preset sharing area, or the first address information and the last address information of the first data in the preset sharing area.
B3, the third process maps the storage information of the first data from the disputer to the second mapping area,
b4, the third process reads the first data from the preset shared area according to the address information and the length information of the first data.
Specifically, the third process maps the first data from the dispatcher queue to the second mapping region according to the second mapping relationship.
It can be seen that, in the present embodiment, in the process of transmitting the first data to the third process by the second process, the second process is required to output the output stream including the storage information of the first data from the preset shared memory area to the disaraptor queue, and a data copy needs to be performed, so that a requirement that a data copy needs to be performed in some inter-process communication scenarios can be met.
Next, the operation principle of the disaraptor queue will be described.
As shown in fig. 5, thread a represents a write thread in the second process having read-write authority, and thread B and thread C represent read threads in the two processes having read-only authority.
As shown in fig. 5, in the dispatcher queue, there is a ring buffer (ring buffer) with many locations called slots, such as slot 1, slot 2, slot 3 …, slot 10, slot 11, slot 12 in fig. 5. Each data in the ring buffer is indexed by a sequence number (sequence number), which in an implementation may be set to be the same as the position number of the slot. The ring buffer maintains the sequence number of the current latest written data, the sequence number is always incremented, for example, the slot 10 and the slot 11 have already been written with data, the sequence number of the latest written data maintained by the ring buffer is 11, after receiving the data a to be written by the thread a, the event handler knows from the ring buffer that the slot position to be written with the data a next is 12, and the thread a writes the data a to be written into the slot position 12. After writing, the ring buffer will update the sequence number of the last written data a to 12 and inform thread B and thread C that the data with sequence number 12 can be read. After receiving the notification, the thread B and the thread C read the data a based on the sequence number 12 of the received data a. In the case where thread B and thread C do not receive the notification, thread B and thread C enter a wait state, waiting for data a to be written to the ring buffer by thread a, and once data a is written to the ring buffer, thread B and thread C receive the notification, and thread B and thread C can then read data a based on the received sequence number 12.
It should be noted that, the thread B and the thread C do not directly read the data a in the ring buffer, but call a wait (wait for) method of a sequence barrier (sequence barrier) object according to the received sequence number 12, transfer the sequence number 12 to the sequence barrier object, and the sequence barrier object obtains the data a of the slot 12 in the ring buffer according to the sequence number 12 and then returns the data a to the thread B and the thread C.
It can be understood that the thread B and the thread C read the data a after receiving the notification of the ring buffer, and the thread B and the thread C do not read the data a when the thread a has not finished writing the data a, so that the thread B or the thread C can be prevented from performing a read operation while the thread a performs a write operation. Meanwhile, the thread B and the thread C read the data A by the sequence barrier object according to the received serial number 12, and the thread B and the thread C do not directly operate the ring buffer, so that the thread B and the thread C do not generate resource competition for the ring buffer, and the thread B and the thread C do not need to be locked.
In a specific application, the number of the grooves in the ring buffer area can be customized and set, and the number of the grooves in the ring buffer area is 2nWherein n is a positive integer. Therefore, the ring buffer can be of different data buffer sizes from tens of thousands to tens of millions, and therefore, the problem of memory overflow when the write operation speed of the thread A is too high can be solved by using the disaraptor queue in the first shared memory.
According to the interprocess communication method provided by the embodiment of the application, the disaraptor queue is used in the first shared memory, so that when the second process performs write operation on the first shared memory, the third process cannot perform read operation on the first shared memory, and the use of locks when a plurality of processes perform read operation on the first shared memory is also avoided. Meanwhile, the interrupt queue is used in the first shared memory, repeated reading and writing of the first shared memory by the process are supported, the throughput of information between the processes can be effectively improved, and the performance of communication between the processes can be further improved.
In addition, it can be seen that, in the inter-process communication method provided in the embodiment of the present application, when the first shared memory includes only a disaraptor queue, a data copy is not required in a one-way communication process from the second process to the third process; under the condition that the first share also comprises a preset share area besides a dispatcher queue, a one-way communication process from the second process to the third process needs to copy data once. The interprocess communication method provided by the embodiment of the application is not only suitable for an interprocess communication scene needing zero-time data copying, but also suitable for an interprocess communication scene needing one-time data copying, and is good in flexibility.
In order to further improve the security and reliability of the communication between the second process and the third process, in a specific embodiment, before the second process writes the first data into the first mapping area, the second process needs to allocate a first bank of memory, where the first bank of 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 bank of 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 region, the third process needs to allocate a second heap memory, where the second heap memory is used to store control information of the third process, and then the third process acquires 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 region according to the control information of the third process.
The control information of the second process may include information that a process communicating with the second process is a third process, and the control information of the second process may also include read-write permission information of the first shared memory that the second process has, and the like, which is not limited specifically herein. The control information of the third process may include information that a process communicating with the third process is the second process, and the control information of the third process may further include read-only permission information of the first shared memory that the third process has, and the like, which is not specifically limited herein.
In the above embodiment, the control information of the second process is stored in the first heap memory (heap memory of the first process), and the control information of the third process is stored in the second heap memory (heap memory of the third process), so that when the second process communicates with the third process, the second process and the third process can directly obtain their control information from their heap memories for communication, and the efficiency of inter-process communication is high. Meanwhile, the control information of the second process is stored in the first heap memory, and the control information of the third process is stored in the second heap memory, so that the control information of the second process and the control information of the third process can be separately stored, the second process and the third process are independent and do not interfere with each other during operation, and when one of the second process and the third process breaks down, the fault cannot be diffused to the other process.
In practical application, the second process and the third process may call a memory allocation (malloc) function or a create (new) function to allocate to obtain a corresponding first heap memory and a second heap memory, the second process may determine a size of the allocated first heap memory, the third process may determine a size of the allocated second heap memory, and the second process and the third process may release the corresponding first heap memory and the second heap memory through a release (free) function or a delete (delete) function.
It should be noted that the process allocates heap memory, which is allocated from the process' own virtual address space. For example, the first heap of memory allocated by the second process is memory in the second process's own virtual address space. As shown in fig. 6, as can be seen from fig. 6, the second process virtual address space includes a heap memory, a stack memory, a shared memory mapping region (first mapping region), and the like. As can be seen from fig. 6, the first mapping region is located between the heap memory of the second process (first heap memory) and the stack memory of the second process. That is, the first bank memory and the first mapping region are two different memories in the second process virtual address space.
The first mapping area stores shared memory information of the second process, and the shared memory information of the second process may include information such as a first mapping relationship or first data written into the first mapping area by the second process, which is not specifically limited herein. The second mapping area stores shared memory information of the third process, and the shared memory information of the third process may include information such as the second mapping relationship or the first data read from the first shared memory, which is not specifically limited herein.
In the above embodiment, the control information of the second process is stored in the first heap memory, the shared memory information of the second process is stored in the first mapping region, and the first heap memory and the first mapping region are different memories, so that the control information of the second process and the shared memory information of the second process can be separately stored, and the security of the shared memory information of the second process can be ensured. Similarly, the control information of the third process is stored in the second heap memory, the shared memory information of the third process is stored in the second mapping area, and the second heap memory and the second mapping area are different memories, so that the control information of the third process and the shared memory information of the third process can be separately stored, and the safety of the shared memory information of the third process can be ensured.
In another specific embodiment, before the second process writes the first data into the first mapping region, the first process needs to allocate a third heap memory, where the third heap memory is used to store control information of the second process, and then the second process sends a first information acquisition request to the first process to request to acquire control information of the second process from the third heap memory, and after the second process acquires the control information of the second process returned by the first process, the second process may write the first data into the first mapping region according to the control information of the second process. Before the third process maps the first data from the first shared memory to the second mapping region, the third process needs to allocate a second heap memory, where the second heap memory is used to store control information of the third process, and then the third process acquires the control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping region according to the control information of the third process.
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 send a request for obtaining the control information to the first process first 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 heap memory, which can be seen that the communication efficiency of the second process is lower than that of the third process. That is, the method of storing the control information of the process in the heap memory of the process is suitable for the process with high performance requirement; the method for storing the control information of the process in the heap memory of the first process is suitable for the processes with lower performance requirements. Therefore, the method is not only suitable for the process with high communication performance requirement, but also suitable for the process with low communication performance requirement, and has high flexibility. Meanwhile, the control information of the second process is stored in the heap memory of the first process, and the control information of the third process is stored in the heap memory of the third process, so that the control information of the second process and the control information of the third process can be separately stored, the second process and the third process are independent and do not interfere with each other during operation, and when one of the second process and the third process fails, the failure cannot be diffused to the other process.
It can also be known from the foregoing embodiment that the third heap memory is a memory in a virtual address space of the first process, the first mapping region is a memory in a virtual address space of the second process, and process isolation exists between the first process and the second process, so it can be understood that the third heap memory and the first mapping region are also two different memories. In addition, it can be seen from the above embodiments that the second heap memory and the second mapping region are two different memories in the third process virtual address space.
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 region, so that the control information of the second process and the shared memory information of the second process can be stored separately, and the security of the shared memory information of the second process is favorably ensured. Meanwhile, the control information of the third process is stored in the second heap memory, and the shared memory information of the third process is stored in the second mapping area, so that the control information of the third process and the shared memory information of the third process can be stored separately, and the security of the shared memory information of the third process is guaranteed.
In another specific implementation, before the second process writes the first data into the first mapping region, the second process needs to allocate a first heap memory, where the first heap memory is used to store 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 region according to the control information of the second process. Before the third process maps the first data from the first shared memory to the second mapping region, the first process needs to allocate a third heap memory, where the third heap memory is used to store control information of the third process, and then the third process sends a second information acquisition request to the first process to request to acquire the control information of the third process from the third heap memory.
It can also be known from the foregoing embodiment that the third heap memory is a memory in a virtual address space of the first process, the second mapping region is a memory in a virtual address space of the third process, and process isolation exists between the first process and the third process, so it can be understood that the third heap memory and the second mapping region are also two different memories. In addition, it can be known that the first heap memory and the first mapping region are two different memories in the second process virtual address space.
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 region, so that the control information of the second process and the shared memory information of the second process can be separately stored, which is beneficial to ensuring the security of the shared memory information of the second process. Meanwhile, 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, and the security of the shared memory information of the third process is guaranteed.
It can be seen from the foregoing embodiment that the heap memory of the first process, the heap memory of the second process, the heap memory of the third process, the first mapping region, the second mapping region, and the first shared memory are different memories. That is to say, the embodiment of the present application can implement 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 beneficial to ensuring the security of the first shared memory information.
The first shared memory may store first shared memory information, and the first shared memory information may include information such as 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, which is not limited specifically herein.
In order to further ensure the reliability of the communication between the second process and the third process, as an embodiment of the present application, the first process includes a detection thread, which is configured to detect the states 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, and when the first process detects that the second process or the third process is reset, the first process may timely notify the second process and the third process that the reset process does not occur so that the communication connection can be terminated in time, and the occupied first shared memory resource can be released in time. Wherein, the process reset means that the state of the process returns to the initial state, and the process waits to be executed again.
In this embodiment, for the unidirectional 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 may close the first shared memory and release the first shared memory resource in time.
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 further includes recovering the shared memory thread, where the recovered shared memory thread is a thread with the highest running level in threads included in the first process, and when the second process communicates with the third process through the first shared memory, if the first process needs to be restarted due to a reset, the thread which runs first in the first process is the recovered shared memory thread, and other threads included in the first process, such as the detection thread, may be delayed or suspended when the shared memory thread is recovered, and the resource is granted to the recovered shared memory thread, and the recovered shared memory thread may recover the first shared memory at the fastest speed.
It can be seen from the foregoing embodiments that, in the inter-process communication method provided in the embodiments of the present application, the first process may further detect whether the second process or the third process is reset, and in a case that the second process or the third process is reset, terminate the communication connection between the second process and the third process in time, and release the resource occupied by the first shared memory. Meanwhile, the first process can also set the running level of the recovered shared memory thread as the highest priority, and is used for rapidly recovering the first shared memory when the first process needs to be restarted.
It can be seen from the foregoing 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. The first process serves as a server process and provides a first shared memory for the second process and the third process, the second process and the third process serve as client processes, and the first shared memory provided by the first process is used for communication.
According to the inter-process communication method provided by the embodiment of the application, the first shared memory between the second process and the third process is created through the first process, the second process writes the first data into the first mapping area, the first data are mapped from the first mapping area to the first shared memory according to the first mapping relation, and the third process maps the first data from the first shared memory to the second mapping area according to the second mapping relation, so that one-way communication from the second process to the third process is realized. The process of transmitting the first data to the third process by the second process is realized by mapping through the first mapping relation and the second mapping relation, can be realized without trapping a system call kernel into a kernel space, does not need to copy data, and has low system overhead and high communication efficiency. Moreover, when the inter-process communication method provided by the application is applied to embedded equipment with various operating systems, because a kernel state is not introduced in the one-way communication process from the second process to the third process, the problem of adaptation between a single board and the operating system does not need to be considered, and the inter-process communication method is simple to operate and strong in portability. Meanwhile, except for the first process, only the second process has the read-write permission of the first shared memory, so that the situation that a plurality of processes write the first shared memory at the same time to cause data chaos in the first shared memory can be avoided. In addition, the second process and the third process are processes registered in the first process, the first process can check the identities of the second process and the third process, and only the process with the read-write permission or the read-only permission of the first shared memory can access the first shared memory, so that malicious programs can be prevented from tampering or damaging data in the first shared memory, and the safety and reliability of inter-process communication can be effectively improved.
The foregoing embodiment describes in detail a process of implementing unidirectional communication from the second process to the third process, and then describes a process of implementing unidirectional communication from the third process to the second process by using the inter-process communication method provided by the present application.
Referring to fig. 7, as shown in fig. 7, the method includes:
s201, the first process creates a second process and a second shared memory between the third processes.
The first process has read-write permission to the second shared memory, the third process has read-write permission to the second shared memory, the second process has read-only permission to the second shared memory, a third mapping relation is formed between a third mapping area and the second shared memory, a fourth mapping relation is formed between a fourth mapping area and the third shared memory, the third mapping area is an area which is in a third process virtual address space and establishes a mapping relation with the second shared memory, and the fourth mapping area is an area which is in a second process virtual address space and establishes a mapping relation with the second shared memory;
a process of creating the second shared memory between the second process and the third process by the first process is similar to the process of creating the first shared memory between the second process and the third process by the first process described in S01, a process of configuring the read-only permission of the second shared memory for the second process by the first process, and a process of configuring the read-write permission of the second shared memory for the third process by the first process is similar to the process of configuring the read-write permission of the first shared memory for the second process by the first process in S101, and a process of configuring the read-only permission of the first shared memory for the third process by the first process is similar to, and specific reference may be made to the related description in S101, and details are not repeated here. The third process establishes a mapping relationship between the third mapping region and the second shared memory, and the second process establishes a mapping relationship between the fourth mapping region and the second shared memory, which is similar to the process of the second process establishing a mapping relationship between the first mapping region and the first shared memory in S101, and is not repeated here.
S202, the third process writes the second data into the third mapping area, maps the second data from the third mapping area to the second shared memory according to the third mapping relation, and maps the second data from the second shared memory to the fourth mapping area according to the fourth mapping relation, so that one-way communication from the third process to the second process is achieved.
The third process writes the second data into the third mapping region, and maps the second data from the third mapping region to the second shared memory according to the third mapping relationship, which can be understood as that the third process writes 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 region according to the mapping relationship between the fourth mapping region and the second shared memory, which can be understood as that the second process performs a read operation on the second shared memory, and reads the second data from the second shared memory.
According to the inter-process communication method provided by the embodiment of the application, the first process is used for creating the second shared memory between the second process and the third process, the third process writes the second data into the third mapping area, the second data is mapped to the second shared memory from the third mapping area according to the third mapping relation, and the third process maps the second data to the fourth mapping area from the second shared memory according to the fourth mapping relation. Thereby realizing the one-way communication from the third process to the second process. The process of transmitting the second data from the third process to the second process is realized by mapping through the third mapping relation and the fourth mapping relation, the process can be realized without trapping a system call kernel into a kernel space, data copying is not needed, the system overhead is small, and the communication efficiency is high. Moreover, when the method is applied to embedded equipment with various operating systems, as the kernel state is not introduced in the one-way communication process from the third process to the second process, the problem of adaptation of the single board and the operating system does not need to be considered, the operation is simple, and the portability is strong. Meanwhile, except for the first process, only the third process has the read-write permission of the second shared memory, so that the situation that a plurality of processes write the second shared memory at the same time to cause data confusion in the second shared memory can be avoided. In addition, the second process and the third process are processes registered in the first process, the first process can verify the identities of the second process and the third process, and only the process with the read-write permission or the read-only permission of the second shared memory can access the second shared memory, so that malicious programs can be prevented from tampering or damaging data in the second shared memory, and the safety and reliability of inter-process communication can be effectively improved.
As can be seen from the above embodiments, the one-way communication from the second process to the third process is realized through S101 and S102, and the one-way communication from the third process to the second process is realized through S201 and S202. It is understood that, through S101, S102, S201, and S202, bidirectional communication between the second process and the third process can be achieved, and details of the process of achieving bidirectional communication between the second process and the third process are not repeated here.
To facilitate a better understanding and an implementation of the above-described aspects of the embodiments of the present application, a more specific scenario of interprocess communication is described below.
Referring to fig. 8, fig. 8 is an interaction diagram of an interprocess communication method according to an embodiment of the present disclosure. As shown in fig. 8, the inter-process communication method may include:
s301, the second process sends a registration request to the first process.
The registration request may include information such as a UID or PID of the second process, which is used for the first process to verify the identity of the second process, and to register the second process according to the information such as the UID or PID of the second process.
S302, the first process receives a registration request sent by the second process, registers for the second process to obtain registration information, creates a first shared memory, and configures the second process with the read-write permission of the first shared memory.
The first process may create a first shared memory according to information such as the UID or PID of the second process, the first shared memory may have a shared memory identifier, and the first shared memory identifier may be named according to a physical address of the first shared memory or information such as the UID or PID of the second process.
And S303, the first process sends the registration information to the second process.
S304, the second process receives the registration information returned by the first process, opens up a first mapping area, and establishes a mapping relation between the first mapping area and the first shared memory.
The registration information may include a first shared memory identifier, which is used by the second process to establish a mapping relationship between the first mapping area and the first shared memory.
S305, the second process sends a connection request to the first process, wherein the connection request is used for requesting the first process to establish the connection between the second process and a third process, and the third process is registered in the first process.
S306, the first process receives the connection request, determines a third process according to the connection request, and configures the third process to have the read-only permission of the first shared memory.
The connection request sent by the second process may include information such as a UID or PID of the third process, which is used by the first process to search the third process according to the identification information of the third process included in the connection request.
S307, the first process sends the connection request to the third process.
S308, the third process receives the connection request, develops a second mapping area and establishes a mapping relation between the second mapping area and the first shared memory.
The connection request may further include a first shared memory identifier, which is used by the third process to establish a mapping relationship between the second mapping area and the first shared memory.
S309, the third process returns a connection response to the first process.
And S310, the first process receives the connection response sent by the third process and forwards the connection response to the second process.
S311, the second process receives the connection response, obtains the control information of the second process from the first stack memory, and writes the first data into the disaraptor queue in the first shared memory through the first mapping area.
S312, the third process obtains the control information of the third process from the second heap memory, and reads the first data from the disaraptor queue in the first shared memory through the second mapping area.
The above details describe an interprocess communication method according to the embodiment of the present application, and based on the same inventive concept, the following provides an interprocess communication apparatus according to the embodiment of the present application, which is applied to various terminal devices that need to perform interprocess communication, including but not limited to electronic devices such as smart phones, tablet computers, laptop computers, and the like. The terminal device may be an electronic device constructed based on an operating system such as android, linux, or windows, or may be constructed based on other systems according to requirements, and is not specifically limited herein.
Referring to fig. 9, fig. 9 is a schematic structural diagram of an inter-process communication apparatus 100 according to an embodiment of the present application, and as shown in fig. 9, the inter-process communication apparatus 100 according to the embodiment of the present application at least includes: a creation module 110 and a communication module 120, wherein,
the creating module 110 is configured to create a first shared memory between a second process and a third process through a first process, where the first process, the second process, and the third process operate in a user space, the first process has a read-write permission for the first shared memory, the second process has a read-write permission for the first shared memory, the third process has a read-only permission for the first shared memory, the second process and the third process are both registered in the first process, a first mapping relationship is provided between the first mapping region and the first shared memory, a second mapping relationship is provided between the second mapping region and the first shared memory, the first mapping region is a region in a virtual address space of the second process that establishes a mapping relationship with the first shared memory, and the second mapping region is a region in a virtual address space of the third process that establishes a mapping relationship with the first shared memory.
The communication module 120 is configured to write the first data into the first mapping region through the second process, map the first data from the first mapping region to the first shared memory according to the first mapping relationship, and map the first data from the first shared memory to the second mapping region through the third process according to the second mapping relationship, so as to implement unidirectional communication from the second process to the third process.
In this embodiment of the present application, the creating module 110 is further configured to:
the creating module 110 configures, through the first process, the second process with the read-write permission of the first shared memory, and configures the third process with the read-only permission of the first shared memory.
In this embodiment of the application, the creating module 110 is specifically configured to:
the creating module 110 configures, through the first process, the second process with the read-write permission of the first shared memory through the access control list ACL, and configures the third process with the read-only permission of the first shared memory.
In this embodiment of the application, before the second process writes the first data into the first mapping region, the apparatus further includes: a distribution module 130 and an acquisition module 140;
the allocating module 130 is configured to allocate a first bank of memory through the second process, where the first bank of memory is used to store control information of the second process.
The obtaining module 140 is configured to obtain, by the second process, 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 region, the allocating module 130 is further configured to allocate, by the third process, a second heap memory, where the second heap memory is used to store control information of the third process.
The obtaining module 140 is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In this embodiment, 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.
In this embodiment of the application, before the second process writes the first data into the first mapping region, the apparatus further includes a sending module 160;
the allocating module 130 is configured to allocate a third heap memory through the first process, where the third heap memory is used to store control information of the second process.
The obtaining module 140 is configured to receive, by a first process, a first information obtaining request sent by a second process, where the first information obtaining request is used to request to obtain control information of the second process from a third heap memory.
A sending module 160, configured to send, by the first process according to the first get information request, the control information of the second process to the second process, so that the second process writes the first data in the first mapping area according to the control information of the second process.
Before the third process maps the first data from the first shared memory to the second mapping region, the allocating module 130 is further configured to allocate, by the third process, a second heap memory, where the second heap memory is used to store control information of the third process.
The obtaining module 140 is further configured to obtain, by the third process, control information of the third process from the second heap memory, so that the third process maps the first data from the first shared memory to the second mapping area according to the control information of the third process.
In this embodiment, the third heap memory and the first mapping region are different memories, the first mapping region is used to store the shared memory information of the second process, the second heap memory and the second mapping region are different memories, and the second mapping region is used to store the shared memory information of the third process.
In this embodiment of the application, before the second process writes the first data into the first mapping region, the allocating module 130 is configured to allocate, by the second process, a first heap memory, where the first heap memory is used to store control information of the second process.
The obtaining module 140 is configured to obtain, by the second process, 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 region, the apparatus further includes a receiving module 150;
the allocating module 130 is configured to allocate a third heap memory through the first process, where the third heap memory is used to store control information of the third process.
The receiving module 150 is configured to receive, by the first process, a second information obtaining request sent by a third process, where the second information obtaining request is used to request to obtain control information of the third process from a third heap memory.
The sending module 160 is configured to send the control information of the third process to the third process according to the second information obtaining request through the first 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.
In this embodiment, 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.
In this embodiment of the present application, the first process includes a detection thread, and the apparatus further includes a detection module 170, where the detection module 170 is specifically configured to:
the detection module 170 detects whether the second process or the third process is reset through the detection thread, and when detecting that the second process or the third process is reset, the detection module 170 notifies the second process and a process which is not reset in the third process to terminate communication through the detection thread and releases the first shared memory.
In this embodiment of the present application, the first process includes recovering the shared memory thread, where the recovering the shared memory thread is a thread with the highest running level in the threads included in the first process, and the apparatus further includes a recovering module 180, where the recovering module 180 is specifically configured to:
when the first process is restarted, the recovery module 180 recovers the first shared memory by recovering the shared memory thread.
In this embodiment of the application, the first shared memory includes a destroyer queue, and the communication module 120 is specifically configured to:
the communication module 120 maps the first data from the first mapping region to the destroyer queue through the second process, and then maps the first data from the destroyer queue to the second mapping region through the third process.
In this embodiment of the application, the creating module 110 is further configured to create, through the first process, a second shared memory between the second process and the third process, where the first process has a read-write permission for the second shared memory, the third process has a read-write permission for the second shared memory, the second process has a read-only permission for the second shared memory, a third mapping relationship is provided between the third mapping region and the second shared memory, a fourth mapping relationship is provided between the fourth mapping region and the second shared memory, the third mapping region is a region in the virtual address space of the third process, which establishes a mapping relationship with the second shared memory, and the fourth mapping region is a region in the virtual address space of the second process, which establishes a mapping relationship with the second shared memory;
the communication module 120 is further configured to write the second data into the third mapping area through the third process, map the second data from the third mapping area to the second shared memory according to the third mapping relationship, and map the second data from the second shared memory to the fourth mapping area according to the fourth mapping relationship by the second process, so that unidirectional communication from the third process to the second process is achieved.
Specifically, the detailed implementation of the inter-process communication apparatus 100 to perform various operations may refer to the description in the related contents in the above method embodiments, and for the sake of brevity of the description, no further description is provided here.
It should be understood that the interprocess communication apparatus 100 is only one example provided for the embodiments of the present application and that the interprocess communication apparatus 100 may have more or less components than those shown in fig. 9, may combine two or more components, or may have a different configuration implementation of the components.
An embodiment of the present application further provides another interprocess communication apparatus, referring to fig. 10, fig. 10 is a schematic structural diagram of an interprocess communication apparatus 200 provided by the present application, where the apparatus 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 by a bus 240. Wherein the content of the first and second substances,
processor 210 may include one or more general-purpose processors, which may be any type of device capable of processing electronic instructions, including a CPU, microprocessor, microcontroller, host processor, controller, and Application Specific Integrated Circuit (ASIC), among others. Processor 210 reads program code stored in memory 220 and, in cooperation with communication interface 230, performs some or all of the steps of the method performed by interprocess communication device 100 in the embodiments described above.
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 using a wireless local area network interface) for communicating with other computing nodes or devices. When the communication interface 230 is a wired interface, the communication interface 230 may adopt a protocol family over a 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, a distributed protocol, and the like.
Memory 220 may store program codes as well as program data. Wherein the program code includes: code of the creation module 110, code of the communication module 120, etc., and program data including: first data, second data, control information for a second process, control information for a third process, and so on. In practical applications, memory 220 may include volatile memory (volatile memory), such as Random Access Memory (RAM); the memory may also include a non-volatile memory (non-volatile memory), such as a read-only memory (ROM), a flash memory (flash memory), a Hard Disk Drive (HDD), or a solid-state drive (SSD) memory, which may also include a combination of the above kinds of memories.
The bus 240 may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The bus 240 may be divided into an address bus, a data bus, a control bus, and the like. For ease of illustration, only one thick line is shown in FIG. 10, but this is not intended to represent only one bus or type of bus.
It should be understood that the interprocess communication apparatus 200 is only one example provided by the embodiments of the application and that the interprocess communication apparatus 200 may have more or less components than those shown in fig. 10, may combine two or more components, or may have a different configuration implementation of the components.
The embodiments of the present application further provide a computer storage medium, where instructions are stored in the computer storage medium, and when the instructions are run on a processor, the method steps in the foregoing method embodiments may be implemented, and specific implementation of the processor of the computer storage medium in executing the method steps may refer to specific operations in the foregoing method embodiments, and details are not described herein again.
The embodiments of the present application further provide 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 described in the above method embodiments.
In the above embodiments, all or part may be implemented by software, hardware, or any combination thereof. When implemented in software, may 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 loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the application to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored on a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website, computer, server, or data center to another website, computer, server, or data center via wire (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., Digital Versatile Disk (DVD)), or a semiconductor medium (e.g., SSD), among others.
The steps in the method of the embodiment of the application can be sequentially adjusted, combined or deleted according to actual needs; the modules in the device of the embodiment of the application can be divided, combined or deleted according to actual needs.
The foregoing detailed description of the embodiments of the present application has been presented to illustrate the principles and implementations of the present application, and the above description of the embodiments is only provided to help understand the method and the core concept of the present application; meanwhile, for a person skilled in the art, according to the idea of the present application, there may be variations in the specific embodiments and the application scope, and in summary, the content of the present specification should not be construed as a limitation to the present application.

Claims (27)

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

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010262408.1A CN113495795A (en) 2020-04-03 2020-04-03 Inter-process communication method and related equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010262408.1A CN113495795A (en) 2020-04-03 2020-04-03 Inter-process communication method and related equipment

Publications (1)

Publication Number Publication Date
CN113495795A true CN113495795A (en) 2021-10-12

Family

ID=77995204

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010262408.1A Pending CN113495795A (en) 2020-04-03 2020-04-03 Inter-process communication method and related equipment

Country Status (1)

Country Link
CN (1) CN113495795A (en)

Cited By (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023093675A1 (en) * 2021-11-25 2023-06-01 北京字节跳动网络技术有限公司 Bidirectional communication method, apparatus and device, and medium
WO2023134411A1 (en) * 2022-01-17 2023-07-20 闻泰通讯股份有限公司 Information transmission method, terminal apparatus, and storage medium
WO2023160253A1 (en) * 2022-02-25 2023-08-31 京东方科技集团股份有限公司 Service processing method and apparatus
CN116881026A (en) * 2023-09-06 2023-10-13 成都谐盈科技有限公司 Shared memory transmission system and method based on CORBA middleware
WO2023198103A1 (en) * 2022-04-14 2023-10-19 华为技术有限公司 Inter-process communication method and electronic device
WO2024088194A1 (en) * 2022-10-28 2024-05-02 华为技术有限公司 Communication method and apparatus, and intelligent driving device

Cited By (7)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2023093675A1 (en) * 2021-11-25 2023-06-01 北京字节跳动网络技术有限公司 Bidirectional communication method, apparatus and device, and medium
WO2023134411A1 (en) * 2022-01-17 2023-07-20 闻泰通讯股份有限公司 Information transmission method, terminal apparatus, and storage medium
WO2023160253A1 (en) * 2022-02-25 2023-08-31 京东方科技集团股份有限公司 Service processing method and apparatus
WO2023198103A1 (en) * 2022-04-14 2023-10-19 华为技术有限公司 Inter-process communication method and electronic device
WO2024088194A1 (en) * 2022-10-28 2024-05-02 华为技术有限公司 Communication method and apparatus, and intelligent driving device
CN116881026A (en) * 2023-09-06 2023-10-13 成都谐盈科技有限公司 Shared memory transmission system and method based on CORBA middleware
CN116881026B (en) * 2023-09-06 2023-11-21 成都谐盈科技有限公司 Shared memory transmission system and method based on CORBA middleware

Similar Documents

Publication Publication Date Title
CN113495795A (en) Inter-process communication method and related equipment
US9519795B2 (en) Interconnect partition binding API, allocation and management of application-specific partitions
US6647423B2 (en) Direct message transfer between distributed processes
JP6328134B2 (en) Method, apparatus, and program for performing communication channel failover in a clustered computer system
US9218299B2 (en) Recovering data segment contents in a distributed shared memory
WO2021217529A1 (en) Method and system for inter-process communication
CN112115090A (en) Multi-protocol support for transactions
EP3217285B1 (en) Transmitting data
US7571440B2 (en) System and method for emulating network communications between partitions of a computer system
US10104005B2 (en) Data buffering
KR20010087404A (en) Interrupt architecture for a non-uniform memory access data processing system
US10754774B2 (en) Buffer manager
US20150370582A1 (en) At least one user space resident interface between at least one user space resident virtual appliance and at least one virtual data plane
US20160350260A1 (en) Dynamic Non-Uniform Memory Architecture (NUMA) Locality for Remote Direct Memory Access (RDMA) Applications
WO2021057489A1 (en) Method and device for virtual machine memory management
CN114281484B (en) Data transmission method, device, equipment and storage medium
WO2022021896A1 (en) Interprocess communication method and apparatus
CN111897666A (en) Method, device and system for communication among multiple processes
CN114371811A (en) Method, electronic device and computer program product for storage management
CN115203110A (en) PCIe function and method of operating the same
US9424114B2 (en) Input/output (I/O) processing via a page fault doorbell mechanism
US8782642B2 (en) Data processing system with data transmit capability
CN116107697B (en) Method and system for communication between different operating systems
KR20050080704A (en) Apparatus and method of inter processor communication
JP3375649B2 (en) Parallel computer

Legal Events

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