Multi-process communication method based on client-server model

Through the multi-process communication method based on the client server model, the service ID and message manager object are used to realize two-way communication between processes, which solves the problem of message queue blocking and multiplexing, simplifies inter-process communication, and improves the flexibility of communication. performance and reliability.

CN112667419BActive Publication Date: 2024-10-22NANJING HEXING GRID TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110002461.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-01-04
Publication Date
2024-10-22
Estimated Expiration
2041-01-04

AI Technical Summary

Technical Problem

In the existing technology, when communicating between processes in embedded terminals, the message queue has blocking problems and is difficult to multiplex, resulting in complex program logic design, and it is impossible to achieve one-to-one or one-to-many or many-to-many two-way communication, synchronization Or insufficient support for asynchronous communication.

Method used

Adopt a multi-process communication method based on the client server model, create message manager objects and message slots through predefined service IDs, realize two-way communication between processes, and support synchronous or asynchronous communication. It uses socket communication to encapsulate the design and provide Blocking/non-blocking send and receive, multiplexing and timeout settings.

Benefits of technology

It simplifies the development of inter-process communication, reduces the complexity of directly using socket communication, provides more efficient multiplexing and convenient communication interfaces, supports one-to-one or one-to-many or many-to-many two-way communication, and enhances Improves communication flexibility and reliability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112667419B_ABST
    Figure CN112667419B_ABST
Patent Text Reader

Abstract

The present invention discloses a multi-process communication method based on a client-server model, comprising the following steps: (1) confirming the processes that need to communicate, and pre-defining and allocating a service ID to each process; (2) each process calls a message registration interface, creates a message manager object, and binds it to the service ID; (3) each process creates a message processing loop, receives messages at a fixed time, and responds and replies; (4) each process can call a message sending interface at any time during program execution, send a message to the target service ID, and realize inter-process communication. The present invention realizes two-way communication between multiple processes, supports one-to-one, one-to-many, or many-to-many, and supports synchronous or asynchronous communication between multiple processes. The message sending / receiving interface of the present invention is based on a client-server model, and is encapsulated and designed based on socket communication. It has the advantages of being built-in to the operating system, blocking / non-blocking sending and receiving, multiplexing, and setting a timeout when receiving.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The invention belongs to the field of embedded terminal operating system process communication, and in particular relates to a multi-process communication method based on a client-server model. Background Art

[0002] Process is a concept of the operating system. Every time a program is executed, a process is created for the operating system. During this process, resources are allocated and released. A process can be considered as an execution process of a program.

[0003] The user spaces of processes are independent of each other and generally cannot access each other. However, in many cases, processes need to communicate with each other to complete a certain function of the system. Processes coordinate their behaviors by communicating with the kernel and other processes. Generally, inter-process communication has the following application scenarios: (1) Data transmission: a process needs to send its data to another process, and the amount of data sent ranges from one byte to several megabytes; (2) Shared data: multiple processes want to operate shared data, and the modification of shared data by one process should be immediately seen by other processes; (3) Notification of events: a process needs to send a message to another or a group of processes to notify it (them) that a certain event has occurred (such as notifying the parent process when the process terminates); (4) Resource sharing: multiple processes share the same resources. To achieve this, the kernel needs to provide locks and synchronization mechanisms; (5) Process control: some processes want to completely control the execution of another process (such as the Debug process). At this time, the controlling process hopes to intercept all traps and exceptions of the other process and be able to know its state changes in a timely manner.

[0004] Basic process communication methods include shared storage, message passing, and pipe communication. Currently, Linux and other operating systems are widely used in embedded terminals. At the same time, object-oriented and other modular technologies are widely used in terminal software development and design, making inter-process communication between different functional modules in the terminal more and more frequent. Therefore, higher requirements are put forward for the inter-process communication mechanism. It is very necessary to realize efficient and reliable process communication to ensure the long-term stable operation of embedded terminals.

[0005] A message queue is a linked list of messages stored in the kernel. Each message queue is represented by a message queue identifier. A message queue is only truly deleted when the operating system is restarted or explicitly deleted. Before a process writes a message to a queue, it does not require another process to wait for the message to arrive on the queue.

[0006] Characteristics of message queues: A message queue is a linked list of messages with a specific format, stored in memory and identified by a message queue identifier; a message queue allows one or more processes to write and read messages to it; communication data between pipes and message queues is based on the first-in-first-out principle; a message queue can implement random query of messages, and messages do not have to be read in first-in-first-out order, but can also be read by message type.

[0007] Disadvantages of message queues: Message queues are built into the operating system and can be sent and received in blocking / non-blocking mode. However, there is no timeout when blocking, which affects the program logic design. Message queues cannot be multiplexed. When multiple processes send messages to each other, multiple message queues need to be established, which makes them more troublesome to use. Summary of the invention

[0008] Purpose of the invention: In view of the above problems, the present invention proposes a multi-process communication method based on a client-server model to realize two-way communication between multiple processes, supporting one-to-one, one-to-many, or many-to-many, and supporting synchronous or asynchronous communication between multiple processes.

[0009] Technical solution: To achieve the purpose of the present invention, the technical solution adopted by the present invention is: a multi-process communication method based on a client-server model, comprising the steps of:

[0010] (1) Confirm the processes that need to communicate and predefine service IDs for each process;

[0011] (2) Each process calls the message registration interface, creates a message manager object, and binds it to the service ID;

[0012] (3) Each process creates a message processing loop, receives messages at regular intervals, and responds to them;

[0013] (4) Each process can call the message sending interface at any time during program execution to send messages to the target service ID to achieve inter-process communication.

[0014] Furthermore, a process can have multiple message processing loops, so multiple service IDs are allocated and multiple registrations are performed.

[0015] Furthermore, the message registration interface includes a message manager object, a message slot, and a message management class; the message slot includes a message buffer, a service ID, a message sequence number, and a socket listening port; the message management class includes a message bulletin board, and the message bulletin board has lock protection.

[0016] Furthermore, the message registration process includes:

[0017] (2.1) Call the message management class to determine whether the service ID has been registered;

[0018] (2.2) If not registered, obtain the lock of the message bulletin board;

[0019] (2.3) Add the message announcement information of the service ID in the message bulletin board;

[0020] (2.4) Release the lock of the message board;

[0021] (2.5) Add the message slot information of the service ID in the message management class.

[0022] Furthermore, the message processing cycle includes: receiving a message; if a message is received, processing it according to the message type; and replying to the message after processing.

[0023] Furthermore, the message receiving process includes:

[0024] (3.1) Call the message management class to find the message slot corresponding to the receiver service ID;

[0025] (3.2) Determine whether there is message data in the message buffer in the message slot;

[0026] (3.3) If there is message data, obtain the message data, identify and match it through the data message frame format, then return the message data and exit the message interface;

[0027] (3.4) If there is no message data, the socket listening port data of the message slot is listened to within the receiving timeout period. If data is heard, the socket listening port data of the message slot is read, matched through data message frame format identification, and then the message data is returned and the message interface is exited; if no data is heard, a timeout is returned.

[0028] Furthermore, the message sending process includes:

[0029] (4.1) Call the message management class to find the message slot corresponding to the sender's service ID;

[0030] (4.2) Obtain the message bulletin board lock through the message management class;

[0031] (4.3) Obtain the socket listening port of the message slot of the target service ID from the message bulletin board, and search for the message announcement information of the target service ID;

[0032] (4.4) Determine whether the content of the sent message has a message header and a message tail. If not, add the message header and a message tail to the content of the sent message;

[0033] (4.5) Send a message to the socket listening port bound to the target service ID.

[0034] Beneficial effects: The message sending / receiving interface of the present invention is based on the client-server model and is encapsulated and designed on the basis of socket communication. It has the advantages of being built-in to the operating system, being able to block / non-block sending and receiving, being multiplexed, and being able to set a timeout when receiving. The multi-process communication method implemented by the present invention reduces the complexity of directly using socket communication and is more convenient in program development. BRIEF DESCRIPTION OF THE DRAWINGS

[0035] Figure 1 It is a message processing model diagram;

[0036] Figure 2 It is a message processing cycle flow chart;

[0037] Figure 3 It is a message registration flow chart;

[0038] Figure 4 It is a message sending flow chart;

[0039] Figure 5 It is a message receiving flow chart. DETAILED DESCRIPTION

[0040] The technical solution of the present invention is further described below in conjunction with the accompanying drawings and embodiments.

[0041] The multi-process communication method based on the client-server model can realize two-way communication between multiple processes, support one-to-one, one-to-many, or many-to-many, and support synchronous or asynchronous communication between multiple processes. The use of this method in software engineering practice can effectively simplify the development and implementation of process communication related functions.

[0042] The multi-process communication method based on the client-server model described in the present invention includes the following steps.

[0043] (1) Confirm the processes that need to communicate, and then predefine and assign service IDs to each process; a process can have multiple service IDs, but each service ID cannot be repeated;

[0044] (2) Each process calls the message registration interface, creates a message manager object, and binds it to the service ID. If a process has multiple service IDs, it needs to register multiple times.

[0045] When the process is running, the message registration interface is called to create a message manager object. Each message manager object has a unique ID, called the service ID, which is used by other message manager objects to find it.

[0046] (3) Each process creates a message processing loop, receives messages at regular intervals, and responds.

[0047] (4) Each process can call the message sending interface at any time during program execution to send messages to the target service ID to achieve inter-process communication.

[0048] like Figure 1 As shown in the figure, a process is supported to have one or more message processing loops. Depending on the position of the message processing loop, there are three different message processing models:

[0049] Model 1: A process has only one message processing loop, and messages are distributed from the main thread to each thread;

[0050] Model 2: Each thread of a process has its own message processing loop;

[0051] Model 3: A hybrid of the above two models, where messages are distributed from the main thread to each thread, and some threads have their own message processing loop.

[0052] The process where the target service ID is located, through the message processing loop, regularly calls the receiving message interface, obtains the message sent to itself, and responds to it. Each message processing loop corresponds to a message manager object.

[0053] like Figure 2 As shown, the message processing cycle includes:

[0054] (1) Receive messages;

[0055] (2) If a message is received, it is processed according to the message type;

[0056] Different types of business messages are distinguished by message type. Message type processing includes data transmission message processing, event notification message processing, process control message processing, and shared data message processing.

[0057] (3) After processing, the message is replied.

[0058] When registering a message, bind each message manager object to a message slot. The message slot includes a message buffer, service ID, message sequence number, and socket listening port. At the same time, define a message management class to manage the message slot. The message management class includes a message bulletin board with lock protection, which is used to record the correspondence between the registered service ID and the message slot for retrieval and search; it also has operation methods for the message slot, including adding, binding, searching, receiving, etc.

[0059] like Figure 3 As shown, the message registration process includes:

[0060] (1) Call the message management class to determine whether the service ID has been registered;

[0061] (2) If not registered, obtain the lock of the message bulletin board and the object handle of the message bulletin board;

[0062] (3) Add the message announcement information of the service ID to the message bulletin board;

[0063] (4) Release the lock of the message board;

[0064] (5) Add the message slot information of the service ID in the message management class.

[0065] The message sending interface includes the sender message manager object, message type, target service ID, and message content.

[0066] like Figure 4 As shown in the figure, the message sending process includes:

[0067] (1) According to the sender message manager object, find the message slot corresponding to the sender service ID through the message management class;

[0068] (2) Obtain the message bulletin board lock through the message management class and obtain the object handle of the message bulletin board;

[0069] (3) Obtain the socket listening port of the message slot of the target service ID from the message bulletin board; search for the message announcement information of the target service ID through the object handle of the message bulletin board;

[0070] (4) Determine whether the content of the sent message has a message header and a message footer. If not, add the message header and the message footer to the content of the sent message;

[0071] (5) Send a message to the socket listening port bound to the target service ID.

[0072] The message receiving interface includes the receiver message manager object, receiving timeout, and receiving buffer.

[0073] like Figure 5 As shown, the message receiving process includes:

[0074] (1) According to the recipient's message manager object, use the search method of the message management class to find the message slot corresponding to the recipient's service ID;

[0075] (2) Determine whether there is message data in the message buffer in the message slot;

[0076] (3) If there is message data, the message data is obtained, matched by data message frame format identification, and then the message data is returned and the message interface is exited;

[0077] (4) If the message data does not exist, the socket receiving interface is called according to the socket listening port of the receiver's message slot, and an attempt is made to receive the message within the receiving timeout period.

[0078] During the timeout period, the socket listening port data of the message slot is monitored. If data is detected, the socket listening port data of the message slot is read, matched by the data message frame format, and then the message data is returned to exit the message interface. During the timeout period, the socket listening port data of the message slot is monitored. If no data is detected, a timeout is returned. The receiving message interface works in blocking mode. If no message is received, the thread calling the receiving message interface will be blocked by the operating system and give up the CPU until a message arrives or the timeout period is reached.

[0079] The multi-process method based on the client-server model includes three process communication interfaces: a message registration interface, a message sending interface, and a message receiving interface.

[0080] The send / receive message interface is designed based on the client-server model and encapsulated on the basis of socket communication. It has the following advantages: built-in operating system, blocking / non-blocking sending and receiving, multiplexing, and timeout setting when receiving. The multi-process communication method implemented by the present invention reduces the complexity of directly using socket communication and is more convenient in program development.

[0081] The specific interfaces are as follows:

[0082] Message registration interface:

[0083]

[0084] Message sending interface:

[0085]

[0086] Message receiving interface:

[0087]

[0088] The corresponding socket interface:

[0089]

[0090]

[0091]

[0092]

[0093] The following comparison results were obtained:

[0094]

[0095] The process communication method of the present invention has fewer functions and fewer function parameters than socket, and the parameters are also simpler and more convenient to use.

Claims

1. A multi-process communication method based on a client-server model, characterized in that: Includes steps: (1) Confirm the processes that need to communicate and predefine service IDs for each process; (2) Each process calls the message registration interface, creates a message manager object, and binds it to the service ID; (3) Each process creates a message processing loop, receives messages at regular intervals, and responds; The message receiving process includes: (3.1) Call the message management class to find the message slot corresponding to the receiver service ID; (3.2) Determine whether there is message data in the message buffer in the message slot; (3.3) If there is message data, obtain the message data, identify and match it through the data message frame format, then return the message data and exit the message interface; (3.4) If there is no message data, the socket listening port data of the message slot is listened to within the receiving timeout period. If data is heard, the socket listening port data of the message slot is read, matched by data message frame format identification, and then the message data is returned and the message interface is exited; if no data is heard, a timeout is returned; (4) Each process can call the message sending interface at any time during program execution to send messages to the target service ID to achieve inter-process communication.

2. The multi-process communication method based on the client-server model according to claim 1, characterized in that: A process can have multiple message processing loops, so multiple service IDs are allocated and multiple registrations are performed.

3. The multi-process communication method based on the client-server model according to claim 1, characterized in that: The message registration interface includes a message manager object, a message slot, and a message management class; the message slot includes a message buffer, a service ID, a message sequence number, and a socket listening port; the message management class includes a message bulletin board, which has lock protection.

4. The multi-process communication method based on the client-server model according to claim 3 is characterized in that: The message registration process includes: (2.1) Call the message management class to determine whether the service ID has been registered; (2.2) If not registered, obtain the lock of the message bulletin board; (2.3) Add the message announcement information of the service ID in the message bulletin board; (2.4) Release the lock of the message board; (2.5) Add the message slot information of the service ID in the message management class.

5. The multi-process communication method based on the client-server model according to claim 1, characterized in that: The message processing cycle includes: receiving a message; if a message is received, processing it according to the message type; and responding to the message after processing.

6. The multi-process communication method based on the client-server model according to claim 3 is characterized in that: The message sending process includes: (4.1) Call the message management class to find the message slot corresponding to the sender service ID; (4.2) Obtain the message bulletin board lock through the message management class; (4.3) Obtain the socket listening port of the message slot of the target service ID from the message bulletin board, and search for the message announcement information of the target service ID; (4.4) Determine whether the content of the message to be sent has a message header and a message tail. If not, add the message header and a message tail to the content of the message to be sent; (4.5) Send a message to the socket listening port bound to the target service ID.