Inter-process communication method and device
By obtaining and repairing write counts and notification counts in a multi-process system, verifying the number of writes to memory segments, and releasing message entities in a timely manner, write exceptions and infinite loop problems in shared memory inter-process communication are solved and communication is improved. reliability and performance.
Patent Information
- Application Number
- CN202310361861.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-31
- Publication Date
- 2024-10-01
AI Technical Summary
In a multi-process system, inter-process communication based on shared memory has problems such as write exceptions, notification loops, full message caches, and reference count tampering, making communication unreliable.
By obtaining the write count and notification count, perform atomic operation repairs to ensure that the notification count is updated, add memory segment write count verification, reduce the read and write permissions of the subscriber process, release the message entity in a timely manner, and improve the reliability of inter-process communication. .
It effectively avoids write exceptions and infinite loops, ensures the normal use of the notification ring, reduces the risk of message tampering, and improves the reliability and performance of inter-process communication.
Smart Images

Figure CN118733289A_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present application relate to the field of information technology, and more specifically, to an inter-process communication method and device. Background Art
[0002] In a multi-process system, different processes need to frequently exchange data through inter-process communication (IPC) to work together. Common inter-process communication methods include pipes, sockets, shared memory (SHM), message queues, and semaphores.
[0003] Shared memory is an efficient inter-process communication method. However, for multi-process scenarios, there are also some problems with the method of implementing inter-process communication through shared memory. For example, when multiple processes queue up to write control messages to the notification ring of shared memory, if a publishing process has a write exception, it will cause other publishing processes to enter an infinite loop waiting state in the process of writing control messages to the notification ring. For another example, the subscriber process fails to read the control message in the notification ring in time, resulting in the control message cache in the notification ring being full, and the publisher process cannot write new control messages to the notification ring. For another example, the publisher and subscriber of the current inter-process communication based on shared memory, when writing or reading the message entity corresponding to the control message through reference counting, the minimum permission of these publisher processes and subscriber processes to the shared memory is to have read and write permissions to the shared memory. If one of the subscriber processes is attacked and the reference count of the message entity is tampered, then other subscriber processes may read the wrong message entity, which will cause other subscriber processes to crash.
[0004] Therefore, how to improve the reliability of inter-process communication based on shared memory has become an urgent problem to be solved. Summary of the invention
[0005] The embodiments of the present application provide a method and device for inter-process communication, which can improve the reliability of inter-process communication based on shared memory.
[0006] In a first aspect, a method for inter-process communication is provided, the method comprising: obtaining a first value of a write count, the first value of the write count being used to indicate the location of a first memory fragment corresponding to a first control message written by a publishing end process in a notification ring. Within a preset time length, if the first atomic operation times out, the notification count is repaired, and the second value of the repaired notification count is greater than the first value, and the first atomic operation comprises updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value. The current value of the notification count is used to indicate the location of X memory fragments corresponding to X control messages read by a subscribing end process in a notification ring, the X control messages correspond to the X memory fragments one-to-one, and X is a positive integer.
[0007] In the above technical solution, when a publishing end process is interrupted or scheduled for a long time when writing a control message in the notification ring, the update of the notification count fails, and other publishing end processes are also unable to update the notification count. Through the above solution, any publishing end process can repair the notification count of the notification ring, and the value of the updated notification count is greater than the value of the write count corresponding to the control message written by the publishing end, so that the notification count in the notification ring can be updated normally, so that the notification ring of the lock-free circular queue can be used normally, thereby improving the reliability of inter-process communication.
[0008] In combination with the first aspect, in some implementations of the first aspect, repairing the notification count includes: determining the position of the second memory fragment corresponding to the current value of the notification count in the notification ring as the first position. Determining the position of the first memory fragment in the notification ring as the second position. When the first position is not after the second position, updating the current value of the notification count to the second value.
[0009] In combination with the first aspect, in some implementations of the first aspect, updating the current value of the notification count to the second value includes: updating the current value to a third value, the position of the third memory segment corresponding to the third value in the notification ring is a third position, and in the notification ring, the third position is between the first position and the second position. Updating the third value to the second value.
[0010] In combination with the first aspect, in certain implementations of the first aspect, the message header corresponding to the first memory segment includes the number of write times of the first memory segment, and the number of write times is the number of times the first memory segment is written. The method also includes: when the first position is not after the second position and when writing the first control message in the first memory segment fails, updating the number of write times of the first memory segment to a preset value, and the preset value is used to indicate that the first memory segment is a non-writable or non-readable memory segment.
[0011] In the above technical solution, a new parameter, namely the number of write times of the first memory segment, is added. In case that the process of writing the first control message by the publishing process is interrupted, the number of write times of the first memory segment can be updated to a preset value, thereby helping other publishing processes or other subscribing processes to know whether the memory segment is available, thereby improving the reliability of inter-process communication.
[0012] For example, if the number of writes to the first memory segment is a preset value, other publisher processes can skip the memory segment and write corresponding control messages in other memory segments. Other subscriber processes can avoid parsing the control message in the first memory segment, which not only saves the parsing process of unavailable information, but also improves the reliability of inter-process communication.
[0013] In combination with the first aspect, in some implementations of the first aspect, the message header corresponding to the first memory segment includes a write count of the first memory segment, where the write count is the number of times the first memory segment is written, and the method further includes: verifying the validity of the first memory segment according to the write count. If the first memory segment is valid, writing the first control message in the first memory segment. The write count is updated.
[0014] In the above technical solution, before writing the first control message in the first memory segment, the validity of the first memory segment is verified according to the information indicated by the write count of the first memory segment, which can improve the reliability of inter-process communication.
[0015] In combination with the first aspect, in some implementations of the first aspect, the method further includes: obtaining a first index number from a first subscriber, the first index number being used to indicate a message entity held by the first subscriber. Obtaining a second index number from a second subscriber, the second index number being used to indicate a message entity held by the second subscriber. Determining the message entity to be released according to the first index number and the second index number.
[0016] In the above technical solution, the publishing end process determines the releasable message entity by obtaining the index number of the message entity held by the subscribing end process, so that the memory space corresponding to the releasable message entity in the shared memory can be uniformly released. In this way, the read and write permissions of the subscribing end process to the shared memory are reduced, thereby reducing the impact of attacks or failures on the subscribing end process and spreading to other subscribing end processes, thereby improving the reliability of inter-process communication.
[0017] For example, when the first subscriber process is attacked, since the release permission of the memory space corresponding to the message entity in the shared memory is on the publisher, the first subscriber does not have the permission to modify the reference count of the message entity. Therefore, the possibility of the impact of the attack on the first subscriber spreading to other subscribers is reduced, thereby improving the reliability of inter-process communication.
[0018] For another example, when the first subscriber process fails, since the publishing end has the right to release the memory space corresponding to the message entity, the publishing end can directly delete the memory space corresponding to the message entity held by the subscriber in the shared memory from the topic. Therefore, the failure of the first subscriber is reduced, and the memory space corresponding to the message entity held by the first subscriber can be released in time to avoid affecting the normal use of the shared memory, thereby improving the reliability of inter-process communication based on shared memory.
[0019] In combination with the first aspect, in some implementations of the first aspect, determining the message entity to be released according to the first index number and the second index number includes: determining the message entity corresponding to the index number before the smallest index number of the first minimum index number and the second minimum index number as the message entity to be released. The first index number is the first smallest index number among the message entities held by the first subscriber; and the second index number is the second smallest index number among the message entities held by the second subscriber.
[0020] In the above technical solution, the first publisher determines the message entity with the smallest index number among the message entities held by multiple subscribers by traversing the smallest index number of the message entities held by the subscribers. The first publisher can uniformly release the memory space corresponding to the message entity before the message entity with the smallest index number, and timely release the memory space corresponding to the message entity that has been read, thereby improving the reliability of inter-process communication based on shared memory.
[0021] In combination with the first aspect, in certain implementations of the first aspect, determining the message entity to be released based on the first index number and the second index number includes: obtaining the smallest index number, and determining the message entity corresponding to the index number before the smallest index number as the message entity to be released.
[0022] In the above technical solution, the first publishing end does not need to traverse the index number included in the received message index multiple times, which can save computing power to a certain extent and thus speed up the determination of the message entity to be released.
[0023] In combination with the first aspect, in certain implementations of the first aspect, determining the message entity to be released according to the first index number and the second index number includes: determining the message entity to be released according to the first value, the first index number, the second index number, and the number of caches in the notification ring. The first index number is a set of index numbers of message entities held by the first subscriber, the second index number is a set of index numbers of message entities held by the second subscriber, and the number of caches in the notification ring is less than or equal to the number of memory fragments of the notification ring.
[0024] In the above technical scheme, when the message entities corresponding to the smallest index number traversed are all the same message entity within a preset time period, that is, when a certain subscription end abnormally holds a certain message entity for a long time without reading it, the message entities between the index number of the abnormally held message entity and the index number of the message entity corresponding to the notification ring control message are released, which can further ensure that the memory space corresponding to the message entity is released in time, thereby improving the reliability of inter-process communication based on shared memory.
[0025] In combination with the first aspect, in certain implementations of the first aspect, determining the message entity to be released according to the first value, the first index number, the second index number, and the number of caches in the notification ring includes determining it as a first message entity set according to the first value and the number of caches in the notification ring, the first message entity set being the message entities corresponding to the control messages in the notification ring. Determining it as a second message entity set according to the first index number and the second index number, the second message entity set being the message entities held by the first subscriber and the second subscriber. Message entities other than the first message entity set and the second message entity set are determined as message entities to be released.
[0026] In combination with the first aspect, in certain implementations of the first aspect, the number of message entities held by the first subscriber is less than or equal to a first threshold, and the number of message entities held by the second subscriber is less than or equal to a second threshold.
[0027] In the above technical solution, an upper limit is set on the number of message entities held by each subscriber process, thereby preventing the subscriber from holding too many message entities, resulting in the inability of new message entities to apply for memory space in the shared memory.
[0028] In some possible implementations, the second publisher receives the third index number of the first subscriber, wherein the message entity corresponding to the third index number and the message entity corresponding to the first index number are located in different shared memories.
[0029] In the above technical solution, different publishing terminals may correspond to different shared memories, so that the possibility of the impact of the attack on the first publishing terminal spreading to other publishing terminals can be reduced, thereby improving the reliability of inter-process communication.
[0030] In combination with the first aspect, in some implementations of the first aspect, a fourth value of the read count is obtained, where the read count is used to indicate a position in the notification ring where the subscriber process reads the second control message. According to the write count and the read count, it is determined whether to read a message entity corresponding to the second control message in the shared memory.
[0031] It should be understood that the read count is determined by the notification count of the notification ring, and the fourth value of the read count is less than or equal to the current value of the notification count.
[0032] In the above technical solution, when the subscriber process reads the control message slowly, the notification ring cache is full, and the publisher process cannot continue to write the control message into the notification ring. The subscriber process determines whether to read the message entity corresponding to the control message in the memory segment in the notification ring based on the write count and the read count. The publisher process does not need to perceive whether the notification ring is full, and the impact of the slow reading of the subscriber process will not spread to the publisher. In this way, it can be ensured that the writing of the control message of the publisher process is not affected by the reading of the subscriber process, and the decoupling of the publisher process and the subscriber process is achieved, while ensuring that the subscriber reads the correct message entity according to the control message, that is, the reliability of inter-process communication is guaranteed.
[0033] In combination with the first aspect, in certain implementations of the first aspect, determining whether to read the message entity corresponding to the second control message in the shared memory according to the write count and the read count includes: when the first value is greater than or equal to the sum of the fourth value and the number of memory fragments in the notification ring, determining not to read the message entity corresponding to the second control message. Alternatively, when the first value is less than the sum of the fourth value and the number of memory fragments in the notification ring, determining to read the message entity corresponding to the second control message.
[0034] In a second aspect, an inter-process communication device is provided, the device comprising an acquisition unit and a processing unit: the acquisition unit is used to acquire a first value of a write count, the first value of the write count is used to indicate the position of a first memory fragment corresponding to a first control message written by a publishing end process in a notification ring. The processing unit is used to repair the notification count within a preset time length if the execution of a first atomic operation times out, the second value of the repaired notification count is greater than the first value, and the first atomic operation includes updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value. The current value of the notification count is used to indicate the position of X memory fragments corresponding to X control messages read by a subscribing end process in a notification ring, the X control messages correspond to the X memory fragments one-to-one, and X is a positive integer.
[0035] It should be understood that the beneficial effects brought about by the technical solution of the second aspect are similar to those of the first aspect. For details, please refer to the first aspect and will not be repeated here.
[0036] In conjunction with the second aspect, in some implementations of the second aspect, the processing unit is specifically configured to: determine the position of the second memory fragment corresponding to the current value of the notification count in the notification ring as the first position. Determine the position of the first memory fragment in the notification ring as the second position. When the first position is not after the second position, update the current value of the notification count to the second value.
[0037] In conjunction with the second aspect, in some implementations of the second aspect, the processing unit is specifically configured to: update the current value to a third value, the position of the third memory segment corresponding to the third value in the notification ring is a third position, and in the notification ring, the third position is between the first position and the second position. Update the third value to the second value.
[0038] In combination with the second aspect, in certain implementations of the second aspect, the message header corresponding to the first memory segment includes the number of write times of the first memory segment, and the number of write times is the number of times the first memory segment is written. The processing unit is also used to: when the first position is not after the second position and when writing the first control message in the first memory segment fails, update the number of write times of the first memory segment to a preset value, and the preset value is used to indicate that the first memory segment is a non-writable or non-readable memory segment.
[0039] In conjunction with the second aspect, in some implementations of the second aspect, the message header corresponding to the first memory segment includes a write count of the first memory segment, where the write count is the number of times the first memory segment is written, and the processing unit is further configured to: verify the validity of the first memory segment according to the write count. If the first memory segment is valid, write the first control message into the first memory segment. Update the write count.
[0040] In combination with the second aspect, in some implementations of the second aspect, the acquisition unit is further used to: acquire a first index number from the first subscriber, the first index number being used to indicate a message entity held by the first subscriber. Acquire a second index number from the second subscriber, the second index number being used to indicate a message entity held by the second subscriber. The processing unit is further used to: determine the message entity to be released according to the first index number and the second index number.
[0041] In conjunction with the second aspect, in some implementations of the second aspect, the processing unit is specifically configured to: determine the message entity corresponding to the index number before the smallest index number between the first minimum index number and the second minimum index number as the message entity to be released. The first index number is the first smallest index number among the message entities held by the first subscriber; the second index number is the second smallest index number among the message entities held by the second subscriber.
[0042] In combination with the second aspect, in some implementations of the second aspect, the processing unit is specifically used to: obtain a minimum index number, and determine a message entity corresponding to an index number before the minimum index number as a message entity to be released.
[0043] In conjunction with the second aspect, in certain implementations of the second aspect, the processing unit is specifically used to: determine the message entity to be released according to the first value, the first index number, the second index number, and the number of caches in the notification ring. The first index number is a set of index numbers of message entities held by the first subscriber, the second index number is a set of index numbers of message entities held by the second subscriber, and the number of caches in the notification ring is less than or equal to the number of memory fragments of the notification ring.
[0044] In combination with the second aspect, in certain implementations of the second aspect, the processing unit is specifically used to: determine, based on the first value and the number of caches in the notification ring, a first message entity set, the first message entity set being message entities corresponding to control messages in the notification ring. Determine, based on the first index number and the second index number, a second message entity set, the second message entity set being message entities held by the first subscriber and the second subscriber. Determine message entities other than the first message entity set and the second message entity set as message entities to be released.
[0045] In combination with the second aspect, in certain implementations of the second aspect, the number of message entities held by the first subscriber is less than or equal to a first threshold, and the number of message entities held by the second subscriber is less than or equal to a second threshold.
[0046] In some possible implementations, the second publisher receives the third index number of the first subscriber, wherein the message entity corresponding to the third index number and the message entity corresponding to the first index number are located in different shared memories.
[0047] In combination with the second aspect, in some implementations of the second aspect, the acquisition unit is further configured to acquire a fourth value of the read count, the read count being used to indicate a position in the notification ring where the subscriber process reads the second control message, and the fourth value of the read count being less than or equal to a current value of the notification count. The processing unit is configured to determine, based on the write count and the read count, whether to read a message entity corresponding to the second control message in the shared memory.
[0048] In conjunction with the second aspect, in some implementations of the second aspect, the processing unit is specifically configured to: when the first value is greater than or equal to the sum of the fourth value and the number of memory segments in the notification ring, determine not to read the message entity corresponding to the second control message. Alternatively, when the first value is less than the sum of the fourth value and the number of memory segments in the notification ring, determine to read the message entity corresponding to the second control message.
[0049] In a third aspect, an inter-process communication device is provided, which includes a processor and a memory, wherein the memory is used to store a computer program, and the processor executes the computer program stored in the memory so that the device executes any possible method in the first aspect.
[0050] In a fourth aspect, an intelligent driving device is provided, which includes any possible device in the second aspect or the third aspect.
[0051] The intelligent driving equipment in this application may include road vehicles, water vehicles, air vehicles, industrial equipment, agricultural equipment, or entertainment equipment, etc. For example, the intelligent driving equipment may be a vehicle, which is a vehicle in a broad sense, and may be a vehicle (such as a commercial vehicle, a passenger car, a motorcycle, a flying car, a train, etc.), an industrial vehicle (such as a forklift, a trailer, a tractor, etc.), an engineering vehicle (such as an excavator, a bulldozer, a crane, etc.), agricultural equipment (such as a mower, a harvester, etc.), amusement equipment, a toy vehicle, etc. The embodiment of this application does not specifically limit the type of vehicle. For another example, the intelligent driving equipment may be a vehicle such as an airplane or a ship.
[0052] In some possible implementations, the intelligent driving device is a vehicle.
[0053] In a fifth aspect, a computer program product is provided, the computer program product comprising: a computer program code, when the computer program code is run on a computer, the computer executes any possible method in the above-mentioned first aspect.
[0054] It should be noted that the above-mentioned computer program code can be stored in whole or in part on the first storage medium, wherein the first storage medium can be packaged together with the processor or separately packaged with the processor, and the embodiments of the present application do not specifically limit this.
[0055] In a sixth aspect, a computer-readable medium is provided, wherein the computer-readable medium stores a program code, and when the computer program code runs on a computer, the computer executes any possible method in the first aspect.
[0056] In a seventh aspect, an embodiment of the present application provides a chip system, which includes a processor for calling a computer program or computer instructions stored in a memory so that the processor executes any possible method in the above-mentioned first aspect.
[0057] In combination with the seventh aspect, in one possible implementation, the processor is coupled to the memory via an interface.
[0058] In combination with the seventh aspect, in a possible implementation, the chip system also includes a memory, in which a computer program or computer instructions are stored. BRIEF DESCRIPTION OF THE DRAWINGS
[0059] Figure 1 This is a schematic diagram of the working principle of a shared memory provided in an embodiment of the present application;
[0060] Figure 2 It is a schematic diagram of a data distribution service (DDS) protocol architecture provided in an embodiment of the present application;
[0061] Figure 3 It is a flowchart of an inter-process communication method based on shared memory provided in an embodiment of the present application;
[0062] Figure 4 It is a flowchart of an inter-process communication method provided by an embodiment of the present application;
[0063] Figure 5 It is a flowchart of a method for repairing a notification count update after timeout provided in an embodiment of the present application;
[0064] Figure 6 It is a flowchart of another inter-process communication method provided by an embodiment of the present application;
[0065] Figure 7 It is a flowchart of a method for releasing a message entity provided in an embodiment of the present application;
[0066] Figure 8 It is a flowchart of an inter-process communication method provided by an embodiment of the present application;
[0067] Fig. 9 It is a software architecture provided by an embodiment of the present application that realizes inter-process communication through kernel state and user state interaction;
[0068] Fig.10 It is a software architecture for implementing inter-process communication through user mode provided in an embodiment of the present application;
[0069] Fig.11 is a schematic block diagram of an inter-process communication device 1100 provided in an embodiment of the present application;
[0070] Fig.12 It is a structural block diagram of an inter-process communication device 1200 provided in an embodiment of the present application. DETAILED DESCRIPTION
[0071] The technical solutions in the embodiments of the present application will be described below in conjunction with the accompanying drawings.
[0072] In order to facilitate understanding of the embodiments of the present application, the following points are explained:
[0073] First, in this application, unless otherwise specified or there is a logical conflict, the terms and / or descriptions between different embodiments are consistent and can be referenced to each other, and the technical features in different embodiments can be combined to form new embodiments according to their internal logical relationships.
[0074] Second, in this application, "at least one" means one or more, and "more than one" means two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. In the text description of this application, the character " / " generally indicates that the associated objects before and after are in an "or" relationship. "At least one of the following" or similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b and c can mean: a, or b, or c, or a and b, or a and c, or b and c, or a, b and c. Where a, b and c can be single or multiple, respectively.
[0075] Third, in this application, "first", "second" and various numerical references are used to distinguish for the convenience of description and are not used to limit the scope of the embodiments of this application. For example, different memory segments are distinguished, rather than used to describe a specific order or sequence. It should be understood that the objects described in this way can be interchanged where appropriate so as to be able to describe solutions other than the embodiments of this application.
[0076] Fourth, in this application, descriptions such as "when...", "under the circumstances of...", and "if" all mean that the device will make corresponding processing under certain objective circumstances, and do not limit the time, nor do they require the device to have a judgment action when implementing, nor do they mean that there are other limitations. In addition, it does not mean that the judgment action after these conditional conjunctions is the only condition for achieving the result, and other additional conditions can also be included to achieve the result.
[0077] Fifth, in this application, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or apparatus comprising a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to these processes, methods, products or apparatuses.
[0078] Sixth, in this application, "used for indication" may include being used for direct indication and being used for indirect indication. When describing that a certain indication information is used for indicating A, it may include that the indication information directly indicates A or indirectly indicates A, but it does not mean that the indication information must carry A.
[0079] The indication method involved in the embodiments of the present application should be understood to include various methods that can enable the party to be indicated to know the information to be indicated. The information to be indicated can be sent as a whole or divided into multiple sub-information and sent separately, and the sending period and / or sending time of these sub-information can be the same or different. The present application does not limit the specific sending method.
[0080] The "indication information" in the embodiments of the present application may be an explicit indication, i.e., directly indicated by signaling, or obtained by combining other rules or other parameters or by deduction according to the parameters indicated by the signaling. It may also be an implicit indication, i.e., obtained by combining other rules or other parameters or by deduction according to a rule or relationship. The present application does not make any specific limitation on this.
[0081] Seventh, in this application, "storage" may refer to storage in one or more memories. The one or more memories may be separately set or integrated in an encoder or decoder, a processor, or a communication device. The one or more memories may also be partially separately set and partially integrated in a decoder, a processor, or a communication device. The type of memory may be any form of storage medium, which is not limited in this application.
[0082] To facilitate understanding, the terms involved in the embodiments of the present application are briefly described below.
[0083] 1. Process
[0084] In computing, a process is an instance of a computer program that consists of one or more threads. An operating system's process consists of program code, allocated system resources, physical access permissions, logical access permissions, and data structures that initiate, control, and coordinate execution activities. Depending on the operating system, a process can consist of multiple threads of execution that execute instructions concurrently.
[0085] 2. Inter-process communication IPC
[0086] When different processes need to communicate with each other, since different processes correspond to different address spaces, some methods are needed to implement IPC. IPC refers to the mechanism provided by the operating system that allows different processes to manage shared data. Specific methods of IPC include pipes, sockets, shared memory, message queues, and semaphores.
[0087] 3. Shared Memory SHM
[0088] Shared memory is physical memory that can be accessed by multiple processes simultaneously. A segment of shared memory can be created by one process and can be accessed by multiple processes.
[0089] 4.Publisher
[0090] Any process can act as a publisher and publish shared data.
[0091] 5. Subscriber
[0092] Any process can act as a subscriber to obtain shared data.
[0093] 6. Atomic Operations
[0094] Atomic operations are operations that will not be interrupted by any thread scheduling mechanism.
[0095] 7. Critical Area
[0096] A critical section is a program segment that accesses a shared resource.
[0097] Figure 1 It is a schematic diagram of the working principle of a shared memory provided in an embodiment of the present application.
[0098] like Figure 1 As shown, the process of inter-process communication based on shared memory can be that first, the publisher A writes the message entity A into the shared memory. The message entity A has a corresponding control message A. The control message A is used by the subscriber A to address and read the message entity A in the same shared memory. The control message A includes the identity of the message entity A, the address offset, and the length of the message entity A.
[0099] Subsequently, publisher A writes control message A into the notification ring, and transmits the notification count value A of the control message A that can be read in the current notification ring to subscriber A. The shared memory includes the notification ring, which can also be called a ring buffer. The notification ring includes N memory segments, where N is a positive integer, for example, Figure 1 As shown, the notification ring includes 8 memory segments, each of which can be written with a control message. The value A of the notification count is used to indicate the position of the memory segment in which the control message A is written in the notification ring.
[0100] Then, subscriber A reads control message A from the currently readable memory segment of the notification ring according to value A of notification count, and parses and addresses control message A to obtain an addressing result.
[0101] Finally, subscriber A reads the message entity A corresponding to control message A from the shared memory according to the addressing result.
[0102] The release of the shared memory corresponding to the message entity A is achieved through reference counting. When the message entity A is written into the shared memory, the initial reference count of the message entity A is M, where M is a positive integer, that is, the number of times the message entity A can be read is M. The subscriber reads the message entity A once according to the control message A, and the reference count of the message entity A becomes M-1. Both the publisher and the subscriber have the right to modify the reference count of the message entity A. When the reference count of the message entity A becomes 0, the message entity A is released, that is, the memory space corresponding to the message entity A in the shared memory is released, and the new message entity can be written into the memory space.
[0103] It should be understood that the above-mentioned publisher A and subscriber A may be different processes.
[0104] Currently, inter-process communication based on shared memory has the following problems.
[0105] First, when multiple processes acting as publishers need to publish control messages at the same time, that is, when multiple processes need to write different message entities into shared memory at the same time, multiple processes need to write different control messages into the notification ring at the same time. Each message entity written into shared memory by each publisher process corresponds to a control message, and each publisher process needs to write its own control message into different memory segments of the notification ring in sequence. At this time, if a process is abnormally interrupted during the writing process, the process cannot complete the step of successfully writing the control message into the notification ring, which will affect the process of subsequent processes writing the control message into the notification ring, causing the notification ring of the shared memory to be unable to be used normally. This will affect the reliability of inter-process communication.
[0106] Second, if the subscriber process is slow to read the control message of the notification ring, if the notification ring of the shared memory is full, the publisher process cannot write new control messages into the notification ring, which means that the publishing process is blocked from publishing control messages. This will also affect the reliability of inter-process communication.
[0107] Third, all processes that implement inter-process communication through the same shared memory currently release the message entity in the shared memory through reference counting, thereby releasing the memory space corresponding to the message entity, and then other processes can write other message entities in the memory space. Specifically, each message entity includes an initial reference count, and the subscriber process or the publisher process can reduce the reference count of the message entity. When the reference count is 0, the memory space corresponding to the message entity can be released. Therefore, both the subscriber process and the publisher process have read and write permissions to the reference count of the message entity in the shared memory. If one of the subscriber processes is attacked and the reference count of a message entity is modified to 0, the corresponding memory space has been released at this time, and a new message entity is written by other publishers. Other subscribers may read the wrong message entity, thereby affecting the communication of the process. This will affect the reliability of inter-process communication.
[0108] In order to solve the above problems, the present application embodiment proposes an inter-process communication method and device. Figures 2 to 12 Detailed description.
[0109] The inter-process communication method of the embodiment of the present application can be applied to inter-process communication scenarios with high reliability requirements, such as inter-process communication scenarios in vehicle-mounted systems or inter-process communication scenarios in robot systems.
[0110] The device that specifically implements the inter-process communication method may be a chip, a chip system or a processor that implements the inter-process communication method, or may be a component that implements the inter-process communication method, that is, a software functional module that implements the inter-process communication method.
[0111] The component implementing the inter-process communication method may be an independent component used for inter-process communication. The component implementing the inter-process communication method may also be a basic communication component in a communication management component of a system with high reliability requirements. For example, a basic communication component in a communication management component of a vehicle-mounted system.
[0112] The following will be described using an in-vehicle system that complies with the automotive open system architecture (AUTOAR) as an example. It should be understood that the inter-process communication method in the embodiment of the present application is a general inter-process communication method based on shared memory, which can be applied to other systems with high reliability requirements, such as robot systems. The embodiment of the present application does not limit this.
[0113] AUTOSAR's adaptive platform (AP) mainly provides high-performance computing and communication mechanisms for vehicle systems, and provides flexible software configuration. AUTOSAR AP includes communication management (CM) components, execution management (EM) components, and platform health management (PHM) components. Among them, the CM component can be used to manage the IPC of the vehicle system, which can be implemented through the data distribution service (DDS) protocol or the scalable service-oriented middleware over IP (SOME / IP) protocol. The DDS protocol is generally used for high-performance internal communication scenarios, and the SOME / IP communication protocol is generally used for communication scenarios that connect to external components. The underlying communication channels of the DDS protocol include shared memory, user datagram protocol (UDP), and transmission control protocol (TCP).
[0114] Figure 2 This is a schematic diagram of a DDS protocol architecture provided in an embodiment of the present application.
[0115] The DDS protocol specification is defined by the Object Management Group (OMG) for publish / subscribe data distribution systems. The core of the DDS protocol is a data-centric publish-subscribe (DCPS) model that is designed to provide efficient data transfer between processes even in distributed heterogeneous platforms. The DCPS model creates a "global data space" that can be accessed by any independent application, such as Figure 2 As shown in Figure 2. DCPS facilitates efficient data distribution. In DDS, each process that publishes or subscribes to data is called a participant. Each process that is a participant can be a publisher and / or a subscriber, such as Figure 2As shown in the figure, when a process acts as a publisher, the publisher can manage one or more data writers, and the publisher publishes data to one or more topics; when a process acts as a subscriber, the subscriber can also manage one or more data readers. The subscriber and the publisher implement inter-process communication through the underlying communication channel (for example, UDP, TCP, or shared memory) allowed by the real-time publish / subscribe (RTPS) protocol. Among them, the topic is used to identify each data object between the data writer and the data reader. Each topic is defined by a name and a data type.
[0116] Figure 3 is a flowchart of an inter-process communication method based on shared memory provided in an embodiment of the present application. It should be understood that Figure 3 yes Figure 2 The detailed process in which publisher A writes control message A into the notification ring and subscriber A reads control message A from the notification ring.
[0117] like Figure 3 As shown, the publisher A and the subscriber A represent different processes respectively. The publisher A may be process #1, and the subscriber A may be process #2.
[0118] S301, the publisher A obtains the A value of the write count of the notification ring, and performs an increment operation on the A value of the write count to update the A value of the write count to the B value.
[0119] The A value of the write count is used to indicate the number of times the write control message of the notification ring is written. For example, when the A value of the write count is 10, the write count is used to indicate that the number of times the write control message of the notification ring is written is 10 times.
[0120] The write count A value is also used to indicate the location of the memory segment where the publisher process writes the control message in the notification ring. For the notification ring of the lock-free circular queue, Figure 3 As shown, the notification ring is divided into N memory segments, each memory segment has its own corresponding position in the notification ring, and N is a positive integer, for example, N can be 8, 128, 256, etc. The A value of the write count can correspond to the memory segment at the corresponding position in the notification ring, and the specific position of the memory segment is determined according to the A value of the write count and the number N of the memory segments of the notification ring.
[0121] For example, the number of memory segments N in the notification ring is 8. Figure 3As shown, the notification ring includes memory segments at position 0 to position 7. For example, when the A value of the write count is 0, it means that the number of writes of the control message of the notification ring is 0. At this time, the memory segment corresponding to the write count of 0 in the notification ring is the memory segment at position 0. For another example, when the A value of the write count is 10, it means that the number of writes of the control message of the notification ring is 10. At this time, the memory segment corresponding to the write count of 10 in the notification ring is the memory segment at position 2. Among them, Figure 3 The position numbering of the memory segments shown is an exemplary display, and the embodiments of the present application do not limit the position, numbering order and number of the memory segments.
[0122] It should be understood that S301 can be implemented through atomic operations, and the specific atomic operation can be fetch_and_add. After the atomic operation, the B value = A value + 1. For example, the A value of the write count is 0, and the B value of the write count is 1. That is, the memory segment where the publisher A can currently write the control message is the memory segment at position 0. The B value of the write count is the position where the publisher can write the next control message. The publisher can be publisher A or another publisher.
[0123] S302, publisher A writes a control message into memory segment A corresponding to value A of the write count.
[0124] For example, if the number of memory segments N in the notification ring is 8, when the write count A value is 0-7, the position number corresponding to the memory segment A is consistent with the write count value. For example, when the write count A value is 0, the memory segment A can be Figure 3 The memory segment at position 0 is shown in the figure. When the write count A is 7, the memory segment A can be Figure 3 The memory segment at position 7 shown in the figure. When the write count value A is greater than or equal to 8, the position number corresponding to memory segment A is the modulo of the write count value A and the number of memory segments N, that is, the position number corresponding to memory segment A = A value%N. For example, when the write count value A is 8, A value%N is 0, and memory segment A can be Figure 3 The memory segment at position 0 is shown. When the write count value A is 12 and the A value % N is 4, the memory segment A can be Figure 3 The memory fragment at location 4 is shown.
[0125] It should be understood that the control message may be a control message of a fixed length, and the fixed length facilitates locating the starting position of the control message.
[0126] S303, publishing end A obtains the current value of the notification count, compares the current value of the notification count with the A value of the write count to see if they are equal, and when the current value of the notification count is equal to the A value of the write count, updates the current value of the notification count to the B value.
[0127] It should be understood that S303 can also be completed through atomic operations. The specific atomic operation can be comp_and_swap or comp_and_set, referred to as CAS atomic operation. CAS atomic operation first needs to determine whether the current value of the notification count is equal to the A value of the write count. If they are equal, then the current value of the current notification count is updated to the B value. Among them, the current value of the notification count is obtained by the publisher from the notification ring of the shared memory. In the embodiment of the present application, the B value = A value + 1.
[0128] S304, publisher A sends the current value of the notification count to subscriber A.
[0129] S305, subscriber A reads the control message according to the current value of the notification count.
[0130] As a possible implementation method, subscriber A determines that the current read count is A value based on the current value of the notification count, that is, based on the A value of the notification count, and the subscriber can read the control message in memory segment A and the memory segment before memory segment A, where memory segment A is the memory segment at the position corresponding to the A value of the read count.
[0131] For example, when the current value of the notification count is 4, the read count is 4, and memory segment A is Figure 3 If the memory segment at position 4 is shown, then subscriber A can read the control messages on the memory segments at positions 0 to 4 in order. When the current value of the notification count is 10, the read count is 10, then subscriber A can read the control messages in the memory segment at position 2 and the memory segments before position 2. That is, subscriber A can read the control messages in the memory segments at positions 2, 1, 0, 7, 6, 5, 4, and 3.
[0132] Figure 3The writing and reading of the message entity in the shared memory-based inter-process communication method shown only occurs in the case of single receiving and single sending between process 1 and process 2, that is, the writing and reading process of the control message of the notification ring only occurs in the case of single receiving and single sending between process 1 and process 2. When process 3 acts as publisher B, publisher A and publisher B need to concurrently write control messages in the notification ring, after publisher A successfully executes S301 to S303, publisher B writes the control message into the notification ring through similar operations from S301 to S303. When process 1 acts as publisher A and executes S302, process 1 is scheduled for a long time or process 1 is interrupted, publisher A will not be able to perform S303, that is, the current value of the notification count cannot be updated to the B value. This will cause the current value of the notification count to be unequal to the B value of the write count when process 3 acts as publisher B and executes S303, the current value of the notification count cannot be updated, and the notification ring will enter a waiting state trapped in an infinite loop, which will cause the notification ring of the lock-free circular queue to be unavailable.
[0133] An embodiment of the present application shows a method for inter-process communication. When multiple processes need to write control messages to a notification ring at the same time, if the writing of control messages of a certain process is interrupted, the notification ring can be repaired without locking, thereby preventing the use of the shared memory notification ring by other processes from being affected by the interruption of the writing of control messages of a certain process.
[0134] Figure 4 It is a flowchart of an inter-process communication method provided in an embodiment of the present application.
[0135] like Figure 4 As shown, the first publisher can be any process that writes a control message in the notification ring.
[0136] S401, the first publishing end obtains the A value of the write count, and performs an increment operation on the A value to update the A value of the write count to the B value.
[0137] It should be understood that S401 corresponds to S301, and the first publishing end may correspond to publishing end A. For a detailed description of S401, reference may be made to S301.
[0138] For example, the A value of the write count is 6, and after executing S401 , the B value of the write count is 7.
[0139] Optionally, S402, the first publishing end checks whether the first memory segment is valid, the first memory segment being a memory segment corresponding to the A value of the write count in the notification ring.
[0140] Among them, checking whether the first memory segment is valid can also be understood as checking the validity of the first memory segment memory, that is, whether the control message can be written into the first memory segment.
[0141] As a possible implementation manner, the first publisher verifies whether the first memory segment is valid according to the number of writes in the message header corresponding to the first memory segment.
[0142] The message header corresponding to the first memory segment can be used by the subscriber to determine the validity of the control message in the first memory segment. The message header can include a magic word.
[0143] It should be understood that the write count is a newly added field in the message header, and the write count is used to indicate the number of times the first memory segment is written. The write count is also used to indicate the validity of the first memory segment, that is, whether the first memory segment is a writable memory segment or a readable memory segment.
[0144] Exemplarily, if the first memory segment has 64 bytes, the message header corresponding to the first memory segment may be located in the first 16 bytes of the first memory segment, and the last 48 bytes of the first memory segment may be used to write the control message.
[0145] Specifically, when the number of write times is a specific value, the first memory segment is valid; when the number of write times is an illegal value, the first memory segment is invalid. For example, the illegal value may be UINT64_MAX.
[0146] In this way, the number of writes to the memory segment can assist the publisher process to determine whether the memory segment corresponding to the current write count is a writable memory segment, or assist the subscriber process to determine whether the memory segment corresponding to the current notification count is a readable memory segment, thereby improving the reliability of inter-process communication.
[0147] S403a, the first publisher writes a control message into the first memory segment.
[0148] Optionally, when the first memory segment is valid, the first publisher writes a control message in the first memory segment.
[0149] The process of the first publishing end writing the control message in step S403a is similar to that in step S302, and the detailed process can refer to S302.
[0150] S404: The first publishing end updates the number of write times of the first memory segment.
[0151] As a possible implementation method, compare whether the value a of the write count is equal to the theoretical write count, and increase the value a of the write count, wherein the theoretical write count is the number of write cycles of the notification ring obtained according to the value A of the write count.
[0152] Exemplarily, the operation of increasing the value a of the number of write times is implemented through a CAS atomic operation.
[0153] It should be understood that the number of write turns of the notification ring can be obtained according to the A value of the write count. N is the number of memory segments in the notification ring. Theoretically, the number of write cycles corresponding to the A value of the write count should be equal to the a value of the write count.
[0154] For example, the A value of the write count is 10, and the number of memory segments N of the notification ring is 8. According to the above relationship, the number of write cycles is 1. At this time, the first memory segment corresponding to the write count 10 is the memory segment at position 2. During the writing process of the control message of the first cycle of the notification ring, the number of successful writes to the memory segment at position 2 is 1. This can be regarded as the theoretical write count. When the a value of the write count is equal to the theoretical write count, the a value of the write count increases by 1.
[0155] S405, the first publishing end obtains the current value of the notification count, compares the current value of the notification count with the A value of the write count to see if they are equal, and if they are equal, updates the current value of the notification count to the B value.
[0156] It should be understood that S405 is similar to S303, and the first publishing end corresponds to publishing end A. For a detailed explanation, please refer to S303, which will not be described in detail here.
[0157] It should also be understood that since S405 may have timed out, that is, the current value of the notification count may not be successfully updated to the value B, Figure 4 This step is shown in a dashed box.
[0158] S406, when S405 times out, the notification count is repaired, and the B value of the repaired notification count is greater than the A value of the write count.
[0159] It should be understood that after S405 is executed overtime, if the notification count is updated to the value B in step S405, then the current value of the notification count is B. If the notification count is not updated to the value B in step S405, then the current value of the notification count is not B.
[0160] It should also be understood that repairing the notification count can also be understood as repairing the notification count of the notification ring.
[0161] In other words, within the preset time period, if the first atomic operation times out, the notification count is repaired, and the repaired value B of the notification count is greater than the value A of the write count. The specific operation of the first atomic operation may be S405.
[0162] As a possible implementation, a timer is used to determine whether the execution of S405 has timed out, that is, the preset duration is implemented by a timer. The start time point of the timer can be the moment when step S405 starts.
[0163] As a possible implementation manner, by setting the number of loops for the first atomic operation, if the number of loops exceeds the preset number, S405 is executed timed out, that is, the first atomic operation is executed timed out.
[0164] It should be noted that the specific S406 will be Figure 5 It should be understood that all processes using the notification ring can execute S406, that is, the first publishing end can repair the current value of the notification count, and other publishing end processes can also repair the current value of the notification count.
[0165] S407: The first publisher sends the value A of the notification count to the first subscriber.
[0166] The A value of the notification count is equal to the B value of the notification count obtained in S406 minus 1.
[0167] S408: The first subscriber reads the control message according to the value A of the notification count.
[0168] Specifically, the first subscriber determines the read count according to the value A of the notification count, and reads the control message according to the memory segment corresponding to the read count. For detailed explanations of the memory segments corresponding to the control messages that can be read, please refer to Figure 3 .
[0169] Optionally, when the first memory segment is invalid, S403b, the first publishing end obtains the B value of the write count, and performs an increment operation on the B value to update the B value of the write count to the C value.
[0170] The subsequent steps are similar to S402, S403a, S404, S405 and S406, and are not described here for brevity. The only difference is that the first publishing end needs to verify whether the fifth memory segment is valid. The fifth memory segment is the corresponding memory segment in the notification ring when the write count is B value.
[0171] Optionally, when the first memory segment is invalid, the first publisher may reset the number of writes to the first memory segment to a valid value. It should be understood that this optional step is not included in Figure 4 That is, the first publisher can continue to write control messages in the first memory segment to cover the original control messages in the first memory segment.
[0172] The subsequent steps are similar to S402, S403a, S404, S405 and S406, and are not described here for brevity.
[0173] Figure 5 It is a flow chart of a method for repairing a notification count update after timeout provided in an embodiment of the present application.
[0174] It should be understood that Figure 5 The method shown can be executed by multiple processes that need to write control messages to the notification ring, that is, all processes that serve as publishers. Figure 5 The method shown.
[0175] S501, obtaining the current value of the notification count.
[0176] Exemplarily, when the first publishing end executes S501, as described in S406, the current value of the notification count may be the value A indicating a failed update, or the value B indicating a successful update.
[0177] For example, the A value of the notification count is 6, and the B value of the notification count is 7. If the update in S405 fails, the current value of the notification count is 6, and if the update in S405 succeeds, the current value of the notification count is 7.
[0178] For other publishers, the current value of the notification count may be the size of the notification count that can be obtained in the notification ring.
[0179] S502: Determine the position of the memory segment corresponding to the current value of the notification count in the notification ring as the first position.
[0180] Exemplarily, when S502 is executed at the first publishing end, for example, when the current value of the notification count is A value, 6, the first position is position 6 in the notification ring; when the current value of the notification count is B value, 7, the first position is position 7 in the notification ring.
[0181] S503, determine whether the first position is after the second position, wherein the first position is the position of the memory segment corresponding to the current value of the notification count in the notification ring, and the second position is the position of the memory segment where the publisher writes the control message in the notification ring.
[0182] The first position not being after the second position can be understood as the first position being at the second position, or the first position being before the second position.
[0183] For example, the first publisher repairs the notification count, the A value of the write count is 6, the memory segment where the first publisher writes the control message is at position 6 in the notification ring, and the second position is at position 6. If the update in S405 fails, the current value of the notification count is 6, the first position is also at position 6, and the first position is at the second position. At this time, the judgment result of S503 is "No". If the update in S405 succeeds, the current value of the notification count is 7, the first position is at position 7, and the first position is after the second position. At this time, the judgment result of S503 is "Yes".
[0184] For example, the second publisher repairs the notification count. After the first publisher executes S401, the second publisher can obtain the B value of the write count as 7. The memory segment where the second publisher writes the control message is located at position 7 in the notification ring, and the second position is located at position 7. If the first publisher fails to update when executing S405, then for the second publisher, the current value of the notification count is 6, the first position is position 6, and the first position is before the second position. At this time, the judgment result of S503 is "No". If the first publisher updates successfully when executing S405, and the second publisher also successfully updates the value of the notification count after writing the control message, then the current value of the notification count is 8, the first position is position 8, and the first position is after the second position. At this time, the judgment result of S503 is "Yes".
[0185] If the judgment result of S503 is "yes", then steps S504 to S506 are executed.
[0186] If the judgment result of S503 is "No", then execute step S507.
[0187] The following will first describe steps S504 to S506 in detail, and then describe step S507 in detail.
[0188] S504: When the first memory segment corresponding to the A value of the write count has not completed the writing process of the control message, determine the first memory segment as a memory segment that cannot be written or read.
[0189] As a possible implementation, compare whether the number of writes to the first memory segment is equal to the number of write cycles corresponding to the A value of the write count. If they are equal, the number of writes is assigned a preset value. The preset value is used to indicate that the first memory segment is an unreadable or unwritable memory segment. For example, the preset value is an illegal value UINT64_MAX. It should be understood that S504 can also be implemented by an atomic operation CAS.
[0190] The number of write times z can be understood as the number of times the first memory segment is written. The number of write times of the memory segment is used to verify the validity of the memory segment.
[0191] It should be understood that the number of writes to the memory segment in the notification ring gradually increases as the control message is written.
[0192] For example, the number of writes to the memory segment at position 0 is z. After completing the writing process of a control message to the memory segment at position 0, the number of writes to the memory segment at position 0 is updated to z + 1. If the control message does not complete the writing process of the memory segment at position 0, the number of writes to the memory segment at position 0 is still z.
[0193] Among them, the number of write turns y corresponding to the A value of the write count satisfies the following relationship: Where N is the number of memory fragments in the notification ring.
[0194] Exemplarily, N is 8. When the A value of the write count is 0 to 7, the number of write cycles of each memory segment in the notification ring is 0. When the A value of the write count is 8 to 15, the number of write cycles of each memory segment in the notification ring is 1. And so on, which will not be enumerated here.
[0195] For example, when the value of A of the write count is 6, if the writing process of the control message has not been completed on the first memory segment at position 6, the number of writes z of the first memory segment is 0. If N is 8, the value of A of the write count is 6, and the number of write turns y corresponding to the value of A of the write count is 0, then z=y, and the number of writes of the first memory segment is updated to a preset value, for example, the preset value may be UINT64_MAX. If the writing process of the control message is completed on the first memory segment at position 6, the number of writes z of the first memory segment is 1, then z≠y, and the second number of writes will not be updated to the preset value.
[0196] S505: perform an increment operation on the current value of the notification count to update the current value of the notification count to the A' value.
[0197] As a possible implementation, the current value of the notification count is updated to the A' value through the atomic operation of fetch_and_add. The A' value can be 1 greater than the current value.
[0198] Exemplarily, when the current value of the notification count is 6, the value of A′ of the notification count may be 7.
[0199] S506: Determine the position corresponding to the A' value of the notification count as a new first position.
[0200] Exemplarily, the value of A' may be 7, and the new first position is at position 7 in the notification ring.
[0201] Use the new first position determined in S506 to execute S503, that is, compare the new first position and the second position again. If the new first position is not after the second position, loop S504 to S506 until the updated first position is after the second position.
[0202] When the first position is after the second position, S507, the value of the notification count corresponding to the first position is determined as the B value of the notification count. The B value is greater than the A value.
[0203] It should be understood that each process acting as a publisher can Figure 5 The repair process shown is completed.
[0204] In the above technical solution, in the case where a publishing process is interrupted in writing a control message, after the notification count update process times out, any process acting as a publishing end can repair the notification count of the notification ring. The updated notification count can enable the notification ring of the lock-free circular queue to be used normally, thereby improving the reliability of inter-process communication.
[0205] For example, through the above solution, the central processing unit (CPU) overhead of 0.5 cores is reduced to about 0.1 cores, an increase of about 4 times. The overall communication performance is improved from 1 core to about 0.6 cores, and the end-to-end performance is improved by about 67%.
[0206] Currently, the subscriber process obtains control messages by reading the notification ring and addresses the message entity in the shared memory based on the parsing result of the control message. However, when the subscriber reads the control message in the notification ring, there is a situation where the control message is not read in time. At this time, the cache of the notification ring is full, which will cause the publisher process to be unable to write new control messages to the notification ring.
[0207] Figure 6 It is a flowchart of another inter-process communication method provided in an embodiment of the present application.
[0208] S601, a first subscriber obtains a write count and a read count.
[0209] It should be understood that the write count is the latest write count of the notification ring. The read count can be the count of the first subscriber reading the notification ring. The read count can be less than or equal to the current value of the notification count.
[0210] It should be understood that the write count can be used as a critical section, that is, the first subscriber can obtain the latest write count in the notification ring.
[0211] S602, determining whether the write count and the read count satisfy a first relationship.
[0212] In other words, according to the write count and the read count, it is determined whether the control message A corresponding to the read count is valid.
[0213] It should be understood that, assuming that the read count is c, the write count is b, and the size of the notification ring is N, the first relationship may be b<c+N. N is a positive integer, for example, N may be 8, 128, 256, and the like.
[0214] As a possible implementation manner, if the read count and the write count satisfy the first relationship, then the control message A corresponding to the read count is valid.
[0215] In other words, the control message A corresponding to the read count is valid, which means that the process of the publishing end process writing the control message in the notification ring has not yet circled, that is, the message entity corresponding to the address of the parsing result of the control message A is message entity A.
[0216] When the control message A is valid, S603, the first subscriber obtains the message entity from the shared memory according to the control message corresponding to the read count.
[0217] As a possible implementation, the first subscriber obtains the control message A corresponding to the read count from the notification ring, parses the control message to obtain a parsing result, and then addresses and reads the message entity A corresponding to the control message A from the shared memory according to the parsing result.
[0218] As a possible implementation manner, if the read count and the write count do not satisfy the first relationship, the control message A corresponding to the read count is invalid.
[0219] In other words, the control message A corresponding to the read count is invalid, which means that the process of writing the control message in the notification ring by the publishing end process has gone around in circles, that is, the memory segment B corresponding to the read count is being written, or the memory segment B corresponding to the read count has been overwritten by a new control message. In other words, the shared memory address obtained by parsing the control message in the memory segment B is not the message entity A corresponding to the control message A.
[0220] Optionally, if the control message A is invalid, the first subscriber may not parse the control message A in the memory segment corresponding to the read count. It should be understood that this step is not Figure 5 Shown in.
[0221] Optionally, after obtaining the read count, S604, the first subscriber parses the control message A corresponding to the read count to obtain a parsing result.
[0222] Optionally, when the control message A is valid, as another possible implementation of S603, the first subscriber addresses and obtains the message entity A corresponding to the control message A from the shared memory according to the parsing result of S604.
[0223] Optionally, when the control message A is invalid, S605, the first subscriber abandons addressing and reading the message entity from the shared memory according to the parsing result.
[0224] In the above technical solution, when the subscriber process reads the control message slowly, the notification ring cache is full, and the publisher process cannot continue to write the control message into the notification ring. Through the above solution, the publisher process does not need to perceive whether the notification ring is full, and the impact of the slow reading of the subscriber process will not spread to the publisher. The above technical solution can ensure that the writing of the control message of the publisher process is not affected by the reading of the subscriber process, and while realizing the decoupling of the publisher process and the subscriber process, it ensures that the subscriber reads the correct message entity according to the control message, that is, it ensures the reliability of inter-process communication.
[0225] Currently, the release of message entities in shared memory needs to be achieved through reference counting. The detailed process is in Figure 1 The relevant description is given.
[0226] For example, the reference count of message entity A is 3, and subscriber A reads message entity A once, and the reference count of message entity A is modified to 2; subscriber B ( Figure 1 (not given in the table) reads message entity A once, and the reference count of message entity A is changed to 1; subscriber C ( Figure 1 (not given) reads message entity A once, and the reference count of message entity A is modified to 0. At this point, message entity A is completely released, that is, the shared memory with message entity A is released. Subscriber A, subscriber B, and subscriber C all have the authority to modify the reference count of message entity A in the shared memory. If one of the subscribers is hijacked, and the hijacked subscriber maliciously modifies the reference count of message entity A, it will cause abnormal reading of message entity A by other subscribers. For example, subscriber A is hijacked, and the reference count of subscriber A's message entity is modified to 0. At this time, the shared memory originally with message entity A is released, and publisher A or other publishers can write a new message entity in the shared memory. When subscriber B addresses the shared memory according to the parsing result of the control message A, it obtains the new message entity instead of the original message entity A.
[0227] Figure 7 It is a flow chart of a method for releasing a message entity provided in an embodiment of the present application.
[0228] S701: A first publisher obtains a first index number of a first subscriber and a second index number of a second subscriber from a shared memory.
[0229] It should be understood that the first index number is used to indicate the index number of the message entity held by the first subscriber, or it can also be called the sequence number of the message entity held by the first subscriber. Correspondingly, the second index number is used to indicate the index of the message entity held by the second subscriber, or it can also be called the sequence number of the message entity held by the second subscriber.
[0230] The first index number may include the index numbers of one or more message entities, and similarly, the second index number may include the index numbers of one or more message entities.
[0231] It should be understood that the index number of the message entity corresponds to the write count of the control message written in the notification ring. For example, the message entity corresponding to the control message #100 is the message entity #100, the corresponding write count when writing the control message #100 is 100, and the index number of the message entity #100 is #100.
[0232] The message entity held by the first subscriber can be understood as a message entity that the first subscriber addresses through a control message but does not perform a read operation on.
[0233] Exemplarily, the first subscriber holds the indexes of message entity A and message entity B, the indexes of the two message entities are #100 and #200 respectively, and the first index number includes index #100 and index #200. The second subscriber holds the indexes of message entity C and message entity B, the indexes of the two message entities are #194 and #200 respectively, and the second index number includes index #194 and index #200.
[0234] S702: The first publishing end determines a message entity to be released according to the first index number and the second index number.
[0235] The index number of the message entity to be released is the index number of the message entity in the shared memory that the first publishing end is going to release.
[0236] Method 1: The first index number is the first minimum index number among the message entities held by the first subscriber, and the second index number is the second minimum index number among the message entities held by the second subscriber. The first publisher traverses the first minimum index number and the second minimum index number, and determines the message entity before the message entity with the smallest index number as the message entity to be released.
[0237] Exemplarily, the first index number includes index #100, the second index number includes index #194, the first publishing end traverses index #100 and index #194, the smallest index number is #100, and the message entities corresponding to indexes #0 to #99 are determined as message entities to be released.
[0238] In this way, the first publisher has read and write permissions to the shared memory, and the subscriber only has read permissions to the shared memory, reducing the number of processes with read and write permissions to the shared memory, which can improve the reliability of inter-process communication to a certain extent.
[0239] Method 2: The first publishing end obtains the last traversal result of the first publishing end, the smallest index number among the index numbers included in the first index number and the second index number, and determines the message entity before the smallest index number as the message entity to be released.
[0240] It should be understood that the smallest index number can be determined by the first publisher after traversing the index numbers included in the first index number and the second index number and saved in the shared memory heap. Therefore, the first publisher can directly obtain the smallest index number from the shared memory heap.
[0241] Exemplarily, the smallest index number obtained by the first publishing end is #100, and the message entities A corresponding to index numbers #0 to #99 are determined as the message entities to be released.
[0242] In this way, the first publishing end does not need to traverse the index number included in the received message index multiple times, which can save computing power to a certain extent, thereby speeding up the determination of the message entity to be released.
[0243] As a possible implementation manner, the first publishing end may determine the message entity to be released by combining the first manner and the second manner.
[0244] Exemplarily, the first publishing end executes method 1 once and method 2 three times to determine the message entity to be released.
[0245] When the message entity to be released determined in the first and second modes is the same message entity within a preset time period, the first publisher determines that the subscriber abnormally holds the message entity.
[0246] Method three: determining the message entity to be released according to the first index number, the second index number, the A value of the write count and the number of buffers in the notification ring. The A value of the write count is the current write count.
[0247] Specifically, according to the A value and the number of caches in the notification ring, the first message entity set is determined, and the first message entity set is the message entity corresponding to the control message included in the notification ring. According to the first index number and the second index number, the second message entity set is determined, and the second message entity set is the message entity held by the first subscriber and the second subscriber. Message entities other than the first message entity set and the second message entity set are determined as message entities to be released.
[0248] The number of caches of the notification ring may be less than or equal to the number of memory segments of the notification ring. For example, if the number N of memory segments of the notification ring is 32, the number of caches of the notification ring may be configured as 10 or 32.
[0249] Exemplarily, the current write count is 201, the number of caches in the notification ring is 10, the first index number includes index #100 and index #200, and the second index number includes index #189 and index #200. The index numbers corresponding to the first message entity set are #192 to #201. The index numbers corresponding to the second message entity set are #100, #189, and #200. Therefore, the index numbers corresponding to the message entities to be released are #0 to #99, #101 to #188, and #190 to #191.
[0250] Exemplarily, the current write count is 201, the number of caches in the notification ring is 32, the first index number includes index #100 and index #200, and the second index number includes index #189 and index #200. The index numbers corresponding to the first message entity set are #170 to #201, and the index numbers corresponding to the second message entity set are #100, #189, and #200. Therefore, the index numbers corresponding to the message entities to be released are #0 to #99 and #101 to #169.
[0251] Optionally, the second publisher receives a third index number from the first subscriber, wherein a message entity corresponding to the third index number and a message entity corresponding to the first index number are located in different shared memories.
[0252] Optionally, the number of message entities held by the first subscriber is less than or equal to a first threshold, and the number of message entities held by the second subscriber is less than or about a second threshold.
[0253] S703: The first publishing end releases the shared memory corresponding to the message entity to be released.
[0254] In the above technical solution, the publishing process can uniformly release the memory space corresponding to the message entity in the shared memory by obtaining the index number of the message entity held by the subscriber process, thereby reducing the read and write permissions of the subscriber process on the shared memory, thereby avoiding the subscriber process being affected by the attack on the reading of the message entity by other subscriber processes, thereby improving the reliability of inter-process communication.
[0255] Figure 8 It is a flowchart of an inter-process communication method provided in an embodiment of the present application.
[0256] S810, obtaining a first value of a write count, where the first value of the write count is used to indicate a position where a publishing end process writes a first memory segment corresponding to a first control message in a notification ring.
[0257] It should be understood that the first value may be Figure 4The first value of the write count may be obtained and saved when the atomic operation of S401 is executed. After the atomic operation of S401, the value obtained is the value of the write count plus 1 based on the first value.
[0258] S820, within the preset time period, if the first atomic operation times out, the notification count is repaired, and the second value of the repaired notification count is greater than the first value. The first atomic operation includes updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value.
[0259] The current value of the notification count is used to indicate the positions of the X memory segments corresponding to the X control messages read by the subscriber process in the notification ring. The X control messages correspond to the X memory segments one by one, and X is a positive integer.
[0260] It should be understood that the first atomic operation can be Figure 4 The specific implementation of the subscriber process reading X control messages according to the current value of the notification count can refer to S305.
[0261] When the execution subject is the first publisher, that is, the execution subject is the publisher process of the write control message interrupt, the execution of the first atomic operation times out, which can be understood as the first atomic operation is not executed.
[0262] When the execution subject is the second publisher, that is, when the execution subject is a publisher process other than the publisher process interrupted by the write control message, the first atomic operation execution times out, which can be understood as the value of the second publisher update notification count times out within the preset time.
[0263] Optionally, the position of the second memory fragment corresponding to the current value of the notification count in the notification ring is determined as the first position. The position of the first memory fragment in the notification ring is determined as the second position. When the first position is not after the second position, the current value of the notification count is updated to the second value.
[0264] It should be understood that the detailed process can be referred to Figure 5 S502 and S503.
[0265] Optionally, the current value is updated to a third value, the position of the third memory segment corresponding to the third value in the notification ring is a third position, and in the notification ring, the third position is between the first position and the second position. The third value is updated to the second value.
[0266] It should be understood that the third value can be Figure 5The A' value in S505, that is, the process of updating the current value of the notification count to the second value, may be updated through an intermediate value. That is, when the first position is not after the second position, the update of the notification count value needs to be updated through a loop from S503 to S506.
[0267] Optionally, the message header corresponding to the first memory segment includes the number of write times of the first memory segment, and the number of write times is the number of times the first memory segment is written. The method also includes: when the first position is not after the second position and the process of writing the control message in the first memory segment fails, the number of write times of the first memory segment is updated to a preset value, and the preset value is used to indicate that the first memory segment is a non-writable or non-readable memory segment.
[0268] It should be understood that the explanation on updating the number of write times of the first memory segment to the preset value may refer to S504.
[0269] Optionally, the message header corresponding to the first memory segment includes a write count of the first memory segment, where the write count is the number of times the first memory segment is written, and the method further includes: verifying the validity of the first memory segment according to the write count. If the first memory segment is valid, writing the first control message in the first memory segment. The write count is updated.
[0270] It should be understood that reference may be made to S402 for determining the validity of the first memory segment based on the number of times the first memory segment is written.
[0271] Optionally, a first index number is obtained from a first subscriber, the first index number being used to indicate a message entity held by the first subscriber. A second index number is obtained from a second subscriber, the second index number being used to indicate a message entity held by the second subscriber. The message entity to be released is determined according to the first index number and the second index number.
[0272] It should be understood that Figure 4 and Figure 5 Based on the method shown, it can also be combined with Figure 7 The method shown. In this way, the reliability of inter-process communication can be further improved. For a detailed explanation of the first index number and the second index number, please refer to Figure 7 .
[0273] Optionally, a fourth value of a read count is obtained, where the read count is used to indicate a position in the notification ring where the subscriber process reads the second control message. According to the write count and the read count, it is determined whether to read a message entity corresponding to the second control message in the shared memory.
[0274] It should be understood that Figure 4 and Figure 5 Based on the method shown, it can also be combined Figure 6The method shown, or, in Figure 4 , Figure 5 and Figure 7 On the basis of Figure 6 The method shown in FIG. 1 can further improve the reliability of inter-process communication. For a detailed process of determining whether to read the message entity corresponding to the second control message in the shared memory, please refer to Figure 6 .
[0275] The following will be combined Fig. 9 and Fig.10 Detailed description of the software architecture to which the above method can be applied.
[0276] Fig. 9 A software architecture for implementing inter-process communication through kernel-mode and user-mode interaction is provided in an embodiment of the present application.
[0277] First, the publisher needs to publish services to the kernel object (KO), and correspondingly, the subscriber needs to subscribe to services from the kernel module.
[0278] Specifically, in the first step, the kernel module is inserted and the device is created. The kernel module is initialized and returns the kernel module initialization result. In the second step, the publisher publishes the service to the kernel module. The kernel module initializes the service resources and returns the service publishing result. In the third step, the subscriber subscribes to the service from the kernel module. The kernel module returns the service information to the subscriber, and then the subscriber adds the returned fd parameter to epoll. epoll is an implementation of kernel I / O multiplexing. This process is not described in Fig. 9 For example, the publisher A publishes the service, and the subscriber A subscribes to the service.
[0279] It should be understood that the second and third steps are not performed in a particular order in a distributed scenario, and the service can be subscribed to first and then registered.
[0280] Secondly, while the publisher writes the message entity into the shared memory, it writes the control message into the notification ring through the kernel module; accordingly, the subscriber reads the control message from the notification ring and reads the message entity according to the parsed address of the control message.
[0281] Among them, the kernel module in the kernel state acts as a global management end to store the link establishment information of the message publishing end and the subscription end.
[0282] Specifically, Fig. 9As shown, in the first step, the publisher calls the interface to apply for the memory space of the message entity, and then writes the message entity in the shared memory. In the second step, the publisher calls the interface to notify the subscriber to receive the control message, and the kernel module returns the result of the control message to the publisher. In the third step, the subscriber calls the interface to read the control message, and returns after parsing the address of the control message. In the fourth step, the publisher calls the interface to obtain the index number of the message entity held by the publisher. In the fifth step, the publisher calls the interface to release the message entity in the shared memory.
[0283] For example, Fig. 9 As shown, while publisher A writes message entity #1 into SHM #1, it writes control message #1 into the notification ring of shared memory through the kernel module. Subscriber A calls the interface to read control message #1, parses control message #1 to obtain the parsing address, and obtains message entity #1 from SHM #1 according to the parsing address.
[0284] It should be understood that if an exception occurs in the second step and the publishing end is interrupted from writing control messages, causing the notification ring to be unable to be used normally, you can Figure 4 and Figure 5 method or Figure 8 The notification ring can be repaired by using the method
[0285] In the third step, when the subscriber reads the control message slowly, which affects the publisher to write new control messages, you can use Figure 6 method.
[0286] To solve the problem that all processes have too much permissions on shared memory, the fourth and fifth steps are used to release the message entities in the shared memory. For details, please refer to Figure 7 method.
[0287] Finally, when the service stops, the publisher needs to unpublish the service, the subscriber needs to unsubscribe from the service, and the kernel module can be removed.
[0288] Specifically, in the first step, the publisher cancels the service publishing, the kernel module cleans up the publisher-related resources and marks that the service has been deregistered and returns the deregistration result. In the second step, the subscriber cancels the service subscription, the kernel module cleans up all resources under the service and cleans up the subscriber-related resources and returns the deregistration result. In the third step, the kernel module is removed, the kernel module cleans up the related resources and returns the removal result. This process is not included in Fig. 9 Shown in.
[0289] It should be understood that in a distributed scenario, there is no order of priority. Only after all subscribers and publishers under the service have registered, the public resources of the service will be released.
[0290] Fig.10It is a software architecture for implementing inter-process communication through user mode provided in an embodiment of the present application.
[0291] Fig.10 and Fig. 9 The difference is that Fig.10 It is an inter-process communication based on shared memory implemented in a pure user state environment. The user state includes management modules, such as Fig.10 As shown, its role and Fig. 9 The kernel modules in the , except that the specific implementation methods of publishing services, subscribing services, unpublishing and unsubscribing are different. The following describes these differences in detail. Fig. 9 Similar, for details, please refer to Fig. 9 .
[0292] First, the publisher needs to publish services to the user-state management module, and correspondingly, the subscriber needs to subscribe to services from the user-state management module.
[0293] Specifically, in the first step, the system starts the trusted process of the management module. In the second step, the publisher publishes services to the management module through a socket. The management module initializes service resources and returns service publishing results. In the third step, the subscriber subscribes to services from the management module through a socket. The management module returns service information to the subscriber, and then the subscriber manages the shared memory created by the management module. This process is not described in Fig.10 For example, the publisher A publishes the service, and the subscriber A subscribes to the service.
[0294] It should be understood that the second and third steps are not performed in a particular order in a distributed scenario, and the service can be subscribed to first and then registered.
[0295] Secondly, the publisher writes the message entity into the shared memory and writes the control message into the notification ring at the same time; accordingly, the subscriber reads the control message from the notification ring and reads the message entity according to the parsed address of the control message.
[0296] It should be understood that the detailed process and Fig. 9 Similar, you can refer to Fig. 9 .
[0297] Finally, when the service stops, the publisher needs to cancel the service publishing, the subscriber needs to cancel the service subscription, and the process of the management module needs to be taken offline.
[0298] Specifically, in the first step, the publisher cancels the service publishing to the management module through the socket. The management module cleans up the resources related to the publisher and marks that the service has been deregistered and returns the deregistration result. In the second step, the subscriber cancels the service subscription to the management module through the socket. The management module cleans up all resources under the service and the resources related to the subscriber and returns the deregistration result. In the third step, the process of the management module is offline. This process is not in Fig.10 Shown in.
[0299] It should be understood that in a distributed scenario, there is no order of priority. Only after all subscribers and publishers under the service have registered, the public resources of the service will be released.
[0300] The above content is the inter-process communication method of the embodiment of the present application. Fig.11 and Fig.12 The inter-process communication device is described in detail. It should be understood that the description of the device embodiment and the description of the method embodiment correspond to each other. Therefore, the contents not described in detail can be referred to the method embodiment above, and for the sake of brevity, they are not repeated here.
[0301] The embodiments of the present application also provide a device for implementing any of the above methods, for example, providing a device including a unit (or means) for implementing each step performed by the intelligent driving device or computing platform in any of the above methods. For another example, providing a device including a unit (or means) for implementing each step performed in the robot system in any of the above methods.
[0302] Fig.11 1 is a schematic block diagram of an inter-process communication device 1100 provided in an embodiment of the present application. Fig.11 As shown, the device 1100 includes: an acquisition unit 1110 is used to acquire a first value of a write count, and the first value of the write count is used to indicate the position of the first memory fragment corresponding to the first control message written by the publishing end process in the notification ring. The processing unit 1120 is used to repair the notification count within a preset time length if the first atomic operation is executed overtime, and the second value of the repaired notification count is greater than the first value, and the first atomic operation includes updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value. Among them, the current value of the notification count is used to indicate the position of the X memory fragments corresponding to the X control messages read by the subscribing end process in the notification ring, and the X control messages correspond to the X memory fragments one by one, and X is a positive integer.
[0303] It should be understood that where the device implementation method is not described in detail, reference can be made to the above method embodiment, and no further details will be given here.
[0304] The functions implemented by the above acquisition unit 1110 and the functions implemented by the processing unit 1120 can be implemented by different processors respectively, or some functions can be implemented by the same processor, or all functions can be implemented by the same processor, which is not limited to the embodiments of the present application.
[0305] The functions implemented by the acquisition unit 1110 may also be implemented by a transceiver unit.
[0306] It should be understood that the division of the units in the above device is only a division of logical functions, and in actual implementation, they can be fully or partially integrated into one physical entity, or they can be physically separated. In addition, the units in the device can be implemented in the form of a processor calling software; for example, the device includes a processor, the processor is connected to a memory, and instructions are stored in the memory. The processor calls the instructions stored in the memory to implement any of the above methods or implement the functions of the units of the device, wherein the processor is, for example, a general-purpose processor, such as a CPU or a microprocessor, and the memory is a memory inside the device or a memory outside the device. Alternatively, the units in the device can be implemented in the form of hardware circuits, and the functions of some or all of the units can be realized by designing the hardware circuits, and the hardware circuits can be understood as one or more processors; for example, in one implementation, the hardware circuit is an application-specific integrated circuit (ASIC), and the functions of some or all of the above units are realized by designing the logical relationship of the components in the circuit; for another example, in another implementation, the hardware circuit can be realized by a programmable logic device (PLD), taking a field programmable gate array (FPGA) as an example, which can include a large number of logic gate circuits, and the connection relationship between the logic gate circuits is configured by a configuration file, so as to realize the functions of some or all of the above units. All units of the above devices can be realized in the form of a processor calling software, or in the form of hardware circuits, or in part by a processor calling software, and the rest by hardware circuits.
[0307] In the embodiment of the present application, the processor is a circuit with the ability to process signals. In one implementation, the processor can be a circuit with the ability to read and run instructions, such as a CPU, a microprocessor, a graphics processing unit (GPU) (which can be understood as a microprocessor) or a digital signal processor (DSP), etc.; in another implementation, the processor can implement certain functions through the logical relationship of the hardware circuit, and the logical relationship of the hardware circuit is fixed or reconfigurable, such as a hardware circuit implemented by an ASIC or PLD, such as an FPGA. In a reconfigurable hardware circuit, the process of the processor loading a configuration document to implement the hardware circuit configuration can be understood as the process of the processor loading instructions to implement the functions of some or all of the above units. In addition, it can also be a hardware circuit designed for artificial intelligence, which can be understood as an ASIC, such as a neural network processing unit (NPU), a tensor processing unit (TPU), a deep learning processing unit (DPU), etc.
[0308] It can be seen that each unit in the above device can be one or more processors (or processing circuits) configured to implement the above method, such as: CPU, GPU, NPU, TPU, DPU, microprocessor, DSP, ASIC, FPGA, or a combination of at least two of these processor forms.
[0309] In addition, all or part of the units in the above device can be integrated together, or can be implemented independently. In one implementation, these units are integrated together and implemented in the form of a system-on-a-chip (SOC). The SOC may include at least one processor for implementing any of the above methods or implementing the functions of each unit of the device. The type of the at least one processor may be different, for example, including a CPU and an FPGA, a CPU and an artificial intelligence processor, a CPU and a GPU, etc.
[0310] It can be seen that each unit in the above device can be one or more processors (or processing circuits) configured to implement the above method, such as: CPU, GPU, NPU, TPU, DPU, microprocessor, DSP, ASIC, FPGA, or a combination of at least two of these processor forms.
[0311] In addition, all or part of the units in the above device can be integrated together, or can be implemented independently. In one implementation, these units are integrated together and implemented in the form of a SOC. The SOC may include at least one processor for implementing any of the above methods or implementing the functions of each unit of the device. The type of the at least one processor may be different, for example, including a CPU and an FPGA, a CPU and an artificial intelligence processor, a CPU and a GPU, etc.
[0312] Fig.12 It is a structural block diagram of an inter-process communication device 1200 provided in an embodiment of the present application.
[0313] Fig.12 The inter-process communication device 1200 shown may include: a processor 1210, a transceiver 1220, and a memory 1230. The processor 1210, the transceiver 1220, and the memory 1230 are connected via an internal connection path, the memory 1230 is used to store instructions, the processor 1210 is used to execute the instructions stored in the memory 1230, and the transceiver 1220 receives / sends part of the parameters. Optionally, the memory 1230 may be coupled to the processor 1210 via an interface, or may be integrated with the processor 1210.
[0314] It should be noted that the transceiver 1220 may include but is not limited to a transceiver device such as an input / output interface to achieve communication between the device 1200 and other devices or a communication network.
[0315] In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in the processor 1210 or an instruction in the form of software. The method disclosed in conjunction with the embodiment of the present application can be directly embodied as a hardware processor for execution, or a combination of hardware and software modules in the processor for execution. The software module can be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 1230, and the processor 1210 reads the information in the memory 1230 and completes the steps of the above method in conjunction with its hardware. To avoid repetition, it is not described in detail here.
[0316] The processor 1210 may be a general-purpose CPU, microprocessor, ASIC, GPU or one or more integrated circuits for executing related programs to implement the inter-process communication method of the method embodiment of the present application. The processor 1210 may also be an integrated circuit chip with signal processing capabilities. In the specific implementation process, the various steps of the inter-process communication method of the present application may be completed by the hardware integrated logic circuit or software instructions in the processor 1210. The above-mentioned processor 1210 may also be a general-purpose processor, DSP, ASIC, FPGA or other programmable logic device, discrete gate or transistor logic device, discrete hardware component. The various methods, steps and logic block diagrams disclosed in the embodiments of the present application may be implemented or executed. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The steps of the method disclosed in the embodiments of the present application may be directly embodied as being executed by a hardware decoding processor, or may be executed by a combination of hardware and software modules in a decoding processor. The software module may be located in a mature storage medium in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 1230, and the processor 1210 reads the information in the memory 1230 and executes the inter-process communication method of the method embodiment of the present application in combination with its hardware.
[0317] The memory 1230 may be a read only memory (ROM), a static storage device, a dynamic storage device or a random access memory (RAM).
[0318] The transceiver 1220 uses a transceiver device such as, but not limited to, a transceiver to implement communication between the apparatus 1200 and other devices or a communication network.
[0319] An embodiment of the present application further provides an intelligent driving device, which may include the above-mentioned inter-process communication device 1100 or the inter-process communication device 1200.
[0320] Optionally, the intelligent driving device may be a vehicle.
[0321] The embodiment of the present application further provides a computer program product, which includes: a computer program code, and when the computer program code is executed on a computer, the computer executes the above method.
[0322] The embodiment of the present application further provides a computer-readable medium, wherein the computer-readable medium stores a program code. When the computer program code is executed on a computer, the computer executes the above method.
[0323] In the implementation process, each step of the above method can be completed by an integrated logic circuit of hardware in a processor or an instruction in the form of software. The method disclosed in conjunction with the embodiment of the present application can be directly embodied as a hardware processor for execution, or a combination of hardware and software modules in a processor for execution. The software module can be located in a storage medium mature in the art such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or a power-on erasable programmable memory, a register, etc. The storage medium is located in a memory, and the processor reads the information in the memory and completes the steps of the above method in conjunction with its hardware. To avoid repetition, it is not described in detail here.
[0324] It should be understood that in the embodiment of the present application, the memory may include a read-only memory and a random access memory, and provide instructions and data to the processor.
[0325] It should also be understood that in the various embodiments of the present application, the size of the serial numbers of the above-mentioned processes does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present application.
[0326] Those of ordinary skill in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.
[0327] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and units described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0328] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0329] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0330] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0331] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in the various embodiments of the present application. The aforementioned storage media include: various media that can store program codes, such as USB flash drives, mobile hard disks, read-only memories ROM, random access memories RAM, magnetic disks or optical disks.
[0332] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any person skilled in the art who is familiar with the present technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. An inter-process communication method, characterized in that: include: Obtaining a first value of a write count, where the first value is used to indicate a location where a publishing end process writes a first memory segment corresponding to a first control message in a notification ring; Within a preset time period, if the first atomic operation times out, the notification count is repaired, and the second value of the repaired notification count is greater than the first value, and the first atomic operation includes updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value; The current value of the notification count is used to indicate the positions of the X memory segments corresponding to the X control messages read by the subscriber process in the notification ring, the X control messages correspond to the X memory segments one-to-one, and X is a positive integer.
2. The method according to claim 1, characterized in that The repairing of the notification count comprises: Determine the position of the second memory fragment corresponding to the current value of the notification count in the notification ring as the first position; Determine the position of the first memory fragment in the notification ring as a second position; When the first position is not after the second position, the current value of the notification count is updated to the second value.
3. The method according to claim 2, characterized in that The updating the current value of the notification count to the second value comprises: updating the current value to a third value, wherein the position of a third memory segment corresponding to the third value in the notification ring is a third position, and in the notification ring, the third position is between the first position and the second position; The third value is updated to the second value.
4. The method according to claim 2 or 3, characterized in that: The message header corresponding to the first memory segment includes the number of times the first memory segment is written, where the number of times the first memory segment is written, and the method further includes: After the first position is not in the second position and when writing the first control message in the first memory segment fails, the number of write times of the first memory segment is updated to a preset value, where the preset value is used to indicate that the first memory segment is a non-writable or non-readable memory segment.
5. The method according to any one of claims 1 to 3, characterized in that: The message header corresponding to the first memory segment includes the number of times the first memory segment is written, where the number of times the first memory segment is written, and the method further includes: Verifying the validity of the first memory segment according to the number of write times; If the first memory segment is valid, writing the first control message into the first memory segment; The number of write times is updated.
6. The method according to any one of claims 1 to 5, characterized in that: The method further comprises: Obtaining a first index number from a first subscriber, where the first index number is used to indicate a message entity held by the first subscriber; Obtaining a second index number from a second subscriber, where the second index number is used to indicate a message entity held by the second subscriber; A message entity to be released is determined according to the first index number and the second index number.
7. The method according to claim 6, characterized in that The first index number is the first smallest index number among the message entities held by the first subscriber; the second index number is the second smallest index number among the message entities held by the second subscriber, The determining, according to the first index number and the second index number, the message entity to be released comprises: The message entity corresponding to the index number before the smallest index number between the first minimum index number and the second minimum index number is determined as the message entity to be released.
8. The method according to claim 7, characterized in that The determining, according to the first index number and the second index number, the message entity to be released comprises: Obtain the minimum index number; The message entity corresponding to the index number before the smallest index number is determined as the message entity to be released.
9. The method according to claim 6, characterized in that The determining, according to the first index number and the second index number, the message entity to be released comprises: Determine the message entity to be released according to the first value, the first index number, the second index number and the number of caches in the notification ring; Among them, the first index number is a set of index numbers of message entities held by the first subscriber, the second index number is a set of index numbers of message entities held by the second subscriber, and the cache quantity of the notification ring is less than or equal to the number of memory fragments of the notification ring.
10. The method according to claim 9, characterized in that The determining the message entity to be released according to the first value, the first index number, the second index number, and the number of caches in the notification ring includes: Determine, according to the first value and the number of caches in the notification ring, a first message entity set, where the first message entity set is a message entity corresponding to a control message in the notification ring; According to the first index number and the second index number, determining a second message entity set, where the second message entity set is message entities held by the first subscriber and the second subscriber; Message entities other than the first message entity set and the second message entity set are determined as the message entities to be released.
11. The method according to any one of claims 6 to 10, characterized in that: The number of message entities held by the first subscriber is less than or equal to a first threshold, and the number of message entities held by the second subscriber is less than or equal to a second threshold.
12. The method according to any one of claims 1 to 11, characterized in that: include: Obtaining a fourth value of the read count, where the read count is used to indicate a position at which the subscriber process reads the second control message in the notification ring, and the fourth value of the read count is less than or equal to a current value of the notification count; According to the write count and the read count, it is determined whether to read a message entity corresponding to the second control message in the shared memory.
13. The method according to claim 12, characterized in that The determining, according to the write count and the read count, whether to read a message entity corresponding to the second control message in the shared memory comprises: When the first value is greater than or equal to the sum of the fourth value and the number of memory fragments of the notification ring, determine not to read the message entity corresponding to the second control message; or, When the first value is smaller than the sum of the fourth value and the number of memory fragments in the notification ring, it is determined to read a message entity corresponding to the second control message.
14. An inter-process communication device, characterized in that: The device comprises an acquisition unit and a processing unit: The acquisition unit is used to acquire a first value of a write count, where the first value of the write count is used to indicate a position where a publishing end process writes a first memory segment corresponding to a first control message in a notification ring; The processing unit is used to, within a preset time period, if the first atomic operation is executed for a timeout, repair the notification count, wherein the second value of the repaired notification count is greater than the first value, and the first atomic operation includes updating the current value of the notification count to the second value when the current value of the notification count is equal to the first value; The current value of the notification count is used to indicate the positions of the X memory segments corresponding to the X control messages read by the subscriber process in the notification ring, the X control messages correspond to the X memory segments one-to-one, and X is a positive integer.
15. The device according to claim 14, characterized in that The processing unit is specifically used for: Determine the position of the second memory fragment corresponding to the current value of the notification count in the notification ring as the first position; Determine the position of the first memory fragment in the notification ring as a second position; When the first position is not after the second position, the current value of the notification count is updated to the second value.
16. The device according to claim 15, characterized in that The processing unit is specifically used for: updating the current value to a third value, wherein the position of a third memory segment corresponding to the third value in the notification ring is a third position, and in the notification ring, the third position is between the first position and the second position; The third value is updated to the second value.
17. The device according to claim 15 or 16, characterized in that The message header corresponding to the first memory segment includes the number of times the first memory segment is written, where the number of times the first memory segment is written, and the processing unit is further configured to: After the first position is not in the second position and when writing the first control message in the first memory segment fails, the number of write times of the first memory segment is updated to a preset value, where the preset value is used to indicate that the first memory segment is a non-writable or non-readable memory segment.
18. The device according to any one of claims 14 to 17, characterized in that The message header corresponding to the first memory segment includes the number of times the first memory segment is written, where the number of times the first memory segment is written, and the processing unit is further configured to: Verifying the validity of the first memory segment according to the number of write times; If the first memory segment is valid, writing the first control message into the first memory segment; The number of write times is updated.
19. The device according to any one of claims 14 to 18, characterized in that The acquisition unit is also used for: Obtaining a first index number from a first subscriber, where the first index number is used to indicate a message entity held by the first subscriber; Obtaining a second index number from a second subscriber, where the second index number is used to indicate a message entity held by the second subscriber; The processing unit is also used for: A message entity to be released is determined according to the first index number and the second index number.
20. The device according to claim 19, characterized in that The first index number is the first smallest index number among the message entities held by the first subscriber; the second index number is the second smallest index number among the message entities held by the second subscriber, The processing unit is specifically used for: The message entity corresponding to the index number before the smallest index number between the first minimum index number and the second minimum index number is determined as the message entity to be released.
21. The device according to claim 20, characterized in that The processing unit is specifically used for: Obtain the minimum index number; The message entity corresponding to the index number before the smallest index number is determined as the message entity to be released.
22. The device according to claim 20, characterized in that The processing unit is specifically used for: Determine the message entity to be released according to the first value, the first index number, the second index number and the number of caches in the notification ring; Among them, the first index number is a set of index numbers of message entities held by the first subscriber, the second index number is a set of index numbers of message entities held by the second subscriber, and the cache quantity of the notification ring is less than or equal to the number of memory fragments of the notification ring.
23. The device according to claim 22, characterized in that The processing unit is specifically used for: Determine, according to the first value and the number of caches in the notification ring, a first message entity set, where the first message entity set is a message entity corresponding to a control message in the notification ring; According to the first index number and the second index number, determining a second message entity set, where the second message entity set is message entities held by the first subscriber and the second subscriber; Message entities other than the first message entity set and the second message entity set are determined as the message entities to be released.
24. The device according to any one of claims 19 to 23, characterized in that The number of message entities held by the first subscriber is less than or equal to a first threshold, and the number of message entities held by the second subscriber is less than or equal to a second threshold.
25. The device according to any one of claims 14 to 24, characterized in that The acquisition unit is further used to acquire a fourth value of the read count, where the read count is used to indicate a position at which the subscriber process reads the second control message in the notification ring, and the fourth value of the read count is less than or equal to a current value of the notification count; The processing unit is further configured to determine, according to the write count and the read count, whether to read a message entity corresponding to the second control message in the shared memory.
26. The device according to claim 25, characterized in that The processing unit is specifically used for: When the first value is greater than or equal to the sum of the fourth value and the number of memory fragments of the notification ring, determine not to read the message entity corresponding to the second control message; or, When the first value is smaller than the sum of the fourth value and the number of memory fragments in the notification ring, it is determined to read a message entity corresponding to the second control message.
27. An inter-process communication device, characterized in that: include: Memory for storing computer programs; A processor, configured to execute the computer program stored in the memory, so that the apparatus performs the method according to any one of claims 1 to 13.
28. An intelligent driving device, characterized in that: Comprising an apparatus as claimed in any one of claims 14 to 27.
29. The intelligent driving device according to claim 28, characterized in that: The intelligent driving device is a vehicle.
30. A computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a computer, the method according to any one of claims 1 to 13 is implemented.
31. A chip, characterized in that: A circuit is included for performing the method according to any one of claims 1 to 13.
Citation Information
Cited By
Communication middleware-oriented communication method and system based on shared memory
CN120144337A
