Inter-process communication method and device
By using the combination of user-mode shared memory and received message queues in an embedded system, the problems of low inter-process communication efficiency and high resource consumption are solved, and efficient and accurate inter-process communication is achieved.
Patent Information
- Application Number
- CN202510866914.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-26
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2045-06-26
AI Technical Summary
The existing inter-process communication methods have problems such as inefficient efficiency, high resource consumption, data inaccuracy and insufficient synchronous asynchronous mechanism in embedded systems, especially when the netconf southbound control process needs frequent communication.
The combination of user-state shared memory and received message queue is adopted. By creating a received message queue in the process list and using shared memory for atomic operations, data accuracy and synchronization are ensured, and frequent read and write data in the kernel state are reduced.
It improves the efficiency of inter-process communication, reduces CPU resource consumption, ensures data accuracy and synchronization, and achieves stable and efficient communication.
Smart Images

Figure CN120371570A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of process communication technologies, and in particular, to a method and device for inter-process communication. Background Art
[0002] In embedded-related systems, there are scenarios of inter-process communication. IPC communication methods include signals, pipes, semaphores, shared memory, (inter-process) queues, SOCKETs, etc. These communication methods have their respective disadvantages in the process of implementing inter-process communication. For example, although the shared memory method is fast, it does not have its own synchronous and asynchronous mechanisms, and cannot ensure the correctness of data when used by multiple processes; the message queue method does not have the problem of reading order, but it will consume additional CPU resources when there are many processes, affecting efficiency. When the data volume is large, the transmission efficiency is low, and it will occupy additional memory; in the prior art, the netconf southbound management and control process needs to obtain a large amount of data from other processes and perform inter-process communication frequently, which consumes a large amount of resources. Summary of the Invention
[0003] Embodiments of this specification provide a method and device for inter-process communication to solve the technical problem of how to improve the effect and efficiency of inter-process communication.
[0004] To solve the above technical problem, the embodiments of this specification provide the following technical solutions: Embodiments of this specification provide a method for inter-process communication, and the method includes: For any process, create a process list in this process; wherein, the process list is generated according to the process information of the target device; Create a receiving message queue corresponding to this process, and extend the receiving message queue to the process list of this process; When this process needs to communicate with a target process, generate a message structure according to the task that the target process needs to execute, and send the message structure to the receiving message queue corresponding to the target process, so that the target process executes the task according to the receiving message queue; Wherein, the target process executing the task according to the receiving message queue includes: Determine the message structure that needs to be processed according to the receiving message queue, determine the target operation that needs to be executed according to the message structure that needs to be processed, and determine the data that needs to be operated from the shared memory, and execute the target operation on the data.
[0005] Optionally, before generating a message structure according to the task that the target process needs to execute, the method further includes: Link this process to the shared memory shared by all processes.
[0006] Optionally, before sending the message structure to the receive message queue corresponding to the target process, the method further includes: Put the data that needs to be processed by the target process into the shared memory, and the message structure includes the location information of the data in the shared memory.
[0007] Optionally, the method further includes: Determine the sending interface of the message structure according to whether a reply message is required to be returned by the target process; And / or Determine whether the message structure reaches the receive message queue corresponding to the target process within a specified time; If not, resend the message structure to the receive message queue corresponding to the target process.
[0008] Another inter-process communication method provided by an embodiment of this specification, the method includes: For any process, establish a process list in this process; wherein, the process list is generated according to the process information of the target device; Create a receive message queue corresponding to this process, and extend the receive message queue to the process list of this process; wherein, the receive message queue is used to queue the message structures sent by other processes; Determine the message structure to be processed according to the receive message queue, determine the target operation to be executed according to the message structure to be processed, and determine the data to be operated on from the shared memory, and execute the target operation on the data.
[0009] Optionally, the method further includes: After receiving the message structure, view the maximum sequence identifier of the message structures that have not been processed yet, and then assign the next sequence identifier to the newly received message structure.
[0010] Optionally, determining the message structure to be processed according to the receive message queue includes: Use the message structure corresponding to the sequence identifier ranked first in the current receive message queue as the message structure to be processed; After processing each message structure, update the receive message queue, and continue to use the message structure corresponding to the sequence identifier ranked first in the updated receive message queue as the message structure to be processed.
[0011] Optionally, the method further includes: Start the receive message thread corresponding to this process, and the receive message thread is used to receive the message structures sent by other processes and store the message structures in the receive message queue of this process.
[0012] An embodiment of this specification provides an inter - process communication device, and the device includes: A first communication preparation module, configured to create a process list for any process, create a receiving message queue corresponding to the process, and extend the receiving message queue to the process list of the process; wherein, the process list is generated according to the process information of the target device; A first communication execution module, configured to generate a message structure according to the task that the target process needs to execute when the process needs to communicate with the target process, and send the message structure to the receiving message queue corresponding to the target process, so that the target process executes the task according to the receiving message queue; Wherein, the target process executing the task according to the receiving message queue includes: Determining the message structure to be processed according to the receiving message queue, determining the target operation to be executed according to the message structure to be processed, and determining the data to be operated from the shared memory, and performing the target operation on the data.
[0013] An embodiment of this specification provides another inter - process communication device, and the device includes: A second communication preparation module, configured to create a process list for any process, create a receiving message queue corresponding to the process, and extend the receiving message queue to the process list of the process; wherein, the receiving message queue is used to arrange the message structures sent by other processes; wherein, the process list is generated according to the process information of the target device; A second communication execution module, configured to determine the message structure to be processed according to the receiving message queue, determine the target operation to be executed according to the message structure to be processed, and determine the data to be operated from the shared memory, and perform the target operation on the data.
[0014] The above - mentioned at least one technical solution adopted in the embodiments of this specification can achieve the following beneficial effects: The embodiments of this specification discard the data structure in the kernel, use the shared memory in the user state, no longer require frequent data reading and writing between the user state and the kernel state, reduce the consumption of CPU resources, and thus improve the efficiency of inter - process communication. Moreover, the embodiments of this specification use the method of controlling the shared memory with the receiving message queue to make the operation on the shared memory an atomic operation, ensuring that only one process can read and write the data in the shared memory at the same time, and ensuring the accuracy of the data. Description of the Drawings
[0015] To more clearly illustrate the technical solutions in the embodiments of this specification or the prior art, the following will briefly describe the drawings required for use in the description of the embodiments of this specification or the prior art. Obviously, the following only illustrates the drawings required for some embodiments of this application. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0016] Figure 1 It is a schematic flowchart of an inter-process communication method provided by the first embodiment of this specification.
[0017] Figure 2 It is a schematic diagram of the inter-process communication process in the first embodiment of this specification.
[0018] Figure 3 It is a schematic diagram of the message structure in the first embodiment of this specification.
[0019] Figure 4 It is a schematic flowchart of an inter-process communication method provided by the second embodiment of this specification.
[0020] Figure 5 It is a schematic structural diagram of an inter-process communication device provided by the third embodiment of this specification.
[0021] Figure 6 It is a schematic structural diagram of an inter-process communication device provided by the fourth embodiment of this specification. Detailed implementation manners
[0022] To enable those skilled in the art to better understand the technical solutions in this specification, the following will clearly and completely describe the technical solutions in the embodiments of this specification in conjunction with the drawings. Obviously, the embodiments involved in the detailed implementation manners are only some embodiments of this application, rather than all embodiments. All other embodiments obtained by those of ordinary skill in the art without creative efforts based on the embodiments in the detailed implementation manners shall fall within the protection scope of this application.
[0023] The first embodiment of this specification (hereinafter referred to as "Embodiment 1") provides an inter-process communication method. The execution subject of Embodiment 1 includes but is not limited to a terminal, a server, an operating system, or an application program. That is, the execution subject can be various and can be set, used, or transformed according to needs. In addition, a third-party application program can also assist the execution subject in executing Embodiment 1. For example, the server can execute the data processing method in Embodiment 1, and a corresponding application program can be installed on the terminal (which can be held by the user). Data can be transmitted between the terminal or the application program and the server to assist the server in executing the data processing method in Embodiment 1.
[0024] Reference Figure 1 and Figure 2 The inter - process communication method provided by Embodiment 1 includes: S101: For any process, establish a process list in this process; wherein, the process list is generated according to the process information of the target device; After the target device (including but not limited to an embedded communication device) is started, each process starts to run. The following is a description from the perspective of a single process: For any process, a process list can be established in this process. Among them, the process list is generated according to the process information of the target device.
[0025] Specifically, the process information of the target device can be obtained, including global resource information such as a process table containing all processes and the process ID of each process. Based on the process table, a table formed by the process IDs of each process is saved, and the table formed by the process IDs is the process list. The specific form of the process list is not limited, and the main information it contains is each process and the process ID of each process.
[0026] Copy a copy of the process list in each process, which also realizes establishing a process list in any process. These contents can be executed by the first process started by the target device.
[0027] S103: Create a receive message queue corresponding to this process, and extend the receive message queue to the process list of this process; In Embodiment 1, for any process, create a receive message queue corresponding to this process, and extend the receive message queue to the process list of this process. The receive message queue can have multiple positions or spaces, and these multiple positions or spaces form a queue - like structure. Of course, the specific structure of the receive message queue is not limited.
[0028] The receive message queue corresponding to this process is bound to the process ID of this process. For this process, a process list has been established in this process before, and the receive message queue corresponding to this process is bound to the process ID of this process, so that (through the process ID of this process) the receive message queue of this process is extended to the process list of this process.
[0029] After the receive message queue is extended, the process list of this process exists independently and is maintained by this process itself, and the process list of this process can be visible to each process.
[0030] In addition, the receive message queue of the process can be assigned a queue identifier, and the identifiers of the receive message queues of different processes are different. Before the receive message queue is actually used to receive various types of messages, some preprocessing operations can be performed on the receive message queue, such as initializing the receive message queue. These preprocessing operations can be carried out as needed, and the first embodiment is not limited.
[0031] In the first embodiment, for any process, a thread corresponding to the process can be started. The thread is used to receive various types of messages sent by other processes and store the received various types of messages in the receive message queue of the process. Hereinafter, the thread will be referred to as the receive message thread.
[0032] For any process, the process can be linked to the shared memory shared by all processes, and the shared memory is visible to all processes. Specifically, after the target device is started, a certain process (such as the first process started by the aforementioned target device) can obtain internal resources such as the heap area, stack area, and registers of the target device, and then determine the shared memory shared by all processes. After each process is started, it can be linked to the shared memory respectively.
[0033] The above content, including the expansion of the receive message queue and the process list, the preprocessing operation of the receive message queue, starting the receive message thread, and the process linking to the shared memory, can be regarded as the preparatory work for inter-process communication. In actual scenarios, these preparatory works can be applied as needed to perform inter-process communication work.
[0034] S105: When the process needs to communicate with the target process, a message structure is generated according to the task that the target process needs to execute, and the message structure is sent to the receive message queue corresponding to the target process, so that the target process executes the task according to the receive message queue; wherein, the target process executing the task according to the receive message queue includes: determining the message structure to be processed according to the receive message queue, determining the target operation to be executed according to the message structure to be processed, and determining the data to be operated from the shared memory, and performing the target operation on the data.
[0035] For any process, when the process needs to communicate with the target process (the target process is the process with which the process wants to communicate or send messages) and send messages to other processes, a message structure can be generated according to the task that the target process needs to execute. Among them, the task that the target process needs to execute is used to indicate what data the target process needs to perform what operations on (hereinafter referred to as the target operation).
[0036] Before generating the message structure, the sending process can prepare the data that the target process needs to process, apply for shared memory, and put the data that the target process needs to process into the shared memory.
[0037] The message structure may include a message header and a message body. Among them, the content of the message header includes but is not limited to: (1) The id corresponding to the receiving process (i.e., the target process); (2) The id corresponding to the sending process (i.e., the process that sends the message structure, which is any of the above processes); (3) The length of the IPC message; (4) The number of IPC messages; (5) The IPC message type; (6) The IPC message subtype; (7) The IPC message opcode (representing the target operation that the receiving process is to perform on the data); (8) The priority.
[0038] The content of the message body includes but is not limited to the location information of the data to be processed by the target process in the shared memory, so that the target process can locate this data from the shared memory according to the message body. For example, the data to be processed by the target process is copied into the shared memory, and an offset data address offset value is parsed. This offset data address offset value is included in the message body, and the data in the shared memory can be located through the offset data address offset value.
[0039] For the specific content of the message header and the message body, refer to Figure 3 . Figure 3 Only for example, in actual situations, the content or form of the message header or the message body can be set or changed according to needs.
[0040] In the first embodiment, the sending process assembles the message header and the message body into a message structure, and sends the message structure to the receiving message queue corresponding to the target process, so that the target process executes the "task that needs to be executed by the target process" according to the receiving message queue.
[0041] In addition, in actual situations, the sending interface of the message structure and the receiving interface of the target process for the message structure can be determined according to different requirements. For example, the sending process can determine the sending interface of the message structure and the receiving interface of the target process for the message structure according to whether the target process needs to return a response message. According to the different sending interfaces and receiving interfaces called and the input parameters, the corresponding functions are implemented.
[0042] For example, to implement the synchronization function (i.e., synchronization between the sending process and the receiving process), when the sending process calls the reply method and needs to wait for the receiving process to return an acknowledgment, the corresponding sending interface and the corresponding receiving interface can be selected; or, to implement the asynchronous function, when the sending process calls a non-reply method and the sending process is regarded as sending a notification to the receiving process without waiting for the receiving process to return an acknowledgment, the corresponding sending interface and the corresponding receiving interface can be selected; or, after each message structure is sent, it can be determined whether the message structure reaches the receiving message queue corresponding to the target process within the agreed time; if not, the sending process resends the message structure to the receiving message queue corresponding to the target process, and the resending of the message structure can be repeated multiple times until the message structure is sent to the receiving message queue of the receiving process within the agreed time. Specifically, the agreed time can be reflected by the timeout parameter of the sending process. In this way, the timeout retransmission mechanism is implemented using the timeout parameter, reducing the impact of accidental factors (such as device lag) on the communication result.
[0043] The following describes how to receive the message structure from the perspective of the target thread and execute the task according to the received message in the receiving message queue: As mentioned above, the receiving message queue can receive various types of messages, and each type of message includes a message structure. The receiving message queue is used to arrange the message structures sent by other processes.
[0044] Among them, the message structure sent by the sending process is received by the receiving message thread of the target process, and a corresponding sequence identifier is assigned to the message structure. The sequence identifier can be a sequence number or a serial number or other forms, which is used to represent the order or serial number of the message structure.
[0045] On the one hand, after a new message structure is received, check the maximum sequence identifier of the message structures that have not been processed by the current target process, and then assign the next sequence identifier to the newly received message structure. For example, if the maximum sequence identifier of the message structures that have not been processed by the current target process is p, the sequence identifier of the newly received message structure is p + 1; on the other hand, the target process determines the message structures that need to be processed according to its corresponding receiving message queue. Specifically, when the target process processes the message structure, it is the message structure corresponding to the first sequence identifier in the message queue that is used as the message structure to be processed. Assume that message structure A is the message structure to be processed. After message structure A is processed, the sequence identifier corresponding to message structure A in the receiving message queue will be released, and the message structure corresponding to the next sequence identifier will move forward and be updated to the just-released sequence identifier.
[0046] Taking the above two aspects into consideration, the message structure processed by the target process is actually always the message structure corresponding to the first sequence identifier in the receive message queue corresponding to the target process. This is because after the target device is started, the first message structure received by the target process is assigned to the first sequence identifier (assuming the sequence identifier is in numerical form and the first sequence identifier is 1) of the receive message queue corresponding to the target process, and the target process will process the message structure with the sequence identifier 1. When receiving the next message structure, the following situations occur: Situation 1: If the message structure with the sequence identifier 1 has been processed, the sequence identifier 1 has been released. The next received message structure is still assigned the sequence identifier 1 and is processed by the target process; Situation 2: If the message structure with the sequence identifier 1 has not been processed yet, the sequence identifier of the next received message structure is 2. When the message structure with the sequence identifier 1 is processed, the sequence identifier 1 is released, and the ranking of the original message structure with the sequence identifier 2 moves forward one position, that is, the sequence identifier of the original message structure with the sequence identifier 2 is updated to 1 and is processed by the target process.
[0047] And so on. Initially, the target process takes the message structure corresponding to the first sequence identifier in the current receive message queue as the message structure to be processed. After receiving a new message structure, it checks the maximum sequence identifier of the message structures that the current target process has not processed yet, and then assigns the next sequence identifier to the newly received message structure. Whenever the target thread processes a message structure, it updates the receive message queue, traverses the updated receive message queue, and continues to take the message structure corresponding to the first sequence identifier in the updated receive message queue as the message structure to be processed. Among them, whenever the target thread processes a message structure and updates the receive message queue, it means that the sequence identifiers of the current message structures to be processed are all updated (that is, the sequence identifiers of the current message structures to be processed all move forward one position).
[0048] In this way, the target process determines the message structure to be processed according to the receive message queue, and ensures that the target process always processes the message structure corresponding to the first sequence identifier in the receive message queue.
[0049] Next, it is described how the target thread processes the message structure and how to execute the task: The target thread determines the target operation to be executed according to the message structure to be processed, and determines the data to be operated on from the shared memory. Specifically, the target process parses the message structure to obtain the message header and the message body. The target process can determine the target operation to be executed according to the message header (see the above description of the message header and Figure 3), the target process can locate the data to be operated on from the shared memory according to the message body, and then perform the target operation on the located data, that is, execute the task that needs to be executed by the target process.
[0050] In the first embodiment, the target process determines whether to return a response message to the process that sends the message structure according to the sending interface used by the process that sends the message structure.
[0051] If a response message needs to be returned, the target process generates a message structure according to the response message to be returned, and sends the message structure to the corresponding process for processing by the process that receives the message structure. The specific content has been described above and will not be elaborated here.
[0052] It should be noted that if the target process only sends a response message to other processes, generally, the target process does not need to store data in the shared memory, but only needs to generate a message structure according to the response message to be returned, and send the message structure to the corresponding process for processing by the process that receives the message structure.
[0053] In the first embodiment, the shared memory only allows a single process to read and write data. When a certain process processes the message structure and reads and writes the data in the shared memory, other processes that need to read and write the data in the shared memory can wait. After the current process finishes reading and writing the data, the next process can perform the reading and writing of the data in the shared memory. Specifically, the sending processes and the receiving processes can also have an order, and the order of the sending processes and the receiving processes can be determined according to the time of applying to write data to the shared memory (sending process) and the time of applying to read data from the shared memory (receiving process).
[0054] The following further illustrates the first embodiment through a specific example: Suppose the sending process is an alarm process and the target process is a terminal process. Then the alarm process performs the following: Prepare data: The alarm process prepares the data that needs to be processed by the terminal process and assembles the data that needs to be processed by the terminal process into a structure.
[0055] Apply for shared memory and fill data into the shared memory: The alarm process applies for a space in the shared memory it is linked to, copies the data that needs to be processed by the terminal process to this space, and parses out an offset data address offset value.
[0056] Assembly message structure: Generate a message structure according to the tasks to be executed by the terminal process. The message structure includes a message header and a message body. In the message header, it is stated that the receiving process is the terminal process, the sending process is the alarm process, the data length, the quantity type and subtype are determined by the data already placed in the shared memory, the operation code is selected as needed (for example, the EVENT event notification identifier can be used in this example), and the corresponding priority is selected according to the urgency of the event. Record the offset data address offset value in the message body.
[0057] Select API as needed: Determine the sending interface of the message structure and the receiving interface of the terminal process according to whether the terminal process needs to return a response message.
[0058] Send the message structure: Send the message structure to the receiving message queue of the terminal process and wait for the terminal process to process the message structure.
[0059] The terminal process performs the following: Wait to receive a message; Discover and receive the message structure and assign a sequence identifier; Loop through its corresponding receiving message queue and process the message structure with the highest priority.
[0060] As mentioned above, after the message structure sent by the alarm process is received, it is assigned a sequence identifier, but it may not be the message structure with the highest priority currently, so it needs to wait for the receiving process to process it. It is possible that after a certain update of the receiving message queue, the message structure sent by the alarm process becomes the message structure with the highest priority and is processed by the terminal process.
[0061] The specific processing process includes: The terminal process parses the message structure to obtain the contents included in the message header and the message body, including information such as the sending process id, length, quantity, type, subtype, operation code, priority, etc. in the message header and the address offset information in the message body. Among them, it is determined that the message structure is sent by the alarm process through the sending process id, and it can be determined that the EVENT operation (i.e., the target operation) needs to be performed on the data through the operation code.
[0062] The pointer in the message body points to the shared memory and is mapped to a specified area in the memory through the offset in the message body. Correspondingly, the terminal process can map a section of data from the shared memory through the offset data address offset value in the message body and the shared memory address, that is, the data to be processed by the terminal process packaged and placed in the shared memory by the alarm process.
[0063] The receiving process performs the target operation on the data mapped from the shared memory, releases the shared memory, and determines whether to return a response message to the sending process according to the sending API used by the alarm process.
[0064] The first embodiment can achieve the following beneficial effects: In the message queue method used in existing process communication, the queue used is essentially a data structure composed of a linked list provided by the kernel. Writing data to the message queue is actually inserting a new node into this data structure; reading data from the message queue is actually reading and deleting a node from this data structure. When communicating, the sending process defines the message to be sent in the message buffer of its own process, and then connects the message structure to the linked list in the kernel. The receiving process matches the corresponding node from the linked list in the kernel (i.e., the message queue), obtains the message, stores it in the message buffer of the receiving process, and deletes the node. In this process, frequent data copying occurs between the user mode and the kernel mode, and frequent read and write operations on the kernel mode will consume additional CPU resources.
[0065] In the first embodiment, the data structure in the kernel is discarded, and shared memory in the user mode is used. There is no longer a need for frequent data reading and writing between the user mode and the kernel mode, reducing the consumption of CPU resources, and thus improving the efficiency of inter-process communication.
[0066] In the existing process communication method implemented through shared memory, there is no synchronization and mutual exclusion mechanism in the shared memory. Therefore, when multiple processes operate on the shared memory simultaneously, there is no mechanism to prevent another process from reading and writing the shared memory before the first process finishes reading and writing the shared memory.
[0067] In the first embodiment, the method of using the receiving message queue to control the shared memory makes the operation on the shared memory an atomic operation, ensuring that only one process can read and write the data in the shared memory at the same time, ensuring the accuracy of the data, and improving the effect of inter-process communication.
[0068] Through the first embodiment, when data is interacted, the synchronization of using the shared memory can be ensured through the characteristics of the receiving message queue to ensure the correctness of the data, and at the same time, the operation on the kernel mode is no longer involved through the shared memory method, reducing the consumption of resources such as CPU and memory, and improving the efficiency of inter-process communication in the embedded system.
[0069] The first embodiment can stably and efficiently meet the requirements of inter-process communication, and is easy to maintain and not prone to errors.
[0070] The first embodiment realizes the on-demand use of synchronous and asynchronous functions during the communication process, adds a timeout retransmission mechanism during the communication process, and improves the efficiency and accuracy during inter-process communication.
[0071] The second embodiment of this specification (hereinafter referred to as "Embodiment 2") provides an inter - process communication method. The execution subject of Embodiment 2 includes, but is not limited to, a terminal, a server, an operating system, or an application program. That is, the execution subject can be diverse and can be set, used, or transformed according to needs. In addition, a third - party application program can assist the execution subject in executing Embodiment 2. For example, the data - processing method in Embodiment 2 can be executed by a server, and a corresponding application program can be installed on a terminal (which can be held by a user). Data can be transmitted between the terminal or the application program and the server, thereby assisting the server in executing the data - processing method in Embodiment 2.
[0072] Refer to Figure 4 , the inter - process communication method provided by Embodiment 2 includes: S202: For any process, create a process list in this process; wherein, the process list is generated according to the process information of the target device; For details, refer to Embodiment 1.
[0073] S204: Create a receive message queue corresponding to this process, and extend the receive message queue to the process list of this process; wherein, the receive message queue is used to queue message structures sent by other processes; For details, refer to Embodiment 1.
[0074] S206: Determine the message structure to be processed according to the receive message queue, determine the target operation to be executed according to the message structure to be processed, and determine the data to be operated on from the shared memory, and execute the target operation on the data.
[0075] For details, refer to Embodiment 1.
[0076] In Embodiment 2, after receiving a message structure, view the maximum sequence identifier of the message structures that have not been processed yet, and then assign the next sequence identifier to the newly received message structure. For details, refer to Embodiment 1.
[0077] In Embodiment 2, determining the message structure to be processed according to the receive message queue may include: Regarding the message structure corresponding to the sequence identifier ranked first in the current receive message queue as the message structure to be processed; After processing each message structure, update the receive message queue, and continue to regard the message structure corresponding to the sequence identifier ranked first in the updated receive message queue as the message structure to be processed.
[0078] For details, refer to Embodiment 1.
[0079] In the second embodiment, for any process, a receiving message thread corresponding to the process is started. The receiving message thread is used to receive a message structure sent by other processes and store the message structure in the receiving message queue of the process. For details, refer to the first embodiment.
[0080] Contents not described in detail in the second embodiment can be implemented with reference to the first embodiment.
[0081] Since a single process can be both a sending process and a receiving process, the execution subject of the first embodiment or the second embodiment can be either a single process or a target device.
[0082] The third embodiment of this specification provides an inter-process communication device corresponding to the inter-process communication method described in the first embodiment. Refer to Figure 5 , the inter-process communication device includes: A first communication preparation module 301, which is used for any process to establish a process list in the process; create a receiving message queue corresponding to the process, and extend the receiving message queue to the process list of the process; wherein, the process list is generated according to the process information of the target device; A first communication execution module 303, which is used when the process needs to communicate with a target process, generate a message structure according to the task to be executed by the target process, and send the message structure to the receiving message queue corresponding to the target process, so that the target process executes the task according to the receiving message queue; Among them, the target process executing the task according to the receiving message queue includes: Determine the message structure to be processed according to the receiving message queue, determine the target operation to be executed according to the message structure to be processed, and determine the data to be operated on from the shared memory, and execute the target operation on the data.
[0083] Optionally, the first communication preparation module 301 is further used to link the process to the shared memory shared by all processes before generating a message structure according to the task to be executed by the target process.
[0084] Optionally, the first communication preparation module 301 is further used to put the data to be processed by the target process into the shared memory before sending the message structure to the receiving message queue corresponding to the target process, and the message structure includes the positioning information of the data in the shared memory.
[0085] Optionally, the first communication execution module 303 is further used for: Determine the sending interface of the message structure according to whether the target process needs to return a response message; And / or, Determine whether the message structure arrives at the receive message queue corresponding to the target process within the agreed time; If not, resend the message structure to the receive message queue corresponding to the target process.
[0086] The fourth embodiment of this specification provides an inter-process communication device corresponding to the inter-process communication method described in the second embodiment. Refer to Figure 6 The inter-process communication device includes: A second communication preparation module 402, for any process, establish a process list for the process; create a receive message queue corresponding to the process, and extend the receive message queue to the process list of the process; wherein, the receive message queue is used to arrange message structures sent by other processes; wherein, the process list is generated according to the process information of the target device; A second communication execution module 404, for determining the message structure to be processed according to the receive message queue, determining the target operation to be executed according to the message structure to be processed, and determining the data to be operated from the shared memory, and performing the target operation on the data.
[0087] Optionally, after receiving the message structure, the second communication execution module 404 is further configured to view the maximum sequence identifier of the message structures that have not been processed yet, and then assign the next sequence identifier to the newly received message structure.
[0088] Optionally, determining the message structure to be processed according to the receive message queue includes: Regarding the message structure corresponding to the sequence identifier ranked first in the current receive message queue as the message structure to be processed; After processing each message structure, update the receive message queue, and continue to regard the message structure corresponding to the sequence identifier ranked first in the updated receive message queue as the message structure to be processed.
[0089] Optionally, the second communication execution module 404 is further configured to start the receive message thread corresponding to the process, and the receive message thread is used to receive message structures sent by other processes and store the message structures in the receive message queue of the process.
[0090] The above embodiments can be referred to or combined with each other. Modules with the same name in different embodiments can be the same module or different modules.
[0091] The above is only for the embodiments of this specification and is not intended to limit this application. For those skilled in the art, various changes and modifications can be made to this application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of this application shall be included within the scope of the claims of this application.
Claims
1. An inter-process communication method, characterized in that, The method includes: For any process, create a process list for this process; wherein, the process list is generated according to the process information of the target device; Create a receiving message queue corresponding to this process, and extend the receiving message queue to the process list of this process; When this process needs to communicate with a target process, generate a message structure according to the task that the target process needs to execute, and send the message structure to the receiving message queue corresponding to the target process, so that the target process executes the task according to the receiving message queue; Wherein, the target process executing the task according to the receiving message queue includes: Determine the message structure that needs to be processed according to the receiving message queue, determine the target operation that needs to be executed according to the message structure that needs to be processed, and determine the data that needs to be operated from the shared memory, and execute the target operation on the data.
2. The method according to claim 1, characterized in that Before generating a message structure according to the task that the target process needs to execute, the method further includes: Link this process to the shared memory shared by all processes.
3. The method according to claim 1 or 2, characterized in that, Before sending the message structure to the receiving message queue corresponding to the target process, the method further includes: Put the data that needs to be processed by the target process into the shared memory, and the message structure includes the location information of the data in the shared memory.
4. The method according to claim 1, wherein The method further includes: Determine the sending interface of the message structure according to whether the target process needs to return a reply message; And / or Determine whether the message structure reaches the receiving message queue corresponding to the target process within a specified time; If not, resend the message structure to the receiving message queue corresponding to the target process.
5. A method for inter-process communication, characterized in that, The method includes: For any process, create a process list for this process; wherein, the process list is generated according to the process information of the target device; Create a receiving message queue corresponding to this process, and extend the receiving message queue to the process list of this process; wherein, the receiving message queue is used to queue the message structures sent by other processes; Determine the message structure that needs to be processed according to the receiving message queue, determine the target operation that needs to be executed according to the message structure that needs to be processed, and determine the data that needs to be operated from the shared memory, and execute the target operation on the data.
6. The method according to claim 5, wherein The method further includes: After receiving a message structure, view the maximum sequence identifier of the message structures that have not been processed yet, and then assign the next sequence identifier to the newly received message structure.
7. The method according to claim 5 or 6, characterized in that, Determining the message structure that needs to be processed according to the receiving message queue includes: Use the message structure corresponding to the sequence identifier ranked first in the current receiving message queue as the message structure that needs to be processed; After processing each message structure, update the receiving message queue, and continue to use the message structure corresponding to the sequence identifier ranked first in the updated receiving message queue as the message structure that needs to be processed.
8. The method according to claim 5, wherein The method further includes: Start the receiving message thread corresponding to this process, and the receiving message thread is used to receive the message structures sent by other processes and store the message structures in the receiving message queue of this process.
9. An inter-process communication device, characterized in that The device includes: A first communication preparation module, which is used for any process to establish a process list for this process; create a received message queue corresponding to this process, and extend the received message queue to the process list of this process; wherein, the process list is generated according to the process information of the target device; A first communication execution module, which is used when this process needs to communicate with a target process to generate a message structure according to the task that the target process needs to execute, and send the message structure to the received message queue corresponding to the target process, so that the target process executes the task according to the received message queue; Wherein, the target process executing the task according to the received message queue includes: Determining the message structure to be processed according to the received message queue, determining the target operation to be executed according to the message structure to be processed and determining the data to be operated from the shared memory, and performing the target operation on the data.
10. An inter-process communication device, characterized in that, The device includes: A second communication preparation module, which is used for any process to establish a process list for this process; create a received message queue corresponding to this process, and extend the received message queue to the process list of this process; wherein, the received message queue is used to arrange the message structures sent by other processes; wherein, the process list is generated according to the process information of the target device; A second communication execution module, which is used to determine the message structure to be processed according to the received message queue, determine the target operation to be executed according to the message structure to be processed and determine the data to be operated from the shared memory, and perform the target operation on the data.
Citation Information
Patent Citations
Interprocess communication method and device thereof
CN101448018A
Processing method for optimization of inter-process communication of embedded operating system
CN104346229A
Multi-process coding method, system and device based on event and shared memory mechanism
CN109413432A
Method for realizing inter-process data interaction based on message queue and shared memory mode
CN112559207A
Inter-process communication method and device, equipment and medium
CN115408172A