CN107704325B - Method and device for transmitting messages between processes - Google Patents

Method and device for transmitting messages between processes Download PDF

Info

Publication number
CN107704325B
CN107704325B CN201610640978.3A CN201610640978A CN107704325B CN 107704325 B CN107704325 B CN 107704325B CN 201610640978 A CN201610640978 A CN 201610640978A CN 107704325 B CN107704325 B CN 107704325B
Authority
CN
China
Prior art keywords
memory block
written
read
memory
currently
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201610640978.3A
Other languages
Chinese (zh)
Other versions
CN107704325A (en
Inventor
冯靖超
夏黎明
王全
曲宁
王海峰
陈卓
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Beijing Baidu Netcom Science and Technology Co Ltd
Original Assignee
Beijing Baidu Netcom Science and Technology Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Beijing Baidu Netcom Science and Technology Co Ltd filed Critical Beijing Baidu Netcom Science and Technology Co Ltd
Priority to CN201610640978.3A priority Critical patent/CN107704325B/en
Publication of CN107704325A publication Critical patent/CN107704325A/en
Application granted granted Critical
Publication of CN107704325B publication Critical patent/CN107704325B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

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

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Data Exchanges In Wide-Area Networks (AREA)
  • Multi Processors (AREA)

Abstract

Methods and apparatus for inter-process transfer of messages are disclosed. One embodiment of the method comprises: dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted; determining a memory block which is not written into currently in the plurality of memory blocks as a memory block to be written into; monitoring whether the memory block to be written is read currently; responding to the current read of the memory block to be written, and executing locking operation on the memory block to be written; and in response to the end of reading the memory block to be written, executing an unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written. The implementation mode effectively controls the speed of writing the message into the shared memory so as to avoid the condition that the speed of sending the message by the sending process is too high, so that the receiving process cannot receive the message sent by the sending process in real time, the synchronism of message processing is ensured, and the reliability of message transmission is improved.

Description

Method and device for transmitting messages between processes
Technical Field
The present application relates to the field of computer technologies, and in particular, to a method and an apparatus for interprocess message transmission.
Background
Data interaction among processes needs to be carried out in a mode of a message queue, a shared memory, a pipeline and the like, when the message queue or the pipeline is used, a process sending messages writes the messages into the message queue according to the sending frequency of the process, and a process receiving the messages obtains the messages from the message queue. When the processing frequency of the process receiving the message is less than the sending frequency of the process sending the message and the message queue is full, the process sending the message continues to write the message into the message queue, which may cause overflow of the message queue, that is, a part of the message which is not received by the process receiving the message may be deleted, which may cause loss of the message.
The commonly used method of inter-process communication based on shared memory cannot ensure the synchronism between the process of sending the message and the process of receiving the message, so that the reliability of message transmission is poor.
Disclosure of Invention
The present application is directed to a method and apparatus for inter-process message transmission to solve the technical problems mentioned in the background section above.
In a first aspect, the present application provides a method for inter-process message transmission, the method comprising: dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted; determining a memory block which is not written into currently in the plurality of memory blocks as a memory block to be written into; monitoring whether the memory block to be written is read currently; responding to the current read of the memory block to be written, and executing locking operation on the memory block to be written; and in response to the end of reading the memory block to be written, executing an unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written.
In some embodiments, the determining a memory block that is not currently written to as a memory block to be written to includes: setting continuous serial numbers for the plurality of memory blocks; gradually increasing the write quantity value prestored in the shared memory; determining a sequence number of a first memory block corresponding to the increased write quantity value, where the write quantity value corresponds to the plurality of memory blocks one to one; monitoring whether the first memory block is written in currently; and in response to that the first memory block is not currently written to, taking the first memory block as the memory block to be written to.
In some embodiments, the determining the sequence number of the first memory block corresponding to the incremented write quantity value includes: determining the number of the plurality of memory blocks; the increased writing quantity value is complemented with the quantity of the plurality of memory blocks; and taking the remainder value as the serial number of the first memory block.
In some embodiments, the method further comprises: in response to that the first memory block is currently written, querying whether other memory blocks are currently written according to the sequence of the sequence numbers of the plurality of memory blocks; and in response to the query that other memory blocks are not written currently, taking the memory blocks which are not written currently as the memory blocks to be written.
In some embodiments, the method further comprises: and in response to the query that other memory blocks are written currently, circularly querying according to the serial numbers of the plurality of memory blocks until one memory block is queried to be written currently, and taking the memory block which is not written currently as the memory block to be written.
In some embodiments, the monitoring whether the memory block to be written is currently read includes: monitoring whether a reading quantity value prestored in the memory block to be written is equal to a preset value or not; when the reading quantity value is equal to the preset value, determining that the memory block to be written is not read currently; and when the reading quantity value is not equal to the preset value, determining that the memory block to be written is currently read.
In some embodiments, the method further comprises: when the message to be transmitted is written into the memory block to be written, setting a writing identifier prestored in the memory block to be written as a true value, wherein the true value corresponds to the current written-in memory block to be written; and in response to the completion of writing the to-be-transmitted message into the to-be-written memory block, setting the write identifier to be a false value, where the false value corresponds to the to-be-written memory block that is not currently written into.
In a second aspect, the present application provides a method for inter-process message transmission, the method comprising: mapping the address of the shared memory of the operating system into the address space of the operating system; determining a memory block which is newly written with the message as a memory block to be read from a plurality of memory blocks of the shared memory; monitoring whether the memory block to be read is written currently; responding to the current writing of the memory block to be read, and entering a process waiting state; and reading the information stored in the memory block to be read in response to the end of writing the memory block to be read.
In some embodiments, the determining the memory block in which the message is written latest as the memory block to be read includes: reading the serial number of each memory block in the shared memory and the prestored written quantity value; the written quantity value is complemented with the quantity of the plurality of memory blocks; and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
In some embodiments, the monitoring whether the memory block to be read is currently written includes: monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or monitoring whether a value of a write identifier prestored in the memory block to be read is a false value, where the false value corresponds to the memory block to be read which is not currently written in.
In some embodiments, the method further comprises: and in response to that the memory block to be read is not written currently, reading the message stored in the memory block to be read.
In some embodiments, the method further comprises: in the process of reading the information stored in the memory block to be read, the reading quantity value prestored in the memory block to be read is increased progressively; and in response to the completion of reading the message stored in the memory block to be read, decrementing the read quantity value prestored in the memory block to be read.
In some embodiments, the method further comprises: in response to the completion of reading the information stored in the memory block to be read, taking other memory blocks as new memory blocks to be read; comparing the residual value with the serial number of the new memory block to be read; reading the message stored in the new memory block to be read in response to the serial number of the new memory block to be read being smaller than the remainder value; and entering the process waiting state in response to the fact that the sequence number of the new memory block to be read is equal to the remainder value.
In a third aspect, the present application provides an apparatus for inter-process message transmission, the apparatus comprising: the device comprises a dividing unit, a sending unit and a receiving unit, wherein the dividing unit is used for dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted; a first determining unit, configured to determine, among the multiple memory blocks, a memory block that is not currently written to as a memory block to be written to; a first monitoring unit, configured to monitor whether the memory block to be written is currently read; a locking unit, configured to perform a locking operation on the memory block to be written in response to that the memory block to be written is currently read; and the writing unit is used for responding to the end of reading the memory block to be written, executing unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written.
In some embodiments, the first determination unit comprises: the setting module is used for setting continuous serial numbers for the plurality of memory blocks; the assignment module is used for increasing the write quantity value prestored in the shared memory; a first determining module, configured to determine a sequence number of a first memory block corresponding to the incremented write quantity value, where the write quantity value corresponds to the multiple memory blocks one to one; a monitoring module, configured to monitor whether the first memory block is currently written in; a second determining module, configured to, in response to that the first memory block is not currently written to, use the first memory block as the memory block to be written to.
In some embodiments, the first determination module is further to: determining the number of the plurality of memory blocks; the increased writing quantity value is complemented with the quantity of the plurality of memory blocks; and taking the remainder value as the serial number of the first memory block.
In some embodiments, the first determining unit further comprises: the query module is configured to, in response to that the first memory block is currently written to, query whether other memory blocks are currently written to according to the sequence of the sequence numbers of the plurality of memory blocks; a third determining module, configured to, in response to the query that another memory block is not currently written to, use the memory block that is not currently written to as the memory block to be written to.
In some embodiments, the first determining unit further comprises: a fourth determining module, configured to, in response to that the other memory blocks are all currently written to, perform cyclic query according to the serial numbers of the multiple memory blocks until one memory block is queried that is not currently written to, and use the memory block that is not currently written to as the memory block to be written to.
In some embodiments, the first monitoring unit comprises: the monitoring module is used for monitoring whether a reading quantity value prestored in the memory block to be written is equal to a preset value or not; a fifth determining module, configured to determine that the memory block to be written is not currently read when the read quantity value is equal to the preset value; a sixth determining module, configured to determine that the memory block to be written is currently read when the read quantity value is not equal to the preset value.
In some embodiments, the apparatus further comprises: a first identification unit, configured to set, when the to-be-transmitted message is written into the to-be-written memory block, a true value of a write identification prestored in the to-be-written memory block, where the true value corresponds to a current write of the to-be-written memory block; and a second identifier unit, configured to set, in response to completion of writing the to-be-transmitted message into the to-be-written memory block, the write identifier to a false value, where the false value corresponds to the to-be-written memory block and is not currently written into the to-be-written memory block.
In a fourth aspect, the present application provides an apparatus for inter-process transfer of messages, the apparatus comprising: the memory mapping unit is used for mapping the address of the shared memory of the operating system into the address space of the memory mapping unit; a third determining unit, configured to determine, from the multiple memory blocks of the shared memory, a memory block in which a message is written most recently as a memory block to be read; a second monitoring unit, configured to monitor whether the memory block to be read is currently written in; the first waiting unit is used for responding to the current writing of the memory block to be read and entering a process waiting state; a first reading unit, configured to read a message stored in the memory block to be read in response to that the memory block to be read is written to the end.
In some embodiments, the third determining unit is further configured to: reading the serial number of each memory block in the shared memory and the prestored written quantity value; the written quantity value is complemented with the quantity of the plurality of memory blocks; and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
In some embodiments, the second monitoring unit is further configured to: monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or monitoring whether a value of a write identifier prestored in the memory block to be read is a false value, where the false value corresponds to the memory block to be read which is not currently written in.
In some embodiments, the apparatus further comprises: a second reading unit, configured to read a message stored in the memory block to be read, in response to that the memory block to be read is not currently written in.
In some embodiments, the apparatus further comprises: a first assignment unit, configured to increment a read quantity value prestored in the memory block to be read in the process of reading the message stored in the memory block to be read; and the second assignment unit is used for responding to the completion of reading the information stored in the memory block to be read and decrementing the reading quantity value prestored in the memory block to be read.
In some embodiments, the apparatus further comprises: a comparing unit, configured to, in response to completion of reading the message stored in the memory block to be read, use another memory block as a new memory block to be read; comparing the residual value with the serial number of the new memory block to be read; a third reading unit, configured to read a message stored in the new memory block to be read in response to that the serial number of the new memory block to be read is smaller than the remainder; and the second waiting unit is used for entering the process waiting state in response to the fact that the sequence number of the new memory block to be read is equal to the remainder value.
According to the method and the device for transmitting the messages among the processes, firstly, the shared memory is divided into a plurality of memory blocks according to the size of the messages to be transmitted, then one memory block which is not written into currently is selected from the divided memory blocks to serve as the memory block to be written into, and the messages to be transmitted are written into the memory block to be written into when the memory block to be written into is not read. When reading the message, firstly mapping the address of the shared memory to the address space of the shared memory, taking the memory block which is newly written with the message in the plurality of memory blocks of the shared memory as the memory block to be read, and reading the message stored in the memory block to be read when the memory block to be read is not written with the message. Therefore, the speed of writing the message into the shared memory by the sending process is effectively controlled, so that the condition that the speed of sending the message by the sending process is too high, the message sent by the sending process cannot be received by the receiving process in real time, the message sent by the process of sending the message can be read by the process of reading the message in time, the message processing synchronism is ensured, and the message transmission reliability is improved.
Drawings
Other features, objects and advantages of the present application will become more apparent upon reading of the following detailed description of non-limiting embodiments thereof, made with reference to the accompanying drawings in which:
FIG. 1 is a flow diagram for one embodiment of a method for inter-process transfer of messages according to the present application;
FIG. 2 is a flow diagram of yet another embodiment of a method for inter-process transfer of messages according to the present application;
FIG. 3 is a flow diagram for one embodiment of a method for inter-process transfer of messages according to the present application;
FIG. 4 is a flow diagram of yet another embodiment of a method for inter-process transfer of messages according to the present application;
FIG. 5 is a schematic diagram of an application scenario of a method for inter-process transfer of messages according to the present application;
FIG. 6 is a block diagram illustrating one embodiment of an apparatus for inter-process transfer of messages according to the present application;
FIG. 7 is a block diagram illustrating one embodiment of an apparatus for inter-process transfer of messages according to the present application;
FIG. 8 is a block diagram of a computer system suitable for implementing an apparatus for interprocess message transmission of an embodiment of the present application.
Detailed Description
The present application will be described in further detail with reference to the following drawings and examples. It is to be understood that the specific embodiments described herein are merely illustrative of the relevant invention and not restrictive of the invention. It should be noted that, for convenience of description, only the portions related to the related invention are shown in the drawings.
It should be noted that the embodiments and features of the embodiments in the present application may be combined with each other without conflict. The present application will be described in detail below with reference to the embodiments with reference to the attached drawings.
FIG. 1 illustrates a flow 100 of one embodiment of a method for inter-process transfer of messages according to the present application. The method for transmitting messages among processes of the embodiment comprises the following steps:
step 101, according to the size of the message to be transmitted, dividing the shared memory allocated by the operating system into a plurality of memory blocks.
In this embodiment, a process of sending a message to be transmitted is referred to as a sending process, and a process of reading a transmitted message is referred to as a receiving process. The sending process sends a message to be transmitted to the receiving process, where the message to be transmitted may be a message that can be transmitted among various processes, and may be road condition information of an emulator of an autonomous vehicle during emulation operation, for example. The Operating System may be various Operating systems, such as a Linux System, a Robot Operating System (ROS), and the like. Shared memory refers to memory that can be accessed by different processes in a multi-process computer system. It is understood that the shared memory is obtained by applying a sending process to an operating system.
After obtaining the shared memory allocated by the operating system, the sending process may divide the shared memory into a plurality of memory blocks according to the size of the message to be transmitted. For example, the size of the message to be transmitted is 8M, the size of the divided memory block may be the same as the size of the message to be transmitted, or may be N times the size of the message to be transmitted, where N is a preset constant (e.g., N is 2.5). The size of the memory block is set to be N times of the message to be transmitted, so that the space size of one memory block can store the transmitted message as much as possible in the next message. For example, in the ROS, each process can be used as a node, and when messages are transmitted among nodes, the messages can be organized to be stored in one or more topics. The size of the message within each topic is comparable. Therefore, in this embodiment, the size of the divided memory block may be slightly larger than the size of the message to be transmitted, so that the memory block can store the message to be transmitted next by the sending process and the receiving process.
Step 102, determining a memory block which is not written in currently among the plurality of memory blocks as a memory block to be written in.
In this embodiment, whether a memory block is currently written may be determined by setting a write flag in each memory block. For example, when the write flag of the memory block is true, the memory block is considered to be currently written, i.e., there are other sending processes writing messages into the memory block. It is to be understood that, in this embodiment, the sending process may also determine whether the memory block is currently written, for example, by setting a bitmap in the shared memory, where the bitmap indicates which memory blocks in the shared memory are currently being written.
In this embodiment, when the sending process selects a memory block to write a message, a memory block that is not currently written to is selected, so as to avoid a phenomenon that a plurality of processes write a message into the same memory block at the same time, which causes message confusion.
Step 103, monitoring whether the memory block to be written is currently read.
In this embodiment, after determining that a memory block that is not currently written in is used as a memory block to be written in, the sending process monitors whether the memory block to be written in is currently read or not through various manners, for example, a flag amount is set in the memory block and is recorded as a read quantity value to mark whether the memory block to be written in is currently read or not; it is also possible to record the number of processes currently reading the message by setting a control bit in the transmitted message.
In some optional implementations of this embodiment, the step 103 may be implemented by the following steps not shown in fig. 1:
monitoring whether a reading quantity value prestored in a memory block to be written is equal to a preset value or not; when the reading quantity value is equal to a preset value, determining that the memory block to be written is not read currently; and when the reading quantity value is not equal to the preset value, determining that the memory block to be written is read currently.
In this embodiment, by pre-storing a read quantity value in each memory block and setting an initial value (for example, setting the initial value to 0), when reading a message stored in the memory block by one more receiving process, the read quantity value is incremented by one; every time at least one receiving process reads the message stored in the memory block, the read quantity value is decremented by one. Thus, when the read quantity value is equal to the initial value, the memory block is considered to be not currently read.
And 104, responding to the current read of the memory block to be written, and executing locking operation on the memory block to be written.
In this embodiment, if the memory block to be written is currently read by the receiving process, the sending process may lock the memory block to be written, so as to avoid that other sending processes also want to write a message into the memory block to be written. When the sending process locks the memory block to be written in, the memory block to be written in can be locked by executing a locking operation on the memory block to be written in or applying for a distributed lock from an operating system.
And 105, in response to the end of reading the memory block to be written, executing an unlocking operation on the memory block to be written, and writing a message to be transmitted into the memory block to be written.
When the memory block to be written is no longer read, the sending process may perform an unlocking operation on the memory block to be written to release the lock on the memory block to be written, and write a message to be transmitted into the memory block to be written. When writing the message into the memory block to be written, the message to be transmitted may be written from the start position of the memory block to be written.
According to the method for transmitting messages among processes provided by the embodiments of the present application, firstly, according to the size of the message to be transmitted, a shared memory is divided into a plurality of memory blocks, then one memory block which is not currently written in is selected from the plurality of divided memory blocks as a memory block to be written in, when the memory block to be written in is not read, the message to be transmitted is written in the memory block to be written in, so that the speed of writing the message into the shared memory by a sending process is effectively controlled, the speed of sending the message by the sending process is prevented from being too fast, the message sent by the sending process cannot be received in real time by a receiving process, the synchronization of message transmission is ensured, and the reliability of message transmission is improved.
FIG. 2 illustrates a flow diagram 200 of yet another embodiment of a method for inter-process transfer of messages according to the present application. As shown in fig. 2, the method for transmitting messages between processes of the present embodiment includes the following steps:
step 201, according to the size of the message to be transmitted, dividing the shared memory allocated by the operating system into a plurality of memory blocks.
In step 202, consecutive serial numbers are set for the plurality of memory blocks.
In this embodiment, the sending process may set consecutive sequence numbers to the plurality of divided memory blocks, for example, if the sending process divides N memory blocks in total, the consecutive sequence numbers may start from 1 to end from N.
In step 203, the write quantity value pre-stored in the shared memory is incremented.
In this embodiment, a write quantity value is set in the shared memory as a scalar value to indicate the location of the memory block in which the message is newly written, and the value is used as a cut-in for selecting the memory block in the sending process. The written quantity value can be accumulated from 0, and can also be accumulated from other preset values. The written quantity value has a one-to-one correspondence relationship with the serial numbers in the memory blocks, so that the sending process can find the corresponding memory block according to the written quantity value. Whenever a sending process needs to write a message into a memory block, the write count value is incremented to point to a new memory block.
Step 204, determining the sequence number of the first memory block corresponding to the incremented write quantity value.
Since the written-in quantity value is in one-to-one correspondence with the serial number of each memory block, when the sending process accesses the shared memory, the written-in quantity value is incremented to determine a new memory block. In this embodiment, the memory block corresponding to the incremented write quantity value is referred to as a first memory block.
In some optional implementations of this embodiment, the step 204 may be implemented by the following steps not shown in fig. 2:
determining the number of the plurality of memory blocks; the increased writing quantity value is complemented with the quantity of the plurality of memory blocks; and taking the remainder value as the serial number of the first memory block.
Since the number of memory blocks is limited and the number of write magnitudes is increasing, it is possible that multiple write magnitudes correspond to the same memory block. In this embodiment, the written-in quantity value may be used to complement the quantity of the memory blocks, and the complemented value is used as the serial number of the memory blocks, so that the written-in quantity value and the memory blocks are in one-to-one correspondence. For example, the sending process divides 3 memory blocks, the incremented write number value is 7, the remainder of dividing 7 by 3 is 1, and the serial number of the memory block corresponding to the write number value 7 is 1.
In step 205, it is determined whether the first memory block is currently written.
In this embodiment, a write identifier may be pre-stored in each memory block as an identifier to identify whether the memory block is currently written in. For example, when the write flag is set to be true, the flag memory block is currently written in; when the write flag is false, the memory block is not currently written. When the first memory block is currently written, execute step 206'; when the first memory block is not currently written to, step 206 is executed.
In step 206, the first memory block is used as the memory block to be written into.
And if the memory block is not written in currently, preparing to write the message to be transmitted into the memory block, and taking the memory block as the memory block to be written in.
Step 207, determine whether the memory block to be written is currently unread.
In this embodiment, when the memory block is currently read, the sending process does not write a message into the memory block, so as to avoid confusion of the message. The sending process may determine whether the memory block to be written is currently read by reading the read quantity value stored in the memory block.
In some optional implementation manners of this embodiment, the step 207 may be specifically implemented by the following steps not shown in fig. 2:
monitoring whether a reading quantity value prestored in a memory block to be written is equal to a preset value or not; when the reading quantity value is equal to a preset value, determining that the memory block to be written is not read currently; and when the reading quantity value is not equal to the preset value, determining that the memory block to be written is read currently.
For example, the initial value of the read quantity value in each memory block may be set to 0, and when one more receiving process reads the message stored in the memory block, the read quantity value is incremented by 1, so that the sending process knows that there are "read quantity values" of the messages stored in the memory block by the receiving processes when knowing the read quantity value. Each time a reading process finishes reading the message, the reading number value is reduced by 1, and then when all receiving processes finish reading the message, the reading number value becomes an initial value of 0, which indicates that the memory block is not currently read.
When the memory block to be written is not currently read, step 208 is executed, otherwise step 219 is executed.
And step 208, setting the current written memory block to be written.
In this embodiment, when the memory block to be written is not read, it indicates that the sending process can write the message to be transmitted into the memory block. At this time, the memory block to be written is set to be written currently, so that other sending processes can definitely know the current state of the memory block. The sending process can indicate the current state of the memory block to be written by setting the write identifier prestored in the memory block. For example, setting the write flag to true indicates that the memory block to be written is currently written.
Step 209, writing the message to be transmitted into the memory block to be written.
When a memory block is not currently written to or read from, the sending process may write a message to the memory block.
Step 210, determining whether the writing to the memory block to be written is completed.
Step 211 is executed when the writing is completed, otherwise step 209 is continued.
In step 211, it is set that the memory block to be written is not written currently.
After the sending process completes writing the message, the write flag of the memory block to be written may be set to false, to indicate that the memory block is not currently written.
When it is determined in step 205 that the first memory block is currently written to, step 206' is performed.
In step 206', it is queried whether other memory blocks are currently written according to the serial numbers of the memory blocks.
When the first memory block is currently being written in, sequentially inquiring whether other memory blocks are currently written in according to the sequence numbers of the memory blocks. For example, if the shared memory is divided into 5 memory blocks, and the serial number of the first memory block is 1, the other memory blocks are queried in the order of 0, 1, 2, 3, and 4.
Step 207', determine whether other memory blocks are not currently written.
When the other memory blocks are not currently written, the step 208 'is executed, otherwise, the step 206' is executed again, and the query is continued.
In this embodiment, it may still be determined whether other memory blocks are currently written through the write flag set in the memory block.
In step 208', the memory block that is not written to currently is used as the memory block to be written to.
And the sending process inquires other memory blocks according to the sequence of 0, 1, 2, 3 and 4, and if a certain memory block is not written currently in the inquiring process, the memory block is taken as a memory block to be written, and a message to be transmitted is prepared to be written into the memory block.
After performing the completion step 208', step 207 is performed. When it is determined in step 207 that the memory block to be written is currently read, step 209' is executed.
Step 209' performs a locking operation on the memory block to be written.
In this embodiment, if the memory block to be written is currently read by the receiving process, the sending process may lock the memory block to be written, so as to avoid that other sending processes also want to write a message into the memory block to be written. When the sending process locks the memory block to be written in, the memory block to be written in can be locked by executing a locking operation on the memory block to be written in or applying for a distributed lock from an operating system.
Step 210', enter a process wait state.
And after the sending process locks the memory block to be written, entering a waiting state until the memory block to be written is written.
In step 211', it is determined whether the memory block to be written is read completely.
When the memory block to be written is read, go to step 212'; otherwise, the step 210' is executed, and the process continues to wait.
In step 212', an unlocking operation is performed on the memory block to be written.
After the memory block to be written is read, the memory block to be written is unlocked, and step 208 is executed.
According to the method for transmitting messages among processes provided by the embodiment of the application, by setting the three identification quantities, namely the writing quantity value, the reading quantity value and the writing identification, a process sending the messages can quickly determine that one current unwritten and unwritten memory block in the shared memory is used as a memory block to be written in, and write the messages into the memory block, so that the message writing efficiency is improved; meanwhile, before the message is written, the process of reading the message is enabled to preferentially read the completion message, the reliability of message transmission is further ensured, and the phenomenon of message transmission confusion is avoided.
FIG. 3 illustrates a flow 300 of one embodiment of a method for inter-process transfer of messages according to the present application. As shown in fig. 3, the method for transmitting messages between processes of the present embodiment includes the following steps:
step 301, mapping the address of the shared memory of the operating system into its own address space.
When a process reading a message reads the message from the shared memory, firstly, the address of the shared memory of the operating system is mapped into the address space of the process, so that the process receiving the message accesses the shared memory as the process accessing the memory of the process. The Operating System may be various Operating systems, and may be a Linux System, a Robot Operating System (ROS), or the like.
In this embodiment, a process of reading a message is referred to as a receiving process, and a process of transmitting a message is referred to as a transmitting process. The receiving process may be various processes capable of receiving messages, for example, a process in which each simulation module in a simulator of an autonomous vehicle is located.
Step 302, determine a memory block, into which a message is written most recently, from among the plurality of memory blocks of the shared memory as a memory block to be read.
In order to ensure that the receiving process can read the latest message, when the receiving process accesses the shared memory, the receiving process first accesses the memory block in which the message is written latest and takes the memory block as the memory block to be read.
Step 303, monitoring whether the memory block to be read is currently written in.
After determining the memory block to be read, first determining whether the memory block is currently written, if the sending process is writing a message into the memory block, the receiving process cannot read the message in the memory block.
In this embodiment, the reading process may determine whether the memory block to be read is currently written in by reading the write identifier in the memory block. Of course, the reading process may also determine the above problem in other ways, such as by way of a bitmap.
Step 304, in response to the current writing of the memory block to be read, entering a process waiting state.
If the memory block to be read is currently written, the receiving process needs to read the memory block after the memory block to be read is written, and therefore the process waiting state needs to be entered.
Step 305, in response to the end of writing the to-be-read memory block, reading the message stored in the to-be-read memory block.
And when the memory block to be read is written into, indicating that the sending process writes all the messages to be transmitted into the memory block to be read, and at this time, the receiving process can read the messages stored in the memory block to be read.
It is understood that the receiving process may also determine whether the memory block to be read is written to the end according to the write identifier in the memory block.
In the method for transmitting messages between processes provided in the foregoing embodiments of the present application, first, an address of a shared memory is mapped into an address space of the shared memory, a memory block, in which a message is written latest, of a plurality of memory blocks of the shared memory is used as a memory block to be read, and when the memory block to be read is not written, a message stored in the memory block to be read is read, so that the message written latest can be read, and synchronization of message processing is ensured.
FIG. 4 illustrates a flow diagram 400 of yet another embodiment of a method for inter-process transfer of messages in accordance with the present application. As shown in fig. 4, the method for transmitting messages between processes of the present embodiment includes the following steps:
step 401, mapping the address of the shared memory of the operating system into its own address space.
Step 402, determining a memory block, in which a message is written most recently, from a plurality of memory blocks of the shared memory as a memory block to be read.
In some optional implementations of this embodiment, the step 402 may be implemented by the following steps not shown in fig. 4:
reading the serial number of each memory block in the shared memory and the prestored written quantity value; the written quantity value and the quantity of the plurality of memory blocks are complemented; and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
When the memory block of the latest write message is determined, the memory block can be determined through the one-to-one correspondence relationship between the write quantity value and the memory block sequence number in the shared memory. That is, the written quantity value is divided by the number of the plurality of memory blocks, and the memory block with the serial number equal to the remainder is used as the memory block of the latest written message.
In step 403, it is determined whether the memory block to be read is currently written.
The receiving process may determine whether the memory block to be read is currently written in various manners, for example, in a manner of writing an identifier, a manner of a bitmap, and the like.
In some optional implementations of this embodiment, the step 403 may be implemented by the following steps not shown in fig. 4:
monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or
And monitoring whether the value of the write identifier prestored in the memory block to be read is a false value, wherein the false value corresponds to the current situation that the memory block to be read is not written in.
The receiving process may determine whether it is currently written by monitoring the value of the write flag in the memory block to be read. When the fact that the writing identification is monitored to be a true value is monitored, the current writing of the memory block to be read is determined; and when the write identifier is monitored to be a false value, determining that the memory block to be read is not written currently.
When the memory block to be read is currently written, execute step 404'; otherwise, step 404 is performed.
In step 404, the read quantity value of the memory block to be read is incremented.
When reading the message, the reading quantity value of the memory block to be read is increased progressively, so that the sending process can clearly know the reading state of the memory block when sending the message.
Step 405, the message stored in the memory block to be read is read.
When the memory block to be read is not written, the receiving process can read the message stored in the memory block to be read.
Step 406, determine whether the memory block to be read is completely read.
If so, go to step 407, otherwise, go back to step 405.
Step 407, the read quantity value of the memory block to be read is decremented.
After the reading is completed, the reading quantity value is decreased, so that the sending process can clearly know the reading state of the memory block when sending the message.
When it is determined in step 403 that the memory block to be read is currently written, step 404' is executed.
Step 404', enter a process wait state.
If the memory block to be read is written currently, the receiving process needs to read the stored message after the sending process finishes writing.
Step 405', it is determined whether the memory block to be read is completely written.
If so, go to step 404, otherwise, go back to step 404'.
In some optional implementations of this embodiment, the method further includes the following steps not shown in fig. 4:
in response to the completion of reading the information stored in the memory block to be read, taking other memory blocks as new memory blocks to be read; and comparing the residual value with the serial number of the new memory block to be read.
And when the serial number of the new memory block to be read is smaller than the remainder value, reading the message stored in the new memory block to be read.
And entering a process waiting state when the sequence number of the new memory block to be read is equal to the remainder value.
After the receiving process reads the information stored in the memory block to be read, the receiving process may read the information stored in the other memory blocks, and use the other memory blocks as new memory blocks to be read. Before reading, the serial number of the new memory block to be read needs to be compared with a remainder obtained by dividing the written quantity value by the number of the memory blocks, if the serial number and the written quantity value are equal, the sending process writes a message into the memory block, and the receiving process enters a process waiting state; if the sequence number of the new memory block to be read is smaller than the remainder, it indicates that the writing of the memory block by the sending process is completed, and the receiving process can read the memory block.
In the method for transmitting messages between processes provided by the embodiments of the present application, through the three flag quantities, i.e., the write quantity value, the write identifier, and the read quantity value, the memory block in which the message is written most recently can be determined quickly, and the state of the memory block can be determined, and meanwhile, by changing the read quantity value, the sending process can also know the read state of the memory block definitely, so that the efficiency of message processing is improved; when the receiving process reads the message in the memory block to be read, the process waiting state is introduced, so that the receiving process can always read the latest written message, and the message transmission synchronism is ensured; therefore, the read message can be processed in real time, and the reliability of message transmission is improved.
With continued reference to fig. 5, fig. 5 is a schematic diagram of an application scenario of the method for inter-process transmission of a message according to the present embodiment. In the application scenario of fig. 5, the sending process 501 selects one currently unwritten memory block from 7 memory blocks (with sequence numbers of 0 and 1 … … 6, respectively) of the shared memory 502 as a memory block to be written into, starts to query the memory block with sequence number 0, finds that the writing identifiers of the memory block 0 and the memory block 1 are both true (there is a flag in the figure), and writes identifiers of the memory blocks with sequence number 2 are false, and then uses the memory block 2 as the memory block to be written into. At this time, the read quantity value in the memory block 2 is monitored to be 2, and the memory block 2 is locked. When the read count value of locked memory block 2 (shown as 503) becomes 0, sending process 501 unlocks it and writes a message to it, at which time its write flag is true (shown as 504). The receiving process 506 accesses the memory block 2 at this time, finds that its write flag is true, enters a wait state, and reads the message stored in the memory block 2 when the write flag is false (as shown in 505).
With further reference to fig. 6, as an implementation of the method shown in the above-mentioned figures, the present application provides an embodiment of an apparatus for inter-process message transmission, where the embodiment of the apparatus corresponds to the embodiment of the method shown in fig. 1, and the apparatus may be applied to various electronic devices in particular. As shown in fig. 6, the apparatus 600 for transmitting a message between processes of the present embodiment includes: a dividing unit 601, a first determining unit 602, a first monitoring unit 603, a locking unit 604, and a writing unit 605.
The dividing unit 601 is configured to divide the shared memory allocated by the operating system into a plurality of memory blocks according to the size of the message to be transmitted.
A first determining unit 602, configured to determine, among the multiple memory blocks divided by the dividing unit 601, a memory block that is not currently written in, as a memory block to be written in.
In some optional implementations of this embodiment, the first determining unit 602 may further include a setting module, an assigning module, a first determining module, a monitoring module, and a second determining module, which are not shown in fig. 6.
The setting module is configured to set consecutive serial numbers for the plurality of memory blocks divided by the dividing unit 601.
And the assignment module is used for increasing the write quantity value prestored in the shared memory.
And the first determining module is used for determining the serial number of the first memory block corresponding to the increased write quantity value. The write quantity value corresponds to the plurality of memory blocks one to one.
And a monitoring module, configured to monitor whether the first memory block determined by the first determining module is currently written in.
And the second determining module is configured to, in response to the monitoring module monitoring that the first memory block is not currently written in, use the first memory block as a memory block to be written in.
In some optional implementations of this embodiment, the first determining module may be further configured to:
determining the number of the plurality of memory blocks; the increased writing quantity value is complemented with the quantity of the plurality of memory blocks; and taking the remainder value as the serial number of the first memory block.
In some optional implementation manners of this embodiment, the first determining unit 602 may further include a query module, a third determining module, and a fourth determining module, which are not shown in fig. 6.
The query module is configured to, in response to the monitoring module monitoring that the first memory block is currently written to, query whether other memory blocks are currently written to according to the sequence of the sequence numbers of the plurality of memory blocks.
And a third determining module, configured to, in response to the query module querying that another memory block is not currently written in, use the memory block that is not currently written in as the memory block to be written in.
A fourth determining module, configured to, in response to the query module querying that all the other memory blocks are currently written in, perform cyclic query according to the serial numbers of the multiple memory blocks until it is queried that one memory block is not currently written in, and use the memory block that is not currently written in as the memory block to be written in.
The first monitoring unit 603 is configured to monitor whether the memory block to be written determined by the first determining unit 602 is currently read.
In some optional implementations of this embodiment, the first monitoring unit 603 may further include a monitoring module, a fifth determining module, and a sixth determining module, which are not shown in fig. 6.
The monitoring module is used for monitoring whether a reading quantity value prestored in the memory block to be written is equal to a preset value or not.
And the fifth determining module is configured to determine that the memory block to be written in is not currently read when the monitoring module monitors that the read quantity value is equal to the preset value.
And the sixth determining module is configured to determine that the memory block to be written into is currently read when the monitoring module monitors that the read quantity value is not equal to the preset value.
The locking unit 604 is configured to, in response to the first monitoring unit 603 monitoring that the memory block to be written is currently read, perform a locking operation on the memory block to be written.
The writing unit 605 is configured to, in response to that the memory block to be written is read, execute an unlocking operation on the memory block to be written, and write the message to be transmitted into the memory block to be written.
In some optional implementations of this embodiment, the apparatus 600 for inter-process message transmission may further include a first identification unit and a second identification unit, which are not shown in fig. 6.
The first identification unit is configured to set a true value to a write identifier pre-stored in the memory block to be written when the message to be transmitted is written in the memory block to be written, where the true value corresponds to the current state in which the memory block to be written is written.
And the second identification unit is used for setting the write-in identification as a false value in response to the completion of writing the to-be-transmitted message into the to-be-written memory block, wherein the false value corresponds to the current state that the to-be-written memory block is not written in.
The device for transmitting messages among processes provided by the above embodiments of the present application effectively controls the speed of writing messages into the shared memory by the sending process, so as to avoid that the speed of sending messages by the sending process is too fast, so that the receiving process cannot receive messages sent by the sending process in real time, and the synchronization of message transmission is ensured, thereby improving the reliability of message transmission.
FIG. 7 is a block diagram illustrating one embodiment of an apparatus for inter-process transfer of messages according to the present application. As shown in fig. 7, the apparatus 700 for transmitting a message between processes of the present embodiment includes: a memory mapping unit 701, a third determining unit 702, a second monitoring unit 703, a first waiting unit 704, and a first reading unit 705.
The memory mapping unit 701 is configured to map an address of the shared memory of the operating system into an address space of the memory mapping unit.
A third determining unit 702 is configured to determine, from the multiple memory blocks of the shared memory, a memory block in which the message is written most recently as a memory block to be read.
In some optional implementations of this embodiment, the third determining unit 702 may be further configured to:
reading the serial number of each memory block in the shared memory and the prestored written quantity value; the written quantity value and the quantity of the plurality of memory blocks are complemented; and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
The second monitoring unit 703 is configured to monitor whether the memory block to be read is currently written in.
In some optional implementation manners of this embodiment, the second monitoring unit 703 may be further configured to:
monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or monitoring whether the value of the write identifier prestored in the memory block to be read is a false value, wherein the false value corresponds to the memory block to be read which is not written currently.
The first waiting unit 704 is configured to enter a process waiting state in response to that the memory block to be read is currently written.
The first reading unit 705 is configured to, in response to that the memory block to be read is written to the end, read a message stored in the memory block to be read.
In some optional implementations of this embodiment, the apparatus 700 for inter-process message transmission may further include a second reading unit, not shown in fig. 7, configured to read the message stored in the memory block to be read, in response to that the memory block to be read is not currently written to.
In some optional implementations of this embodiment, the apparatus 700 for inter-process transmission of messages may further include a first assignment unit and a second assignment unit, which are not shown in fig. 7.
And the first assignment unit is used for increasing the reading quantity value prestored in the memory block to be read in a process of reading the message stored in the memory block to be read.
And the second assignment unit is used for responding to the completion of reading the information stored in the memory block to be read and decrementing the reading quantity value prestored in the memory block to be read.
In some optional implementations of this embodiment, the apparatus 700 for inter-process transmission of a message may further include a comparing unit, a third reading unit, and a second waiting unit, which are not shown in fig. 7.
The comparison unit is used for responding to the completion of reading the information stored in the memory block to be read, and taking other memory blocks as new memory blocks to be read; and comparing the residual value with the serial number of the new memory block to be read.
And the third reading unit is used for reading the message stored in the new memory block to be read in response to the comparison unit determining that the serial number of the new memory block to be read is smaller than the remainder value.
And the second waiting unit is used for entering a process waiting state in response to the comparison unit determining that the sequence number of the new memory block to be read is equal to the remainder value.
The device for transmitting messages among processes provided by the above embodiments of the present application can read the latest written message, and ensure the synchronization of message processing.
Referring now to FIG. 8, shown is a block diagram of a computer system 800 suitable for implementing the apparatus for interprocess message transmission of the present application.
As shown in fig. 8, the computer system 800 includes a Central Processing Unit (CPU)801 that can perform various appropriate actions and processes in accordance with a program stored in a Read Only Memory (ROM)802 or a program loaded from a storage section 808 into a Random Access Memory (RAM) 803. In the RAM 803, various programs and data necessary for the operation of the system 800 are also stored. The CPU 801, ROM 802, and RAM 803 are connected to each other via a bus 804. An input/output (I/O) interface 805 is also connected to bus 804.
The following components are connected to the I/O interface 805: an input portion 806 including a keyboard, a mouse, and the like; an output section 807 including a signal such as a Cathode Ray Tube (CRT), a Liquid Crystal Display (LCD), and the like, and a speaker; a storage portion 808 including a hard disk and the like; and a communication section 809 including a network interface card such as a LAN card, a modem, or the like. The communication section 809 performs communication processing via a network such as the internet. A drive 810 is also connected to the I/O interface 805 as necessary. A removable medium 811 such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, or the like is mounted on the drive 810 as necessary, so that a computer program read out therefrom is mounted on the storage section 808 as necessary.
In particular, according to an embodiment of the present disclosure, the processes described above with reference to the flowcharts may be implemented as computer software programs. For example, embodiments of the present disclosure include a computer program product comprising a computer program tangibly embodied on a machine-readable medium, the computer program comprising program code for performing the method illustrated in the flow chart. In such an embodiment, the computer program can be downloaded and installed from a network through the communication section 809 and/or installed from the removable medium 811. The computer program performs the above-described functions defined in the method of the present application when executed by the Central Processing Unit (CPU) 801.
The flowchart and block diagrams in the figures illustrate the architecture, functionality, and operation of possible implementations of systems, methods and computer program products according to various embodiments of the present application. In this regard, each block in the flowchart or block diagrams may represent a module, segment, or portion of code, which comprises one or more executable instructions for implementing the specified logical function(s). It should also be noted that, in some alternative implementations, the functions noted in the block may occur out of the order noted in the figures. For example, two blocks shown in succession may, in fact, be executed substantially concurrently, or the blocks may sometimes be executed in the reverse order, depending upon the functionality involved. It will also be noted that each block of the block diagrams and/or flowchart illustration, and combinations of blocks in the block diagrams and/or flowchart illustration, can be implemented by special purpose hardware-based systems which perform the specified functions or acts, or combinations of special purpose hardware and computer instructions.
The units described in the embodiments of the present application may be implemented by software or hardware. The described units may also be provided in a processor, and may be described as: a processor includes a dividing unit, a first determining unit, a first monitoring unit, a locking unit, and a writing unit. The names of these units do not form a limitation on the unit itself in some cases, for example, the first monitoring unit may also be described as "a unit that monitors whether the memory block to be written is currently read or not".
As another aspect, the present application also provides a non-volatile computer storage medium, which may be the non-volatile computer storage medium included in the apparatus in the above-described embodiments; or it may be a non-volatile computer storage medium that exists separately and is not incorporated into the terminal. The non-transitory computer storage medium stores one or more programs that, when executed by a device, cause the device to: dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted; determining a memory block which is not written into currently in the plurality of memory blocks as a memory block to be written into; monitoring whether the memory block to be written is read currently; responding to the current read of the memory block to be written, and executing locking operation on the memory block to be written; and in response to the end of reading the memory block to be written, executing an unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written. Or mapping the address of the shared memory of the operating system into the address space of the operating system; determining a memory block which is newly written with the message as a memory block to be read from a plurality of memory blocks of the shared memory; monitoring whether the memory block to be read is written currently; responding to the current writing of the memory block to be read, and entering a process waiting state; and reading the information stored in the memory block to be read in response to the end of writing the memory block to be read.
The above description is only a preferred embodiment of the application and is illustrative of the principles of the technology employed. It will be appreciated by a person skilled in the art that the scope of the invention as referred to in the present application is not limited to the embodiments with a specific combination of the above-mentioned features, but also covers other embodiments with any combination of the above-mentioned features or their equivalents without departing from the inventive concept. For example, the above features may be replaced with (but not limited to) features having similar functions disclosed in the present application.

Claims (26)

1. A method for inter-process transfer of messages, the method comprising:
dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted, wherein the size of each memory block is larger than that of the message to be transmitted;
determining a memory block which is not written into currently in the plurality of memory blocks as a memory block to be written into;
monitoring whether the memory block to be written is read currently;
responding to the current read of the memory block to be written, and executing locking operation on the memory block to be written;
and in response to the end of reading the memory block to be written, executing an unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written.
2. The method according to claim 1, wherein the determining a currently unwritten memory block as the memory block to be written to comprises:
setting continuous serial numbers for the plurality of memory blocks;
gradually increasing the write quantity value prestored in the shared memory;
determining a sequence number of a first memory block corresponding to the increased write quantity value, where the write quantity value corresponds to the plurality of memory blocks one to one;
monitoring whether the first memory block is written in currently;
and in response to that the first memory block is not currently written to, taking the first memory block as the memory block to be written to.
3. The method according to claim 2, wherein the determining the sequence number of the first memory block corresponding to the incremented write quantity value includes:
determining the number of the plurality of memory blocks;
the increased writing quantity value is complemented with the quantity of the plurality of memory blocks;
and taking the remainder value as the serial number of the first memory block.
4. The method of claim 2, further comprising:
in response to that the first memory block is currently written, querying whether other memory blocks are currently written according to the sequence of the sequence numbers of the plurality of memory blocks;
and in response to the query that other memory blocks are not written currently, taking the memory blocks which are not written currently as the memory blocks to be written.
5. The method of claim 4, further comprising:
and in response to the query that other memory blocks are written currently, circularly querying according to the serial numbers of the plurality of memory blocks until one memory block is queried to be written currently, and taking the memory block which is not written currently as the memory block to be written.
6. The method according to any of claims 1 to 5, wherein the monitoring whether the memory block to be written is currently read comprises:
monitoring whether a reading quantity value prestored in the memory block to be written is equal to a preset value or not;
when the reading quantity value is equal to the preset value, determining that the memory block to be written is not read currently;
and when the reading quantity value is not equal to the preset value, determining that the memory block to be written is currently read.
7. The method of claim 1, further comprising:
when the message to be transmitted is written into the memory block to be written, setting a writing identifier prestored in the memory block to be written as a true value, wherein the true value corresponds to the current written-in memory block to be written;
and in response to the completion of writing the to-be-transmitted message into the to-be-written memory block, setting the write identifier to be a false value, where the false value corresponds to the to-be-written memory block that is not currently written into.
8. A method for inter-process transfer of messages, the method comprising:
mapping the address of the shared memory of the operating system into the address space of the operating system;
determining a memory block which is newly written with the message as a memory block to be read from a plurality of memory blocks of the shared memory;
monitoring whether the memory block to be read is written currently;
responding to the current writing of the memory block to be read, and entering a process waiting state;
and reading the information stored in the memory block to be read in response to the end of writing the memory block to be read.
9. The method according to claim 8, wherein the determining the memory block of the latest write message as the memory block to be read includes:
reading the serial number of each memory block in the shared memory and the prestored written quantity value;
the written quantity value is complemented with the quantity of the plurality of memory blocks;
and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
10. The method according to claim 8, wherein the monitoring whether the memory block to be read is currently written to comprises:
monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or
And monitoring whether a value of a write identifier prestored in the memory block to be read is a false value, wherein the false value corresponds to the memory block to be read which is not written currently.
11. The method of claim 8, further comprising:
and in response to that the memory block to be read is not written currently, reading the message stored in the memory block to be read.
12. The method according to claim 8 or 11, characterized in that the method further comprises:
in the process of reading the information stored in the memory block to be read, the reading quantity value prestored in the memory block to be read is increased progressively;
and in response to the completion of reading the message stored in the memory block to be read, decrementing the read quantity value prestored in the memory block to be read.
13. The method of claim 9, further comprising:
in response to the completion of reading the information stored in the memory block to be read, taking other memory blocks as new memory blocks to be read; comparing the residual value with the serial number of the new memory block to be read;
reading the message stored in the new memory block to be read in response to the serial number of the new memory block to be read being smaller than the remainder value;
and entering the process waiting state in response to the fact that the sequence number of the new memory block to be read is equal to the remainder value.
14. An apparatus for inter-process transfer of messages, the apparatus comprising:
the device comprises a dividing unit, a sending unit and a receiving unit, wherein the dividing unit is used for dividing a shared memory allocated by an operating system into a plurality of memory blocks according to the size of a message to be transmitted, and the size of each memory block is larger than that of the message to be transmitted;
a first determining unit, configured to determine, among the multiple memory blocks, a memory block that is not currently written to as a memory block to be written to;
a first monitoring unit, configured to monitor whether the memory block to be written is currently read;
a locking unit, configured to perform a locking operation on the memory block to be written in response to that the memory block to be written is currently read;
and the writing unit is used for responding to the end of reading the memory block to be written, executing unlocking operation on the memory block to be written, and writing the message to be transmitted into the memory block to be written.
15. The apparatus of claim 14, wherein the first determining unit comprises:
the setting module is used for setting continuous serial numbers for the plurality of memory blocks;
the assignment module is used for increasing the write quantity value prestored in the shared memory;
a first determining module, configured to determine a sequence number of a first memory block corresponding to the incremented write quantity value, where the write quantity value corresponds to the multiple memory blocks one to one;
a monitoring module, configured to monitor whether the first memory block is currently written in;
a second determining module, configured to, in response to that the first memory block is not currently written to, use the first memory block as the memory block to be written to.
16. The apparatus of claim 15, wherein the first determining module is further configured to:
determining the number of the plurality of memory blocks;
the increased writing quantity value is complemented with the quantity of the plurality of memory blocks;
and taking the remainder value as the serial number of the first memory block.
17. The apparatus of claim 15, wherein the first determining unit further comprises:
the query module is configured to, in response to that the first memory block is currently written to, query whether other memory blocks are currently written to according to the sequence of the sequence numbers of the plurality of memory blocks;
a third determining module, configured to, in response to the query that another memory block is not currently written to, use the memory block that is not currently written to as the memory block to be written to.
18. The apparatus of claim 17, wherein the first determining unit further comprises:
a fourth determining module, configured to, in response to that the other memory blocks are all currently written to, perform cyclic query according to the serial numbers of the multiple memory blocks until one memory block is queried that is not currently written to, and use the memory block that is not currently written to as the memory block to be written to.
19. The apparatus according to any one of claims 14-18, wherein the first monitoring unit comprises:
the monitoring module is used for monitoring whether a reading quantity value prestored in the memory block to be written is equal to a preset value or not;
a fifth determining module, configured to determine that the memory block to be written is not currently read when the read quantity value is equal to the preset value;
a sixth determining module, configured to determine that the memory block to be written is currently read when the read quantity value is not equal to the preset value.
20. The apparatus of claim 14, further comprising:
a first identification unit, configured to set, when the to-be-transmitted message is written into the to-be-written memory block, a true value of a write identification prestored in the to-be-written memory block, where the true value corresponds to a current write of the to-be-written memory block;
and a second identifier unit, configured to set, in response to completion of writing the to-be-transmitted message into the to-be-written memory block, the write identifier to a false value, where the false value corresponds to the to-be-written memory block and is not currently written into the to-be-written memory block.
21. An apparatus for inter-process transfer of messages, the apparatus comprising:
the memory mapping unit is used for mapping the address of the shared memory of the operating system into the address space of the memory mapping unit;
a third determining unit, configured to determine, from the multiple memory blocks of the shared memory, a memory block in which a message is written most recently as a memory block to be read;
a second monitoring unit, configured to monitor whether the memory block to be read is currently written in;
the first waiting unit is used for responding to the current writing of the memory block to be read and entering a process waiting state;
a first reading unit, configured to read a message stored in the memory block to be read in response to that the memory block to be read is written to the end.
22. The apparatus of claim 21, wherein the third determining unit is further configured to:
reading the serial number of each memory block in the shared memory and the prestored written quantity value;
the written quantity value is complemented with the quantity of the plurality of memory blocks;
and taking the memory block with the sequence number equal to the remainder value as the memory block to be read.
23. The apparatus of claim 21, wherein the second monitoring unit is further configured to:
monitoring whether a value of a write identifier prestored in the memory block to be read is a true value, wherein the true value corresponds to the current write of the memory block to be read; or
And monitoring whether a value of a write identifier prestored in the memory block to be read is a false value, wherein the false value corresponds to the memory block to be read which is not written currently.
24. The apparatus of claim 21, further comprising:
a second reading unit, configured to read a message stored in the memory block to be read, in response to that the memory block to be read is not currently written in.
25. The apparatus of claim 21 or 24, further comprising:
a first assignment unit, configured to increment a read quantity value prestored in the memory block to be read in the process of reading the message stored in the memory block to be read;
and the second assignment unit is used for responding to the completion of reading the information stored in the memory block to be read and decrementing the reading quantity value prestored in the memory block to be read.
26. The apparatus of claim 22, further comprising:
a comparing unit, configured to, in response to completion of reading the message stored in the memory block to be read, use another memory block as a new memory block to be read; comparing the residual value with the serial number of the new memory block to be read;
a third reading unit, configured to read a message stored in the new memory block to be read in response to that the serial number of the new memory block to be read is smaller than the remainder;
and the second waiting unit is used for entering the process waiting state in response to the fact that the sequence number of the new memory block to be read is equal to the remainder value.
CN201610640978.3A 2016-08-08 2016-08-08 Method and device for transmitting messages between processes Active CN107704325B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201610640978.3A CN107704325B (en) 2016-08-08 2016-08-08 Method and device for transmitting messages between processes

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201610640978.3A CN107704325B (en) 2016-08-08 2016-08-08 Method and device for transmitting messages between processes

Publications (2)

Publication Number Publication Date
CN107704325A CN107704325A (en) 2018-02-16
CN107704325B true CN107704325B (en) 2021-08-27

Family

ID=61168475

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201610640978.3A Active CN107704325B (en) 2016-08-08 2016-08-08 Method and device for transmitting messages between processes

Country Status (1)

Country Link
CN (1) CN107704325B (en)

Families Citing this family (6)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN108833477B (en) * 2018-05-16 2020-09-29 百度在线网络技术(北京)有限公司 Message transmission method, system and device based on shared memory
CN109656730B (en) * 2018-12-20 2021-02-23 东软集团股份有限公司 Cache access method and device
CN110515868A (en) * 2019-08-09 2019-11-29 苏州浪潮智能科技有限公司 The method and apparatus for showing image
CN111506436B (en) * 2020-03-25 2024-05-14 炬星科技(深圳)有限公司 Method for realizing memory sharing, electronic equipment and shared memory data management library
CN112749025A (en) * 2020-12-30 2021-05-04 深兰人工智能(深圳)有限公司 Data distribution method and device based on shared memory
CN115878343A (en) * 2021-09-29 2023-03-31 华为技术有限公司 Inter-process communication method and related device

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2001013229A2 (en) * 1999-08-19 2001-02-22 Venturcom, Inc. System and method for data exchange
CN1740978A (en) * 2004-08-23 2006-03-01 华为技术有限公司 Method for realing sharing internal stored data base and internal stored data base system
CN1787588A (en) * 2005-12-01 2006-06-14 大唐软件技术有限责任公司 Method for processing multiprogress message and method for processing multiprogress talk ticket
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
CN102693162A (en) * 2011-12-29 2012-09-26 中国科学技术大学苏州研究院 Method for process communication among multiple virtual machines on multi-core platform based on shared memory and intercore interruption
CN102750187A (en) * 2012-07-11 2012-10-24 北京联时空网络通信设备有限公司 Out-of-process interaction method and device
CN103197958A (en) * 2013-04-01 2013-07-10 天脉聚源(北京)传媒科技有限公司 Data transmission method and system and transponder device
CN104424030A (en) * 2013-08-22 2015-03-18 腾讯科技(深圳)有限公司 Sharing memory multi-process operation method and device

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO2001013229A2 (en) * 1999-08-19 2001-02-22 Venturcom, Inc. System and method for data exchange
CN1740978A (en) * 2004-08-23 2006-03-01 华为技术有限公司 Method for realing sharing internal stored data base and internal stored data base system
CN1787588A (en) * 2005-12-01 2006-06-14 大唐软件技术有限责任公司 Method for processing multiprogress message and method for processing multiprogress talk ticket
CN1920780A (en) * 2006-08-15 2007-02-28 杭州华为三康技术有限公司 Method and system for accessing critical region
CN102693162A (en) * 2011-12-29 2012-09-26 中国科学技术大学苏州研究院 Method for process communication among multiple virtual machines on multi-core platform based on shared memory and intercore interruption
CN102750187A (en) * 2012-07-11 2012-10-24 北京联时空网络通信设备有限公司 Out-of-process interaction method and device
CN103197958A (en) * 2013-04-01 2013-07-10 天脉聚源(北京)传媒科技有限公司 Data transmission method and system and transponder device
CN104424030A (en) * 2013-08-22 2015-03-18 腾讯科技(深圳)有限公司 Sharing memory multi-process operation method and device

Also Published As

Publication number Publication date
CN107704325A (en) 2018-02-16

Similar Documents

Publication Publication Date Title
CN107704325B (en) Method and device for transmitting messages between processes
US9967222B2 (en) Method and device for managing shared memory in robot operating system
US8224977B2 (en) Using local locks for global synchronization in multi-node systems
CN104424030B (en) Method and device for sharing memory by multi-process operation
CN108572876B (en) Method and device for realizing read-write lock
US20170353554A1 (en) Message transmitting method and device for a robot operating system
US8190857B2 (en) Deleting a shared resource node after reserving its identifier in delete pending queue until deletion condition is met to allow continued access for currently accessing processor
CN105468718B (en) Data consistency processing method, device and system
CN108845877B (en) Method, device and system for managing memory
US10817439B2 (en) Method and apparatus for transmitting a message
CN112087401B (en) Method and device for realizing service quality in distributed storage
US10133688B2 (en) Method and apparatus for transmitting information
US7970927B1 (en) Concurrent transmit processing
CN108399046B (en) File operation request processing method and device
CN114281484A (en) Data transmission method, device, equipment and storage medium
CN108614734B (en) Security parameter index management method and device
CN109271193B (en) Data processing method, device, equipment and storage medium
US20110302377A1 (en) Automatic Reallocation of Structured External Storage Structures
US9965344B2 (en) Method and apparatus for transmitting data in a robot operating system
CN115951845B (en) Disk management method, device, equipment and storage medium
CN111126604A (en) Model training method, device, server and storage medium
CN112235204B (en) Data reporting method, system, electronic equipment and storage medium
CN114675954A (en) Task scheduling method and device
CN113672400A (en) Data processing method, device and equipment and readable storage medium
CN108874560B (en) Method and communication device for communication

Legal Events

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