A real-time data distribution method and device based on shared memory

By using shared memory channels among multiple processes to implement the publication and subscription of data topics, it solves the problem of insufficient data distribution performance and real-time performance among multi-process architecture applications, and achieves efficient and real-time data distribution, which is suitable for a variety of application scenarios. .

CN112463400BActive Publication Date: 2024-10-11HANGZHOU EBOYLAMP ELECTRONICS CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202011194540.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2020-10-30
Publication Date
2024-10-11
Estimated Expiration
2040-10-30

AI Technical Summary

Technical Problem

Existing data distribution methods between multi-process architecture applications have shortcomings in performance, real-time and versatility, especially in situations with high real-time requirements, such as robot motion control and autonomous driving control, which cannot meet real-time requirements. And its versatility is poor.

Method used

Adopting a real-time data distribution method based on shared memory, by establishing a shared memory channel between multiple processes, publishing and subscribing between data topics and processes is achieved, reducing the number of data copies, and supporting one-to-one, one-to-many and many-to-many. Data distribution, independent of application business logic.

Benefits of technology

It greatly improves the efficiency and real-time performance of data distribution, reduces data transmission paths, is suitable for all single-machine multiple application software data distribution scenarios, and provides strong versatility.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN112463400B_ABST
    Figure CN112463400B_ABST
Patent Text Reader

Abstract

The present invention discloses a real-time data distribution method and device based on shared memory, the method comprises: a publishing process and a receiving process load a data distribution component, the data distribution component applies for a plurality of shared memory channels, each shared memory channel is used for data distribution of a data subject; the receiving process subscribes to the data subject of interest, and applies for a shared memory channel from the data distribution component, and fixes the data subject of the applied shared memory channel as the subscribed data subject; the publishing process publishes data to all shared memory channels corresponding to the data subject to which the data belongs; the receiving process periodically queries whether there is data in the applied shared memory channel, and if so, receives the data in the shared memory channel; otherwise, continues to periodically query. The present invention reduces the number of copies in the data distribution process, and has strong versatility.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of computer communication technology, and specifically relates to a real-time data distribution method and device based on shared memory. Background Art

[0002] Since the performance improvement of a single CPU core is limited, the current high-performance CPU design is developing towards multi-core and multi-threaded. In order to use CPU resources more efficiently and avoid the suspension of services provided by the application due to the crash of a single process, the application architecture based on multi-process development is usually selected as the preferred architecture for complex applications. Unlike single-process multi-threaded applications, the data space isolation between processes of multi-process applications cannot directly share data like multi-threaded architectures. Therefore, multi-process architecture applications require efficient message passing and data distribution mechanisms. The communication between multi-process architecture applications depends on the underlying communication mechanism of the operating system. Usually, the underlying communication mechanism of the operating system is relatively simple to take into account the general functions. To implement a complex data distribution mechanism, especially when it comes to supporting one-to-one, one-to-many, many-to-one and many-to-many data distribution, a set of complex data interaction mechanisms needs to be designed.

[0003] Currently, common methods for distributing data between applications in a multi-process architecture include: implementing it based on a combination of TCP / IP network communication and custom protocols; implementing it based on a message bus (such as DDS middleware); and implementing message sending and reading based on the message queue provided by the operating system to complete one-to-one or many-to-one data distribution.

[0004] However, the existing technologies have many shortcomings in terms of performance, real-time and versatility. The performance of TCP / IP network communication and custom protocols is limited by the implementation of the operating system network protocol stack, and the performance of network communication within a single machine is low; the IP and port information of the receiving module and the sending module cannot be shared in real time, and a forwarding module needs to be designed. The data is copied multiple times between the transceiver module and the forwarding module, resulting in low communication efficiency and large latency, which cannot meet the use occasions with high real-time requirements (such as robot motion control and autonomous driving control); custom communication protocols lack specifications and standards, are usually highly coupled with the application framework, and have poor versatility.

[0005] Data distribution based on message bus (such as DDS middleware) is generally used for data transmission and reception between multiple machines. It is also implemented based on TCP / IP protocol and has the same real-time and performance problems. In addition, this type of middleware is complex to implement and depends on many libraries. It is a heavyweight message bus and cannot be simply applied to embedded systems with high real-time requirements.

[0006] The data distribution method based on the message queue provided by the operating system has a limit on the data length of a single message. Modifying the length limit requires configuring the operating system kernel parameters, which makes it difficult to form a general framework. More importantly, the message queue only supports one-to-one and many-to-one data distribution, and cannot realize one-to-many and many-to-many data transmission and reception. The message queue implemented by the operating system has four memory copies in the user space and kernel space, and the communication efficiency is low. The process of sending and receiving four copies of data in the message queue is as follows: Figure 1 As shown, when a message is sent, the user cache is copied to the kernel cache through a system call, and then copied from the kernel cache to the message queue. When a message is received, the message is copied from the message queue to the kernel cache and then copied from the kernel cache to the receiving user cache through a system call. Summary of the invention

[0007] The purpose of the present application is to provide a real-time data distribution method and device based on shared memory, which reduces the number of copies in the data distribution process and has strong versatility.

[0008] In order to achieve the above purpose, the technical solution adopted by this application is:

[0009] A real-time data distribution method based on shared memory is used for data distribution among multiple processes, wherein the process used for publishing data is a publishing process, and the process used for receiving data is a receiving process. The real-time data distribution method based on shared memory comprises:

[0010] The publishing process and the receiving process load a data distribution component, and the data distribution component applies for multiple shared memory channels, each shared memory channel is used for data distribution of a data topic;

[0011] The receiving process subscribes to the data subject of interest, and applies for a shared memory channel from the data distribution component, fixing the data subject of the applied shared memory channel as the subscribed data subject;

[0012] The publishing process publishes the data to all shared memory channels corresponding to the data topic to which the data belongs;

[0013] The receiving process periodically queries whether there is data in the applied shared memory channel, and if so, receives the data in the shared memory channel; otherwise, continues to query periodically.

[0014] Several optional methods are also provided below, but they are not intended to be additional limitations on the above-mentioned overall solution, but are merely further supplements or preferences. Under the premise that there are no technical or logical contradictions, each optional method can be combined with the above-mentioned overall solution separately, and multiple optional methods can also be combined.

[0015] Preferably, the data distribution component includes a channel management unit and a module ID management unit, and each process carries an independent process ID. When the publishing process and the receiving process load the data distribution component, the data distribution component automatically completes the initialization of the channel management unit and the module ID management unit, wherein:

[0016] The module ID management unit initialization includes:

[0017] During the first initialization, apply for module management shared memory, set the layout of the module management shared memory, and apply for multiple globally unique module IDs; during non-first initialization, mount the corresponding module management shared memory, and use the applied multiple globally unique module IDs;

[0018] The layout of the module management shared memory is that the memory addresses are, from low to high, the last allocated module ID, the process ID array and the module status array, the sizes of the process ID array and the module status array are the same and equal to the number of module IDs, the process ID array is stored in the format of module ID-process ID, and the module status array is stored in the format of module ID-module status; after the module ID management unit is initialized, the last allocated module ID is initialized to 0, the process ID array is initialized to empty, and the module status array is initialized to free;

[0019] The channel management unit initialization includes:

[0020] During the first initialization, apply for a channel management shared memory, set the layout of the channel management shared memory, and apply for multiple globally unique channel IDs; during non-first initialization, attach the corresponding channel management shared memory and use the applied multiple globally unique channel IDs;

[0021] The layout of the channel management shared memory is that the memory addresses are, from low to high, the last allocated channel ID and the channel information array, wherein the size of the channel information array is equal to the number of shared memory channels, and each element in the channel information array is used to record the channel ID and the attributes of the shared memory channel, and the attributes of the shared memory channel include the shared memory key corresponding to the channel, the shared memory size, the semaphore key, the data topic ID, the module ID, the subscriber thread ID, the subscriber process ID, the channel usage status and the channel cleanup status; after the channel management unit is initialized, the last allocated channel ID is initialized to 0, and the channel information array is initialized to a default value.

[0022] Preferably, after the publishing process and the receiving process load the data distribution component, the module ID management unit allocates a respective module ID to each process, and updates the last allocated module ID, process ID array and module status array.

[0023] Preferably, the shared memory channel includes a shared memory queue, which is a FIFO queue. The Head pointer of the shared memory queue records the relative pointer of the first packet header, and the Tail pointer of the shared memory queue records the relative pointer of the last packet header, and the relative pointer is an offset relative to the starting address of the shared memory channel.

[0024] Preferably, the publishing process publishes the data to all shared memory channels corresponding to the data topic to which the data belongs, including:

[0025] Mounting the channel management shared memory;

[0026] Acquire channel IDs of all shared memory channels fixed to a specified data topic based on the channel management shared memory;

[0027] Determine whether the channel clearing status in the corresponding channel information array is to be cleared according to the channel ID, and clear all shared memory channels whose channel clearing status is to be cleared;

[0028] Dynamically mount the shared memory queues in the remaining shared memory channels;

[0029] Send data to each shared memory queue.

[0030] Preferably, the receiving process periodically queries whether there is data in the applied shared memory channel, and if so, receives the data in the shared memory channel; otherwise, continues to query periodically, including:

[0031] Mounting the channel management shared memory;

[0032] Acquire the attributes of the shared memory channel of the specified data topic subscribed by the receiving process based on the channel management shared memory;

[0033] Mounting a shared memory queue in the shared memory channel according to the attribute of the shared memory channel;

[0034] Determine whether there is data in the shared memory queue. If there is data, obtain the data packet size and data packet release time;

[0035] Read data from the shared memory queue based on the obtained data packet size and data packet release time.

[0036] Preferably, the real-time data distribution method based on shared memory further includes the receiving process unsubscribing from the data topic, wherein the receiving process unsubscribing from the data topic includes:

[0037] Mounting the channel management shared memory;

[0038] Traversing the channel management shared memory, searching for a corresponding channel ID based on the process ID and the data subject to be unsubscribed;

[0039] According to the found channel ID, the attributes of the shared memory channel are obtained from the corresponding channel management shared memory, and the shared memory queue is mounted according to the attributes of the shared memory channel;

[0040] Set the channel usage status in the properties of the shared memory channel to idle, release the resources of the shared memory queue, and mark the channel cleanup status as pending cleanup.

[0041] Preferably, the real-time data distribution method based on shared memory further comprises, when all processes unsubscribe from the data topic, destroying the data distribution component by the process that unsubscribes last, wherein destroying the data distribution component comprises destroying the module ID management unit and destroying the channel management unit;

[0042] The module ID management unit is destroyed, including: cleaning up the module management shared memory and recycling the module ID;

[0043] The channel management unit is destroyed, including: clearing the channel management shared memory and recycling the channel ID.

[0044] The present application also provides a real-time data distribution device based on shared memory, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, it implements the real-time data distribution method based on shared memory described in any of the above technical solutions.

[0045] The real-time data distribution method and device based on shared memory provided by the present application, by opening different shared memory channels on the shared memory, establishing the corresponding relationship between the shared memory channels and the data topics, the publishing process sends data to a specific data topic, and the receiving process receives data from the subscribed data topic, and decouples the association between multiple processes by publishing and subscribing to the topic, thereby realizing one-to-one, one-to-many and many-to-many data distribution between processes; and the data transmission and reception of different programs only have two memory copies and do not need to enter the kernel state of the operating system, which reduces the data copy process, shortens the data transmission path, and greatly improves the data distribution efficiency and real-time performance. In addition, the data distribution component provided by the present invention is independent of the application business logic, is applicable to all scenarios of data distribution of multiple application software on a single machine, and has strong versatility. BRIEF DESCRIPTION OF THE DRAWINGS

[0046] Figure 1 It is a flow chart of data distribution in the prior art;

[0047] Figure 2 This is a flow chart of a real-time data distribution method based on shared memory in this application;

[0048] Figure 3 Flow chart of the registration subscription process for this application receiving process;

[0049] Figure 4 A flowchart of the data release process for this application;

[0050] Figure 5 Flow chart of unsubscribing data subject for this application receiving process;

[0051] Figure 6 A schematic diagram of an embodiment of a real-time data distribution method based on shared memory of the present application;

[0052] Figure 7 This is a structural diagram of a real-time data distribution device based on shared memory in this application. DETAILED DESCRIPTION

[0053] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0054] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as those commonly understood by those skilled in the art to which this application belongs. The terms used herein in the specification of this application are only for the purpose of describing specific embodiments and are not intended to limit this application.

[0055] In one of the embodiments, a real-time data distribution method based on shared memory is provided, which is used for data distribution among multiple processes. Each process carries an independent process ID. The process used to publish data is the publishing process, and the process used to receive data is the receiving process. The publishing process and the receiving process complete data interaction through a data distribution component, and the data distribution component is implemented based on shared memory.

[0056] like Figure 2 As shown, the real-time data distribution method based on shared memory in this embodiment includes the following steps:

[0057] Step S1: The publishing process and the receiving process load a data distribution component. The data distribution component applies for multiple shared memory channels, and each shared memory channel is used for data distribution of a data topic.

[0058] The data topic is used to identify different data types. When the publishing process and the receiving process load the data distribution component, the data distribution component automatically completes the shared memory initialization. In this embodiment, the data distribution component includes a channel management unit and a module ID management unit, so the shared memory initialization includes the channel management unit initialization and the module ID management unit initialization.

[0059] To support cross-process data distribution, unlike the common message-based heap memory management, the configuration information of the above-mentioned module ID management unit and channel management unit is not stored in the process's heap space but in shared memory. All processes using this component can directly manage shared memory, eliminating the overhead of copying and transmitting messages between different processes and ensuring the consistency of management unit information.

[0060] Specifically, in this embodiment, the module ID management unit initialization includes:

[0061] During the first initialization, apply for module management shared memory, set the layout of module management shared memory, and apply for multiple globally unique module IDs; since the same shared memory cannot be created again after the shared memory is created, this embodiment, during non-first initialization (i.e., when loaded from the second process), hooks up the corresponding module management shared memory and directly uses the applied multiple globally unique module IDs. Since the method of this application is applicable to data distribution between multiple processes, the module ID needs to be globally unique at the operating system level, and the module ID management unit adopts a strategy of dynamic application and dynamic release.

[0062] As shown in Table 1, the layout of the module management shared memory is that the memory addresses are, from low to high, the last allocated module ID, the process ID array, and the module status array. The sizes of the process ID array and the module status array (i.e., n in Table 1) are the same and equal to the number of module IDs.

[0063] Table 1 Module management shared memory layout

[0064] Last assigned module ID Process ID array[n] Module status array[n]

[0065] The process ID array records the process ID (including thread ID) of the application thread. It is obtained using the underlying function of the operating system when the component is initialized. The process ID can be used to dynamically determine whether the program in which the component is located is alive. The module status array records the module status (such as whether it is idle). The last allocated module ID temporarily stores the latest allocated module ID to quickly determine the most likely idle module ID for the next allocation.

[0066] The process ID array is stored in the format of "module ID-process ID", and the module status array is stored in the format of "module ID-module status". Of course, it is also possible to store only the process ID and module status in the process ID array and module status array in the order of the module ID, and establish a mapping relationship between the module ID, the process ID, and the module status.

[0067] In this embodiment, after the module ID management unit is initialized, the last allocated module ID is initialized to 0, the process ID array is initialized to empty, and the module status array is initialized to idle.

[0068] For each process that loads the data distribution component, the module ID management unit will assign a module ID to each process and update the last assigned module ID, process ID array, and module status array. This records the currently loaded process, and the assigned module ID is used to identify the process, making it easier to query the current memory resource usage.

[0069] Furthermore, by using the module ID management unit to assign module IDs to processes, it is also possible to distinguish processes by combining module IDs and process IDs, so as to facilitate the same process to achieve multiple loading and subscriptions. Since the same receiving process can subscribe to multiple different data topics, but it is impossible to receive the same data twice for the same data topic, the module ID set in this embodiment can also be further used as a distinguishing judgment condition. The same process applies for multiple module IDs, allowing the same process with different module IDs to subscribe to the same data topic, that is, the same process with different module IDs is equivalent to two different processes for processing. While realizing data transmission based on the minimum number of copies, it can also realize redundant transmission of data, greatly improving the practicality and application prospects of data distribution in this application.

[0070] A core point of module ID management unit initialization is how to judge whether module management shared memory has been created and initialized. Because modern operating system process space is completely isolated, variables cannot be shared with each other to judge whether the module management shared memory has been created. Therefore, this embodiment adds specific identification according to different operating system shared memory creation interfaces, directly creates shared memory when the shared memory does not exist, and performs initialization process at the same time. When the shared memory already exists, an error message will be returned, prompting that the shared memory already exists. At this time, it is only necessary to directly mount the existing shared memory without performing initialization process.

[0071] In the initialization of the module ID management unit, it is necessary to dynamically apply for the module ID. The module ID is stored in the shared memory and is a globally unique ID at the operating system level. It is allocated when the module is initialized and released when the module is destroyed. In order to improve the efficiency of module ID application and registration, this embodiment uses the CPU atomic operation instructions to implement self-increment and self-decrement operations of the module ID, avoiding the use of time-consuming system semaphores and mutexes and other global lock resources. The core of module ID application and registration is to use CAS (Compare And Swap) technology to modify the ID without using a mutex.

[0072] Specifically, in this embodiment, the channel management unit initialization includes:

[0073] During the first initialization, apply for channel management shared memory, set the layout of channel management shared memory, and apply for multiple globally unique channel IDs; during non-first initialization, attach the corresponding channel management shared memory and directly use the multiple globally unique channel IDs that have been applied for. Similar to the module ID, the channel ID in this embodiment needs to be globally unique at the operating system level. The channel ID management also adopts the strategy of dynamic application and dynamic release. The channel management unit is used to manage the shared memory channel, which can effectively improve the utilization rate of the shared memory channel and optimize the allocation.

[0074] As shown in Table 2, the layout of the channel management shared memory is that the memory address is the last allocated channel ID and the channel information array from low to high. The last allocated channel ID temporarily stores the latest allocated channel ID, which is used to quickly determine the most likely idle channel ID for the next allocation. The size of the channel information array is equal to the number of shared memory channels, and each element in the channel information array is used to record the channel ID and the attributes of the shared memory channel. The attributes of the shared memory channel include the shared memory Key (key value of the shared memory) corresponding to the channel, the shared memory size, the semaphore Key (key value of the semaphore), the data topic ID, the module ID, the subscriber (i.e., the receiving process) thread ID, the subscriber process ID, the channel usage status, and the channel cleanup status.

[0075] Table 2 Layout of channel management shared memory

[0076]

[0077] After the channel management unit is initialized, the last allocated channel ID is initialized to 0, and the channel information array is initialized to a default value, which can be 0, empty or free, etc., according to the properties of the actual field. The method for determining whether the channel management shared memory has been created during the initialization process is the same as that in the module ID management unit initialization, so it will not be described in detail here.

[0078] Step S2: The receiving process subscribes to the data subject of interest, and applies for a shared memory channel from the data distribution component, fixing the data subject of the applied shared memory channel as the subscribed data subject.

[0079] In this embodiment, the process of registering and subscribing to the data topic of interest by the receiving process is essentially to establish an association between the module ID, topic and shared memory channel corresponding to the receiving process. The receiving process and the sending process (referred to as the publisher) exchange data through different shared memory channels. The subscriber and the publisher can only send and receive messages after establishing a subscription relationship. The publisher and the subscriber are associated through data topics (identifying different data types).

[0080] In one embodiment, the registration subscription process is as follows Figure 3 As shown, the specific process is divided into three sub-steps:

[0081] (1) Find out whether the receiving process has subscribed to the data topic to be subscribed. The search process can be to traverse all shared memory channels of the channel management unit to check whether the data topic exists among the data topics subscribed by the receiving process.

[0082] (2) If the subscription has already been made, the subscription fails; otherwise, the receiving process applies for a shared memory channel. There is a many-to-one correspondence between the shared memory channel and the receiving process, and a one-to-one correspondence between the shared memory channel and the data topic. That is, the receiving process can subscribe to multiple data topics at the same time, and needs to apply for the corresponding number of shared memory channels at the same time. It should be noted that the method of applying for available shared memory is a relatively mature technology in the field of data communication, and will not be described in detail here.

[0083] (3) Set the properties of the shared memory channel. This includes setting the shared memory key, shared memory size, semaphore key, data topic ID, module ID, subscriber thread ID, subscriber process ID, channel usage status, and channel cleanup status of the channel's internal queue. It should be noted that shared memory keys and semaphore keys are system resources and may be occupied by other programs. Pre-allocated strategies cannot be used. The method for dynamically applying for unoccupied shared memory keys and semaphore keys is: within a certain range of key values, try to create shared memory and semaphores based on a certain key. If successful, use the key value. If unsuccessful, add one to the key value and continue to create. Repeat the above process until the first unused key value is found.

[0084] In order to ensure the normal caching of data, in one embodiment, the shared memory channel includes a shared memory queue for caching the data sent by the sending process. The shared memory queue is a general cache queue based on shared memory, which is similar to the queue interface of the C++ standard library, but the internal implementation and usage scenarios are completely different. The standard library is implemented based on heap memory and can only be used between threads within a process; while the shared memory queue of the present application is implemented based on shared memory, and can be used between all processes within the operating system, and can also be used between threads within a process, supporting single producer single consumer and multi-producer single consumer usage modes, and has a wider range of applications.

[0085] In this embodiment, the shared memory queue is a FIFO queue. One of the key points of the implementation of the FIFO queue is to solve the problem that the pointer cannot be used across processes. Since each process has the same physical address after connecting to the shared memory, the starting address of the virtual address may be different. The FIFO queue implementation in the C++ standard library is not feasible. The main reason is that the starting address of the shared memory pointer is different. To solve this problem, this embodiment proposes the concept of "relative pointer", that is, the offset relative to the starting address of the shared memory. The value of the relative pointer is the offset relative to the starting address of the shared memory.

[0086] The FIFO layout of the shared memory queue is shown in Table 3. The Head pointer of the shared memory queue records the relative pointer of the first packet header, and the Tail pointer of the shared memory queue records the relative pointer of the last packet header. The relative pointer should be understood as an offset relative to the start address of the shared memory channel where the shared memory queue is located.

[0087] Table 3 FIFO layout of shared memory queue

[0088]

[0089] In the shared memory queue, when data is queued, the Tail pointer at the end of the column points to the head of the new data packet; when data is dequeued, the Head pointer at the head of the next data packet points to the head of the next data packet. For each data packet, a fixed-size packet node attribute structure is added, which includes but is not limited to the next node pointer, queue entry time, data packet size, and custom attribute packet fields. The custom packet field here provides the shared memory queue of this embodiment with the possibility of editing, supports the writing of custom fields, and improves the versatility of the overall method.

[0090] The shared memory queue implements data enqueue, data dequeue, and determines whether the queue is empty. The data enqueue operation checks the enqueue buffer pointer and size parameters to determine whether the column tail pointer exceeds the memory size after enqueue. If it does not exceed the memory size, put the data at the end of the shared memory queue and update the column tail pointer at the same time; if it exceeds the memory size, try to allocate space for the data from the column head. If there is enough space, the data is enqueued and the column tail pointer is updated and returned successfully. If there is insufficient space, the enqueue operation returns failure. The data dequeue operation checks the dequeue buffer pointer and size parameters to determine whether the queue is empty. If the queue is empty, dequeue fails. If the queue is not empty, dequeue data from the queue head and copy it to the receiving process, update the receiving process data size, update the column head pointer, and return dequeue success. Determine whether the queue is empty The queue is determined by determining whether the head and tail pointers are equal and are NULL values.

[0091] In this embodiment, the shared memory queue implements data receiving and sending cache based on shared memory. Data enqueueing and data dequeueing require only two data copies for data transmission between processes. Compared with the four data copies of commonly used message queue data transmission, it saves system resources, reduces data transmission delay, and improves data distribution efficiency.

[0092] Step S3: The publishing process publishes the data to all shared memory channels corresponding to the data topic to which the data belongs.

[0093] Different from the common data publishing mechanism based on message routing, the data sent by the sending process of this embodiment is stored in shared memory, and the program sending and receiving data does not need to transfer messages through a specific server, which reduces the data copy process, reduces latency, and significantly improves transmission performance. Data publishing supports one-to-many and many-to-many data distribution operations between publishers and subscribers. The specific sub-processes are as follows:

[0094] Specifically, in this embodiment, the publishing process publishes data to all shared memory channels corresponding to the data topic to which the data belongs, mainly including the following principles:

[0095] Mount the channel management shared memory; based on the channel management shared memory, obtain the channel IDs of all shared memory channels fixed to the specified data subject; determine whether the channel clearing status in the corresponding channel information array is to be cleared according to the channel ID, and clear all shared memory channels whose channel clearing status is to be cleared; dynamically mount the shared memory queues in the remaining shared memory channels; and send data to each shared memory queue.

[0096] The data release in this embodiment does not need to pay attention to the receiving process, but only needs to be released to the specified data topic, which is not restricted by the system business logic and has great flexibility. It should be noted that the release principle provided in the above embodiment is only a general principle provided in this embodiment, and the steps can be adjusted accordingly based on this principle. For example, in another embodiment, a release process is provided as follows.

[0097] like Figure 4 As shown, in this embodiment, the publishing process publishes data to all shared memory channels corresponding to the data topic to which the data belongs, including the following steps:

[0098] (1) The mounted channel manages the shared memory. To ensure that all data distribution components are mounted on the same shared memory, the shared memory key value and size are globally unique.

[0099] (2) Obtain all receiving processes that subscribe to the data topic. To improve efficiency and support concurrent operations, each publishing process independently manages the current channel management unit in the shared memory, does not occupy the global mutex lock, and multiple publishing programs do not interfere with each other.

[0100] (3) Determine whether the receiving process is alive, and clean up the shared memory, semaphores, and channel shared memory queues occupied by the dead receiving process.

[0101] (4) According to the properties of the shared memory channel corresponding to the receiving process, the shared memory queue where the receiving process is located is dynamically mounted. The shared memory queue is determined by the shared memory identifier Key, the semaphore Key and the shared memory size Size.

[0102] (5) Send data to the shared memory queue corresponding to the receiving process, call the shared memory queue enqueue interface, and enqueue the data.

[0103] (6) If data transmission fails, there are two cases: if the receiving process has died, the transmission is abandoned; or the reliability QoS setting at the time of subscription determines whether to resend the data.

[0104] (7) If the data is sent successfully, it will continue to be sent to the shared memory queue corresponding to other receiving processes that subscribe to the topic.

[0105] Step S4, the receiving process periodically queries whether there is data in the applied shared memory channel, and if so, receives the data in the shared memory channel; otherwise, continues to query periodically.

[0106] Specifically, the following steps are included:

[0107] (1) Attach the channel to manage the shared memory. To ensure that all processes attach the same shared memory, the key value and size of the channel management shared memory are globally unique.

[0108] (2) Based on the channel management shared memory, the attributes of the shared memory channel of the specified data topic subscribed by the receiving process are obtained. Specifically, the corresponding channel ID is first searched according to the specified data topic subscribed by the receiving process, and then the attributes of the corresponding shared memory channel are obtained according to the channel ID.

[0109] (3) A shared memory queue in the shared memory channel is mounted according to the attributes of the shared memory channel, and the channel shared memory queue is determined by a shared memory identifier Key, a semaphore Key, and a shared memory size Size.

[0110] (4) Determine whether there is data in the shared memory queue. If there is data, obtain the data packet size and data packet release time. If there is no data, it is processed in two cases according to whether the blocking parameter is set: if the blocking parameter is not set, directly return no data; if the blocking parameter is set, block and wait until the data arrives.

[0111] (5) Read data from the shared memory queue according to the obtained data packet size and data packet publishing time. The data is dequeued from the shared memory queue, and the dequeuing parameter information includes data cache buf, cache space size, actual data size, and custom identifier (agreed by the publisher and subscriber).

[0112] It should be noted that the timed query method can ensure that the receiving process can obtain the required data, reduce the interaction between the receiving process and the sending process, and have little pressure on the system operation. However, in addition to the timed query method, other common data receiving and reading methods are also within the protection scope of this application. In other embodiments, if it is necessary to improve the timeliness of data reception, the receiving process also supports a notification mechanism to receive data, that is, the sending process actively notifies the corresponding receiving process after sending data to the shared memory channel, and the receiving process receives the data in the shared memory channel in a callback manner after receiving the notification.

[0113] In another embodiment, the real-time data distribution method based on shared memory of the present application further includes receiving a process to unsubscribe from a data topic. In this embodiment, the receiving process unsubscribes from a data topic, such as Figure 5 As shown, including:

[0114] (1) Attach channels to manage shared memory.

[0115] (2) Traverse the channel management shared memory and search for the corresponding channel ID based on the process ID and the data topic to be unsubscribed; if the channel exists, return the channel ID; if the channel does not exist, return -1.

[0116] (3) obtaining the attributes of the shared memory channel from the corresponding channel management shared memory according to the found channel ID, and attaching the shared memory queue according to the attributes of the shared memory channel;

[0117] (4) Set the channel usage status in the properties of the shared memory channel to idle, release the resources of the shared memory queue, mark the channel cleanup status as pending cleanup, and clean it up uniformly by the data publishing process.

[0118] And in the unsubscription process, the resources of the module ID will be released, that is, the module management shared memory will be mounted, the occupied module ID will be released, and the last allocated module ID, process ID array and module status array in the module management shared memory will be updated.

[0119] In another embodiment, the real-time data distribution method based on shared memory further includes destroying the data distribution component by the last unsubscribed process when all processes unsubscribe from the data topic, wherein destroying the data distribution component includes destroying the module ID management unit and destroying the channel management unit.

[0120] The module ID management unit is destroyed, including: cleaning up the module management shared memory and recycling the module ID.

[0121] The channel management unit is destroyed, including: clearing the channel management shared memory and recycling the channel ID.

[0122] It should be pointed out that under normal circumstances, the destruction process will be automatically executed when the program exits, but in abnormal circumstances, such as when the program exits abnormally and there is no time to release resources, it is possible that system resources will be exhausted due to resource leakage. This solution solves the above problems through two mechanisms: periodic resource recycling and triggered recycling. Periodic resource recycling uses the system timer to periodically check the zombie module and clean up the zombie module resources; the triggered recycling mechanism triggers the zombie module to be checked when data is released, and cleans up its resources if a zombie module is found.

[0123] The real-time data distribution method and device based on shared memory of this embodiment, by opening different shared memory channels on the shared memory, establishing the corresponding relationship between the shared memory channels and the data topics, the publishing process sends data to a specific data topic, and the receiving process receives data from the subscribed data topic, decouples the association between multiple processes by publishing and subscribing to the topic, thereby realizing one-to-one, one-to-many and many-to-many data distribution between processes; and the data transmission and reception of different programs only requires two memory copies and does not need to enter the kernel state of the operating system, which reduces the data copy process, shortens the data transmission path, and greatly improves the data distribution efficiency and real-time performance. In addition, the data distribution component provided by the present invention is independent of the application business logic, is applicable to all scenarios of data distribution of multiple application software on a single machine, and has strong versatility.

[0124] In another embodiment, in order to facilitate the understanding of the data distribution method of the present application, a specific example is provided to illustrate the main idea of ​​the present application.

[0125] like Figure 6 As shown in the figure, different applications load the same data distribution component (interface) when they are started, and data distribution between different applications is realized through interface calls. Program A, Program B, Program C, and Program D load the same data distribution component. Assuming that Program B and Program D subscribe to data topic 1, they will automatically apply for the corresponding shared memory channel during the subscription process. When Program A publishes data to data topic 1, the data is published to the shared memory channel of Program B and Program D, so that Program B and Program D can receive data identified as data topic 1 from the shared memory channel; similarly, Program C publishes data of data topic 1 to the shared memory channel of Program B and Program D, and Program B and Program D receive data identified as data topic 1. Different shared memory channels complete data forwarding between different programs. The data publishing program does not need to understand the information of other programs and only focuses on the topic. This method reduces the coupling between applications and provides a general solution for real-time data distribution and modular integration of applications in complex application systems.

[0126] In another embodiment, a real-time data distribution device based on shared memory is also provided. Figure 7 As shown, the device of this embodiment includes a processor, a memory, and a computer program stored in the memory and executable in the processor. When the processor executes the computer program, the steps in the real-time data distribution method based on shared memory in any of the above embodiments are implemented.

[0127] The processor referred to in this embodiment may be a central processing unit (CPU), or other general-purpose processors, such as a digital signal processor (DSP), an application-specific integrated circuit (ASIC), and a field programmable gate array (FPGA); the memory is used to store computer programs and other programs and data related to the device. The memory may be an internal storage unit, such as a hard disk or memory, or an external storage device, such as a plug-in hard disk, a smart memory card, a secure digital card, a flash memory card, etc. Further, the memory may include both a memory storage unit and an external storage device; the computer program may be divided into one or more modules / units and stored in the memory, and executed by a processor to complete the present application. One or more modules / units may be a series of computer program instruction segments that can perform specific functions, and the instruction segments are used to describe the execution process of the computer program.

[0128] It should be understood that, although the various steps in the flow chart are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear description in this article, the execution of these steps is not strictly limited in order, and these steps can be executed in other orders. Moreover, at least a portion of the steps in the figure may include multiple sub-steps or multiple stages, and these sub-steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these sub-steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a portion of the sub-steps or stages of other steps.

[0129] The technical features of the above-described embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above-described embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0130] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A real-time data distribution method based on shared memory, used for data distribution among multiple processes, wherein the process used for publishing data is the publishing process, and the process used for receiving data is the receiving process, characterized in that: The real-time data distribution method based on shared memory includes: The publishing process and the receiving process load a data distribution component, and the data distribution component applies for multiple shared memory channels, each shared memory channel is used for data distribution of a data topic; The receiving process subscribes to the data subject of interest, and applies for a shared memory channel from the data distribution component, fixing the data subject of the applied shared memory channel as the subscribed data subject; The publishing process publishes the data to all shared memory channels corresponding to the data topic to which the data belongs; The receiving process periodically queries whether there is data in the shared memory channel applied for, and if so, receives the data in the shared memory channel; otherwise, continues to query periodically; The data distribution component includes a channel management unit and a module ID management unit, and each process carries an independent process ID. When the publishing process and the receiving process load the data distribution component, the data distribution component automatically completes the initialization of the channel management unit and the module ID management unit, wherein: The module ID management unit initialization includes: During the first initialization, apply for module management shared memory, set the layout of the module management shared memory, and apply for multiple globally unique module IDs; during non-first initialization, mount the corresponding module management shared memory, and use the applied multiple globally unique module IDs; The layout of the module management shared memory is that the memory addresses are, from low to high, the last allocated module ID, the process ID array and the module status array, the sizes of the process ID array and the module status array are the same and equal to the number of module IDs, the process ID array is stored in the format of module ID-process ID, and the module status array is stored in the format of module ID-module status; after the module ID management unit is initialized, the last allocated module ID is initialized to 0, the process ID array is initialized to empty, and the module status array is initialized to free; The channel management unit initialization includes: During the first initialization, apply for a channel management shared memory, set the layout of the channel management shared memory, and apply for multiple globally unique channel IDs; during non-first initialization, attach the corresponding channel management shared memory and use the applied multiple globally unique channel IDs; The layout of the channel management shared memory is that the memory addresses are, from low to high, the last allocated channel ID and the channel information array, wherein the size of the channel information array is equal to the number of shared memory channels, and each element in the channel information array is used to record the channel ID and the attributes of the shared memory channel, and the attributes of the shared memory channel include the shared memory key corresponding to the channel, the shared memory size, the semaphore key, the data topic ID, the module ID, the subscriber thread ID, the subscriber process ID, the channel usage status and the channel cleanup status; after the channel management unit is initialized, the last allocated channel ID is initialized to 0, and the channel information array is initialized to a default value.

2. The real-time data distribution method based on shared memory as claimed in claim 1, characterized in that: After the publishing process and the receiving process load the data distribution component, the module ID management unit allocates a respective module ID to each process, and updates the last allocated module ID, process ID array and module status array.

3. The real-time data distribution method based on shared memory as claimed in claim 1, characterized in that: The shared memory channel includes a shared memory queue, which is a FIFO queue. The Head pointer of the shared memory queue records the relative pointer of the header of the first data packet, and the Tail pointer of the shared memory queue records the relative pointer of the header of the last data packet. The relative pointer is an offset relative to the starting address of the shared memory channel.

4. The real-time data distribution method based on shared memory as claimed in claim 3, characterized in that: The publishing process publishes data to all shared memory channels corresponding to the data topic to which the data belongs, including: Mounting the channel management shared memory; Acquire channel IDs of all shared memory channels fixed to a specified data topic based on the channel management shared memory; Determine whether the channel clearing status in the corresponding channel information array is to be cleared according to the channel ID, and clear all shared memory channels whose channel clearing status is to be cleared; Dynamically mount the shared memory queues in the remaining shared memory channels; Send data to each shared memory queue.

5. The real-time data distribution method based on shared memory as claimed in claim 3, characterized in that: The receiving process periodically queries whether there is data in the applied shared memory channel, and if so, receives the data in the shared memory channel; Otherwise, continue to query periodically, including: Mounting the channel management shared memory; Acquire the attributes of the shared memory channel of the specified data topic subscribed by the receiving process based on the channel management shared memory; Mounting a shared memory queue in the shared memory channel according to the attribute of the shared memory channel; Determine whether there is data in the shared memory queue. If there is data, obtain the data packet size and data packet release time; Read data from the shared memory queue based on the obtained data packet size and data packet release time.

6. The real-time data distribution method based on shared memory as claimed in claim 3, characterized in that: The real-time data distribution method based on shared memory further includes a receiving process unsubscribing from a data topic, wherein the receiving process unsubscribing from a data topic includes: Mounting the channel management shared memory; Traversing the channel management shared memory, searching for a corresponding channel ID based on the process ID and the data subject to be unsubscribed; According to the found channel ID, the attributes of the shared memory channel are obtained from the corresponding channel management shared memory, and the shared memory queue is mounted according to the attributes of the shared memory channel; Set the channel usage status in the properties of the shared memory channel to idle, release the resources of the shared memory queue, and mark the channel cleanup status as pending cleanup.

7. The real-time data distribution method based on shared memory as claimed in claim 6, characterized in that: The real-time data distribution method based on shared memory also includes, when all processes unsubscribe from the data topic, destroying the data distribution component by the process that unsubscribes last, wherein destroying the data distribution component includes destroying the module ID management unit and destroying the channel management unit; The module ID management unit is destroyed, including: cleaning up the module management shared memory and recycling the module ID; The channel management unit is destroyed, including: clearing the channel management shared memory and recycling the channel ID.

8. A real-time data distribution device based on shared memory, comprising a memory and a processor, wherein the memory stores a computer program, characterized in that: When the processor executes the computer program, the real-time data distribution method based on shared memory described in any one of claims 1 to 7 is implemented.