Universal inter-process communication method based on shared memory and suitable for C / C + + language
By using a shared memory allocator and adapting to the C/C++ language, the problems of data communication protocol and read/write synchronization in inter-process shared memory communication are solved, achieving efficient inter-process data sharing, reducing the difficulty of code writing and improving performance.
Patent Information
- Application Number
- CN202410542947.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-01
- Publication Date
- 2025-11-04
AI Technical Summary
Traditional inter-process shared memory communication methods require defining data communication protocols and synchronizing read and write data, which increases the difficulty of program writing and impacts performance.
It adopts a shared memory allocator and C/C++ language adaptation, provides safe memory allocation and release through the shared memory allocator, and manages inter-process communication data using a shared container, thus reducing the need for read and write data synchronization.
It reduces the difficulty of writing code for inter-process shared memory communication, improves program performance, and simplifies the conversion of multi-threaded programs to multi-process programs.
Smart Images

Figure CN120892220A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The technical field is a computer software system inter-process communication technology through shared memory. BACKGROUND
[0002] A computer process is a program instance with independence under the management of a computer operating system. Due to the complexity of a large software system, it is not feasible or necessary to use a single process to complete all software system functions. A large software system is often composed of multiple subsystems, and each subsystem is composed of one or more processes. The data interaction between subsystems or processes within a subsystem involves data transmission or data sharing between processes, that is, the problem of inter-process communication. There are multiple ways of inter-process communication, including files, pipes, message queues, signals, semaphores, sockets, and shared memory. Except for shared memory, the common feature of the above-mentioned methods is that the data exchanged must be transmitted through the kernel. When writing data, the writing process must call a system function to copy the data from the user memory space of the writing process to the kernel space. When reading data, the reading process must call a system function to copy the data from the kernel space to the user memory space of the reading process. Two system calls are required, which has a great impact on performance. Shared memory is a communication mechanism in which multiple processes can access the same memory region. Multiple processes map the same physical memory into their virtual memory space and directly read and write the memory, as if they all own the memory. Through effective read-write synchronization mechanisms, efficient data sharing between processes can be achieved without the need for two system calls, reducing the number of data copies and improving program performance. This avoids the disadvantages of other methods that require kernel transmission of shared data.
[0003] However, the traditional inter-process shared memory communication method still has the problem of data communication protocol and read-write data synchronization between processes. The data communication protocol is used to define the data type and data boundary, and the read-write data synchronization is used to safely read and write data to avoid multiple processes reading and writing the same memory at the same time. The data communication protocol and read-write data synchronization are guarantees for correct transmission and sharing of data between processes, which increases the difficulty of inter-process communication through shared memory. Therefore, how to develop a general inter-process shared memory communication method to solve the above problems has become a technical problem to be solved.
[0004] The present application is aimed at solving this problem and discloses a general inter-process communication implementation method based on shared memory and suitable for C / C++ languages, which does not require a data communication protocol and weakens read-write data synchronization. This method solves the problem of C / C++ language inter-process shared memory communication, reduces the difficulty of code writing for inter-process shared memory communication programs, and also provides the possibility of converting a multi-threaded program into a multi-process program, avoiding the difficulties of multi-threaded program development. SUMMARY
[0005] In order to solve the problem of defining data communication protocol and reading and writing data synchronization in traditional inter-process shared memory communication, the method solves the problem through shared memory allocator and C language / C++ language adaptation. The shared memory allocator is a general shared memory allocation method, which can allocate and release shared memory for C language variables or C++ language class objects. The allocation and release process is inter-process mutual exclusion, which ensures the safety of allocation and release and plays a role in reading and writing synchronization. The C language / C++ language adaptation provides a C language / C++ language container and container element created on the shared memory space obtained by the shared memory allocator, which is convenient for managing inter-process communication data and solves the problem of defining data communication protocol in traditional method.
[0006] The overall architecture of inter-process communication through shared memory is shown in the accompanying drawings of the specification Figure 1 (Multi-process shared memory communication diagram).
[0007] Figure 1 When the reading and writing processes are started, the shared memory is first loaded, and the shared memory is mapped to the process's own memory space. Then it is judged whether the shared container on the shared memory is initialized. If it is not initialized, the shared memory is first initialized, the shared memory allocator is created, and then the shared container is created. The initialization of the shared container depends on the initialization of the shared memory. The shared container initialization will create a container total pool, an empty container pool and a full container pool. The empty container pool stores empty containers, and the full container pool stores full containers inserted with elements. A certain number of empty containers are created in advance on the empty container pool. The above container pool and container allocator use the shared memory allocator, so that the memory of the container pool and the elements of the container is allocated from the shared memory space.
[0008] The writing process first locks the empty container pool, and gets the empty container from the empty container pool. If there is no empty container, it waits for the empty container pool to have an empty container. After getting the notification that the empty container pool has an empty container, the empty container is taken out, the shared data is written into the empty container, and the full container with written shared data is inserted into the full container pool, and the reading process is notified that the full container pool has a full container. Writing shared data into an empty container is to write data into a container element. The memory space of the container element is allocated from the shared memory space by the shared memory allocator. In this way, the shared data is written into the shared memory. The space of the shared data on the shared memory is defined by the shared memory allocator, and the writing process does not need to define the boundary of the data according to the communication data protocol.
[0009] The read process first locks the full container pool, obtains a full container from the full container pool, waits for the full container pool to have a full container if there is no full container, obtains the full container pool full container notification after obtaining the full container pool full container, extracts the full container, reads the shared data from the full container, empties the full container, the shared memory space occupied by the deleted full container element is also released, and then an empty container is inserted into the empty container pool, and the write process is notified that the empty container pool has an empty container. The shared data is read from the full container, and the memory space of the full container element is allocated from the shared memory space when the element is added to the full container. In this way, the shared data is read from the shared memory, and the read process does not need to define the data boundary according to the communication data protocol.
[0010] The data sharing mode of the above-mentioned read-write process through the container pool and the container is a specific implementation of the method, and the write process may also be responsible for the addition, deletion and modification of shared data, and the read process only needs to read the shared data. The specific implementation can be adjusted according to actual needs.
[0011] The shared memory initialization separates different regions of the shared memory according to functions, which is convenient for allocation, release and management of the shared memory. The method is based on the commonly used 64-bit operating system of the current computer system, and the memory address occupies 8 bytes. If it is a 32-bit operating system, the memory address occupies 4 bytes, and the memory distribution of different regions needs to be adjusted accordingly. The overall distribution of the shared memory space is shown in the accompanying drawings of the specification Figure 2-1 (Overall distribution of shared memory space).
[0012] The above-mentioned overall distribution of the shared memory space is not the only arrangement, and appropriate adjustments can be made in the actual method implementation to facilitate the implementation method as a benchmark. The initialization shared memory flow is shown in the accompanying drawings of the specification Figure 12 (initialization shared memory flow chart).
[0013] Because the shared container is built on the basis of the shared memory, the initialization of the shared container is based on the initialization of the shared memory, and 8 bytes of space at the beginning of the shared memory are reserved for storing the address of the shared container pool, which is convenient for all processes to connect the shared memory and obtain the shared container pool address.
[0014] The space for storing the container pool address is reserved, and the container pool address space distribution diagram is shown in the accompanying drawings of the specification Figure 2-2 (container pool address space distribution diagram).
[0015] The space for the shared memory mutex used when allocating and releasing the shared memory is reserved, and the mutex is initialized. The shared memory mutex space distribution is shown in the accompanying drawings of the specification Figure 2-3 (shared memory mutex space distribution diagram).
[0016] Space for memory block size array is reserved, which is used to get the corresponding memory block size by index, and is used when initializing the memory block free list range array. The space distribution of the memory block size array is shown in the accompanying drawings of the specification Figure 3-1 (Space distribution diagram of memory block size array).
[0017] The memory block size array index and the memory size corresponding to the index have a certain relationship, which is related to the specific implementation and can be determined by yourself. The maximum index 63 corresponds to a memory block size of 16320 bytes, Figure 3-1 (Space distribution diagram of memory block size array) is only an example. The relationship between the memory size corresponding to each index is: 0 index is 8 bytes, and other indexes are the memory block size of the previous index + (2 raised to the power of (index value / 8)) * 8. The memory block size value increases faster as the index value increases, in order to adapt to the management of various memory blocks.
[0018] Space for memory block free list range array is reserved, which is used to get the index of memory block free list head array by memory block size. According to the logic of "range beg < memory block size <= range end", the corresponding memory block free list range array index can be found, and the memory block free list head can be obtained from the memory block free list head array by the index, and then the memory block free list can be obtained. The space distribution of the memory block free list range array is shown in the accompanying drawings of the specification Figure 3-2 (Space distribution diagram of memory block free list range array). When initializing the memory block free list range array, the memory block size array needs to be used to get the memory block size by index.
[0019] Space for memory block free list head array is reserved, which is used to store the head nodes of free memory blocks of various sizes. When allocating shared memory blocks, the index corresponding to the free memory block is first found through the memory block free list range array, and then the head node of the memory block free list corresponding to the index is found in the memory block free list head array, and then the free block is found through the memory block free list and allocated to the process. The space distribution of the memory block free list head array is shown in the accompanying drawings of the specification Figure 3-3 (Space distribution diagram of memory block free list head array).
[0020] The above memory block free list heads all point to the structure shown in the accompanying drawings of the specification Figure 4-1 (Space distribution diagram of memory block free list head array element). Figure 4-2 (Space distribution diagram of memory block free list head array element). Figure 3-3 The fixed memory block list heads with indexes (0-4) of Figure 4-2The index (0-4) of the fixed memory block linked list head array element corresponding to the fixed memory block; if it is a non-fixed block linked list head, it points to the Figure 5-2 The idle non-fixed memory block linked list shown in the non-fixed memory block space distribution diagram.
[0021] The fixed memory block linked list head array space is reserved for storing the fixed memory block linked list head. The fixed memory block (also called small memory block or fixed block, the terms are interchangeable) linked list head array space is immediately followed by the memory block free linked list head array space, which stores the free fixed memory total block linked list head and the allocated fixed memory total block linked list head two addresses and the corresponding structure. Each fixed memory total block can link multiple fixed memory total blocks as needed. Each fixed memory total block contains 60 fixed memory blocks. The fixed memory total block space distribution is shown in the Figure 6-2 (fixed memory block total block space distribution diagram). The fixed memory total block header stores the allocation status of each fixed memory block (small memory block). Two allocation status fields are provided for 28+32=60 bits, and each small memory block allocation status occupies one bit. The header information of each small memory block includes block type, index position of small memory block in fixed memory total block, and small memory block size. The small memory block space distribution is shown in the Figure 6-3 (fixed memory block space distribution diagram). When the small memory block is released, the small memory block size and index can be used to find the fixed memory total block header where the small memory block is located, and the allocation status of the small memory block in the total block header is modified to free.
[0022] The fixed memory block linked list head array space distribution is shown in the Figure 4-2 (fixed memory block linked list head array space distribution diagram). The space distribution of the fixed memory block linked list head array element is shown in the Figure 4-3 (fixed memory block linked list head array element space distribution diagram).
[0023] The non-fixed memory block space contains all allocated and free non-fixed memory blocks (also called large memory blocks or non-fixed blocks, the terms are interchangeable). The free non-fixed memory blocks are linked to the memory block free linked list head array corresponding to the memory size through the predecessor and successor pointers in the block. The overall space distribution of the non-fixed memory block is shown in the Figure 5-1 (non-fixed memory block space distribution diagram), and the distribution of a single non-fixed memory block is shown in Figure 5-2 (non-fixed memory block space distribution diagram). The tail of the non-fixed memory block contains block allocation status information and block total size information. The space distribution diagram of the non-fixed memory block tail is shown in Figure 5-3 (non-fixed memory block tail space distribution diagram).
[0024] Tail non-fixed free memory block as the remaining unallocated memory, also known as tail block, with the whole block of free non-fixed memory block inserted into the last chain table of the head array of memory block free chain table, used for process to allocate various types of memory blocks, and the tail block pointer needs to be initialized at the end of the shared memory initialization. When allocating fixed memory total block, the required memory size will be cut from the tail of the tail block, and the tail block pointer will not move, and the size of the tail block, the predecessor and successor pointers and the tail information will be adjusted. When allocating non-fixed memory block, the required memory size will be cut from the head of the tail block, the tail block pointer will be moved accordingly, and the size of the tail block, the predecessor and successor pointers and the tail information will be adjusted. The tail block space distribution is shown in the accompanying drawings of the specification Figure 6-1 (Tail non-fixed free memory block space distribution diagram), the space distribution in the tail block is the same as that of the non-fixed memory block, which is shown in Figure 5-2 (non-fixed memory block space distribution diagram), compared with the general non-fixed memory block, the tail block only has a different role in allocating shared memory.
[0025] Multiple fixed memory total blocks, fixed blocks are memory blocks allocated in fixed sizes, used to optimize small memory block allocation and improve shared memory space utilization. Once the fixed memory total block is cut from the tail of the tail block, it will not be subjected to memory merging operation again. The allocation and release of fixed blocks are maintained through fixed memory total blocks and the chain table to which the fixed memory total blocks belong. The fixed memory total block distribution diagram is shown in the accompanying drawings of the specification Figure 6-2 (fixed memory total block space distribution diagram).
[0026] When allocating fixed blocks, the idle fixed memory total block chain table to which the fixed memory total block belongs will be found according to the size of the fixed block. If there is a fixed memory total block in the idle fixed memory total block chain table, an idle fixed block will be directly allocated from the fixed memory total block. If there is no fixed memory total block, a fixed memory total block will be cut from the tail of the tail block, the head of the fixed memory total block and 60 fixed blocks will be initialized, the newly added fixed memory total block will be inserted into the idle fixed memory total block chain table, an idle fixed block will be allocated from the newly added fixed memory total block, and the corresponding state bit of the fixed memory block in the fixed memory total block will be modified to allocated. If all the fixed blocks in the fixed memory total block are allocated, the fixed memory total block will be removed from the idle fixed memory total block chain table and inserted into the allocated fixed memory total block chain table, so as to avoid searching for idle fixed memory block from the total block again next time the fixed memory block is allocated. However, all the fixed memory blocks of the total block have been allocated, and the search will certainly fail, wasting CPU resources. The fixed memory block space distribution is shown in the accompanying drawings of the specification Figure 6-3 (fixed memory block space distribution diagram).
[0027] Shared container initialization is the prerequisite for process to read and write shared data, and the shared container includes container total pool, empty container pool and full container pool. Process reads and writes shared data through shared container. The shared container initialization process is shown in the accompanying drawings of the specificationFigure 11 (initialization of shared container flow chart).
[0028] Obtain the parameters for initializing the shared container, including the directory of shared memory, the capacity of the container pool, and the capacity of the container.
[0029] Shield other signals except the terminate process signal to avoid the process from being terminated unexpectedly due to receiving other signals, and install a processing function for the terminate process signal, so that the process can be normally terminated when the signal processing function receives the terminate process signal, thereby avoiding the process from being terminated unexpectedly and causing the consistency of shared memory data to be destroyed.
[0030] Open the shared memory in a read-write manner, obtain the size of the shared memory, and map the shared memory to the process's own virtual memory space in a manner that can be shared and read and written by the process.
[0031] The started process will determine whether the shared memory has been initialized, and if not, it will first initialize the shared memory, take the starting address of the shared memory space as the address of the container total pool, and then construct an empty container pool and a full container pool, and insert the empty container pool and the full container pool into the container total pool.
[0032] The empty container pool and the full container pool themselves contain their own mutexes and condition variables, and the mutexes and condition variables of the empty container pool and the full container pool are initialized to facilitate the concurrent management of the empty containers and the full containers in the respective pools.
[0033] Construct empty containers with the number of parameters of the empty container, and insert the empty containers into the empty container pool for the write process to obtain the empty containers.
[0034] If the process determines that the shared memory has been initialized, it only needs to connect the shared memory address space and obtain the container total pool address from the starting position of the shared memory to obtain the empty container pool and the full container pool, and further obtain the empty containers and the full containers.
[0035] Shared memory allocation is the basis for reading and writing C / C++ language variables and objects between processes. When a process allocates shared memory, it can be allocated in two ways according to the required memory size: fixed memory block (small memory block) allocation and non-fixed memory block (large memory block) allocation. The fixed memory block can also be understood as a small memory block. Memory blocks of 40 bytes or less (including 40 bytes, the number of bytes can be determined by the specific implementation) are considered small memory blocks, and memory blocks exceeding 40 bytes are considered non-fixed memory blocks or large memory blocks. The allocation and release of the two types of memory blocks are different, and the main reason is the consideration of the space cost of memory allocation. By optimizing the allocation of small memory blocks, the space allocation cost of small memory blocks is reduced, and since the allocation of small memory blocks is always the main part of the process memory allocation, not optimizing it will waste memory space. The shared memory allocation process is shown in the accompanying drawings of the specification Figure 13 (shared memory allocation flow chart).
[0036] When a large memory block (non-fixed memory block) is allocated, in addition to the memory actually allocated by the process, the head (8 bytes) and tail (8 bytes) of the memory block are also included. The head information is used to identify the block size, that is, the size of the memory available to the process (that is, the memory space between the head and the tail). The size of the memory available to the process is an integer multiple of 8 bytes (in order to align the memory, a few bytes of memory are allocated in addition to the actual memory requested as available memory to ensure that the available memory is an integer multiple of 8 bytes, which is beneficial for CPU addressing to improve process running speed). The tail information is used to identify the size of the entire memory block (including the head, available memory, and tail itself) and the state of the memory block. With the tail information of the non-fixed memory block, the allocation state and block size information of the adjacent non-fixed memory blocks before and after the non-fixed memory block can be obtained, which facilitates the merging of multiple adjacent free non-fixed memory blocks into larger non-fixed memory blocks, enabling the recycling of free non-fixed memory blocks and solving the allocation problem when the memory space is insufficient. It can also solve the problem of fragmentation of shared memory space.
[0037] When a small memory block (fixed memory block) is allocated, if it is still allocated as a large memory block, not only the head (8 bytes) and tail (8 bytes) of the large memory block are required, but also pointers to the previous and next free blocks are required. The entire memory block requires at least 32 bytes (head (8 bytes), predecessor node pointer (8 bytes), successor node pointer (8 bytes), and tail (8 bytes)) of space, which is very uneconomical for small memory space. When allocating small memory, only the head and the size of the memory available to the process are allocated, and the size of the available memory is also rounded up to an integer multiple of 8 bytes. The reason is the same as for large memory block allocation, which eliminates the tail and the chain table pointer to the previous and next blocks when it is free. This greatly improves the efficiency of space allocation. For example, when allocating 8 bytes of memory, according to the small memory block, only 16 bytes of total memory (8 bytes of head plus 8 bytes of available memory) need to be allocated. According to the large memory block, at least 32 bytes (head, tail, and chain table pointer to the previous and next blocks) are required. The space utilization is doubled.
[0038] When a process allocates shared memory, depending on the size of the memory to be allocated, it is determined whether to allocate fixed memory or non-fixed memory. If the memory block space to be allocated is <= 40 bytes, it is allocated as a fixed memory block. If it is > 40 bytes, it is allocated as a non-fixed memory block.
[0039] When allocating fixed memory blocks, first, the memory block free list range array is queried according to the size of the memory block to be allocated. According to the logic of "range array element beg < memory block size <= range array element end", the index of the memory block free list head array is obtained. The memory block free list head is obtained by using the index. The fixed memory block list head array is obtained by using the memory block free list head. The free fixed memory total block list is obtained. The free fixed memory total block is obtained from the free fixed memory total block list. If there is a free fixed memory total block, an idle fixed memory total block is directly obtained. If there is no free fixed memory total block, a space containing 60 fixed memory blocks and the head of the fixed memory total block is cut from the rear of the tail block of the shared memory. All 60 fixed memory blocks are initialized. The type, index and size of each fixed memory block are assigned corresponding values according to the type of the fixed memory block. The free fixed memory total block header information is also initialized, including the total block type, the fixed block chain table to which the total block belongs, and the allocation state of the fixed block. The detailed information of each field can be found in Figure 6-2 (Fixed memory total block space distribution diagram) and Figure 6-3 (Fixed memory block space distribution diagram). Then the newly allocated fixed memory total block is inserted into the free fixed memory total block list, and the newly allocated free fixed memory total block is obtained. A fixed memory block with an idle allocation state is found in the obtained free fixed memory total block, and the allocation state bit of the corresponding fixed block in the free fixed memory total block is modified to allocated. It is judged whether all fixed blocks in the free fixed memory total block are allocated. If all fixed blocks are allocated, the free fixed memory total block that has been allocated is removed from the free fixed memory total block list and inserted into the allocated fixed memory total block list. The reason for distinguishing between the free and allocated fixed memory total block lists is to avoid the fixed memory total block that has been allocated all fixed blocks remaining in the free fixed memory total block list. When allocating free fixed blocks next time, it is necessary to try to allocate from the fixed memory total block that has been allocated all fixed blocks. Such a try will inevitably fail, and it will be necessary to try other free fixed memory total blocks, resulting in waste of CPU resources. The fixed memory block allocation process is shown in the Figure 14 (Fixed memory allocation flowchart).
[0040] When allocating a non-fixed memory block, first, the range array of the memory block free list is queried according to the size of the memory block to be allocated, and the index value of the range array is found according to the logic of "range array element's beg < memory block size <= range array element's end". The index is the index of the memory block free list head array. The non-fixed memory block free list head is found by using the index. An idle non-fixed memory block is obtained from the non-fixed memory block free list. If the non-fixed memory block free list is empty, the tail block is directly obtained. If the obtained is the tail block, the required non-fixed memory block is cut from the front part of the tail block. The available memory size of the head of the cut memory block, the block state (changed to allocated) of the tail, and the block size information are modified. The available memory size of the head of the remaining block and the block size information of the tail are also modified accordingly. The non-fixed memory block free list is obtained according to the available size of the remaining block. The idle non-fixed memory block obtained after the cut is reinserted into the non-fixed memory block free list for subsequent allocation. If the obtained idle non-fixed memory block is the tail block, the tail block pointer is modified after the cut. The non-fixed memory block allocation process is shown in the flowchart of the accompanying drawings Figure 15 (non-fixed memory block allocation flowchart).
[0041] In order to ensure the consistency and safety of shared memory data, the shared memory needs to be locked before the process allocates memory. The lock is divided into shared memory mutex lock or memory block chain table lock. The shared memory mutex lock is to lock the whole shared memory, and the memory block chain table lock is to lock the specific memory block chain table. The lock granularity is finer, and the concurrency of multiple processes is stronger. The memory is unlocked after being allocated, and one method can be selected for implementation.
[0042] The shared memory release is the process releasing the shared memory block after the use of the shared memory block is completed. The shared memory block size information is obtained by moving 4 bytes from the address of the shared memory block to be released. The shared memory block size can know whether the shared memory block is a fixed memory block or a non-fixed memory block. According to the allocated available memory size, the release method is divided into two types: fixed memory block (small memory block) release and non-fixed memory block (large memory block) release. The available memory size <= 40 bytes is released as a fixed memory block, and the size greater than 40 bytes is released as a non-fixed memory block. The shared memory block release process is shown in the flowchart of the accompanying drawings Figure 16 (shared memory release flowchart).
[0043] The fixed memory block (small memory block) release, the fixed block header information (fixed block type, size and index in the total block) is obtained, the fixed memory total block information is obtained, the fixed block index corresponding to the fixed block allocation state bit in the fixed memory total block information is changed to idle, the memory block free list range array index is obtained through the fixed block size, and then the memory block free list head is obtained through the index to obtain the fixed memory block list head array. If the state of the fixed memory total block is in the allocated fixed memory total block list, and all the fixed memory blocks are in the idle state, the state of the fixed memory total block is changed to be in the idle fixed memory total block list, and the fixed memory total block is moved to the idle fixed memory total block list. The idle fixed memory total block list and the allocated fixed memory total block list are set for the fixed memory total block list, so as to speed up the allocation of the idle fixed memory block, avoid the interference of the subsequent idle fixed memory block allocation caused by the fixed memory total block with no idle fixed memory block and the fixed memory total block with idle fixed memory block stored in one list, and improve the allocation efficiency.
[0044] The non-fixed memory block (large memory block) release, the non-fixed block header and tail information are obtained according to the available size of the shared memory, the memory block allocation state of the tail is changed to idle, the memory block free list array index of the non-fixed memory block is obtained, the non-fixed memory block free list head is obtained through the index, and the idle non-fixed memory block is inserted into the non-fixed memory block free list to achieve the purpose of releasing the idle non-fixed memory block.
[0045] As in the shared memory allocation, in order to ensure the data consistency and safety of the shared memory, the process needs to lock the shared memory before releasing the shared memory. The lock is divided into a shared memory mutex or a memory block list lock. The shared memory mutex is to lock the whole shared memory, and the memory block list lock is to lock the specific memory block list. The lock granularity is finer, and the concurrency of multiple processes is stronger. The memory is unlocked after being released, and one method can be selected for implementation.
[0046] Idle non-fixed memory block merging, when the shared memory is insufficient, the front and rear non-fixed memory blocks adjacent to the idle non-fixed memory block address are found, if the adjacent blocks are also idle blocks, the adjacent blocks are merged, the head and tail information of the merged block are modified, and the new block after merging is re-inserted into the non-fixed memory block free list corresponding to its size. Since the merging of idle non-fixed memory will reduce the memory allocation efficiency, the merging action is triggered as little as possible, only when the size of the idle non-fixed memory block held by the tail block is less than 5% of the total size of the shared memory, the merging action is triggered, and when the size of the merged block is greater than 15% or all the mergable blocks are merged, the merging action is stopped to avoid frequent merging leading to a significant decrease in allocation efficiency. The specific merging action trigger threshold can be set by the implementation.
[0047] Read and write process start, processes using shared memory can be roughly divided into read processes and write processes, processes may read and write, or only read or write, and can be classified as read processes and write processes. Whether the write process or the read process starts, it is necessary to create a shared memory interface object, initialize the shared memory container, and create read and write worker threads after the initialization of the shared memory container is successful.
[0048] The write process worker thread accepts the container total pool as a thread parameter, and the write worker thread obtains the full container pool and the empty container pool from the container total pool. The empty container pool stores empty containers, and the write worker thread locks the empty container pool. If there is no empty container in the empty container pool, the lock is released and the write worker thread waits for an empty container. When there is an empty container in the empty container pool, an empty container is taken out, the empty container pool lock is released, the write thread generates shared data, and the shared data is written to the empty container. Then, the full container pool is locked, the full container is added to the full container pool, and a signal is sent to the read process waiting for the full container pool to have a full container. Then, the full container pool lock is released, and the write thread continues to wait for an empty container in the empty container pool.
[0049] The write process flowchart is shown in the accompanying drawings of the specification Figure 7 (write process flowchart).
[0050] The write worker thread flowchart is shown in the accompanying drawings of the specification Figure 8 (write worker thread flowchart).
[0051] The read process working thread accepts the total container pool as a thread parameter, and the read working thread obtains the full container pool and the empty container pool from the total container pool, the full container pool stores the full containers written by the write process, the read working thread locks the full container pool, if there is no full container in the full container pool, the lock is released and the read working thread waits until there is a full container, when the full container pool has a full container, the read working thread takes a full container, releases the full container pool lock, the read working thread reads the shared data from the full container, and empties the full container, locks the empty container pool, adds the empty container to the empty container pool, sends a signal to the write process waiting for the empty container pool to have an empty container, and then releases the empty container pool lock, and then continues to wait for the full container pool to obtain a full container.
[0052] The read process flowchart is shown in the accompanying drawings of the specification Figure 9 (Read process flowchart).
[0053] The read working thread flowchart is shown in the accompanying drawings of the specification Figure 10 (Read working thread flowchart).
[0054] The above is the general execution flow of the read and write processes, and in actual implementation, the specific implementation method can be adjusted according to the actual situation.
[0055] The termination of the read and write processes needs to be strictly handled, and the "kill -9 process number" command of the Unix system cannot be used to forcibly kill the process under special circumstances, so as to avoid the consistency of the shared memory data being destroyed, resulting in unsafe shared memory data. When terminating the process, a termination signal needs to be sent to the process, and the process installs a termination signal processing function when starting, and after the process captures the termination signal, the process state is modified to termination by the signal processing function.
[0056] The write process sends a signal to the empty container pool waiting process, and after the write process working thread receives the signal, it judges that the process state is termination, and then exits the waiting and releases the empty container pool lock, and then exits the write working thread, and then the write process normally exits. The write process termination flowchart is shown in the accompanying drawings of the specification Figure 17 (Written process termination flowchart).
[0057] The read process sends a signal to the full container pool waiting process, and after the read process working thread receives the signal, it judges that the process state is termination, and then exits the waiting and releases the full container pool lock, and then exits the read working thread, and then the read process normally exits. The read process termination flowchart is shown in the accompanying drawings of the specification Figure 18 (Read process termination flowchart).
[0058] Through the above method, the inter-process shared memory communication function can be realized in the form of a shared library, shielding the underlying implementation details, making inter-process communication through shared memory easier. For multi-threaded programs, multiple threads can also be split into multiple processes, implemented as a multi-process program through the above shared memory communication method, reducing the difficulty of code writing for multi-threaded programs. BRIEF DESCRIPTION OF DRAWINGS
[0059] Figure 1 is a general diagram of inter-process shared memory communication, multiple write processes write data to shared memory, multiple read processes read data from shared memory, write processes take empty containers from the empty container pool, insert full containers into the full container pool after writing data, read processes take full containers from the full container pool, empty full containers after reading data, and insert empty containers into the empty container pool for write processes to continue writing.
[0060] Figure 2 contains Figure 2-1 (shared memory space distribution diagram), Figure 2-2 (container pool address space distribution diagram), and Figure 2-3 (shared memory mutex space distribution diagram). The shared memory space distribution diagram describes the role of each part of the shared memory space, which is divided into 9 parts according to different functions, as described in the previous initialization of shared memory; the container pool address space distribution diagram is the container pool address information; the shared memory mutex space distribution diagram is the shared memory mutex space information.
[0061] Figure 3 contains Figure 3-1 (memory block size array space distribution diagram), Figure 3-2 (memory block free list range array space distribution diagram), and Figure 3-3 (memory block free list head array space distribution diagram). The memory block size array is the index corresponding to each memory block, which can be used to obtain the memory block size for constructing the memory block free list range array; the memory block free list range array is an array index obtained by the size of the free memory block, which is used to obtain the memory block free list head of Figure 3-3 (memory block free list head array) through the index; the memory block free list head array is a linked list head for storing memory block free lists, which is used to obtain memory block linked list locks and memory block free lists through the linked list head.
[0062] Figure 4 contains Figure 4-1 (memory block free list head array element space distribution diagram), Figure 4-2 (fixed memory block linked list head array space distribution diagram), and Figure 4-3(Fixed memory block linked list head array element space distribution diagram). The memory block free linked list head array element stores the memory block linked list lock and the memory block linked list head, the memory block linked list lock is used for allocating and releasing the shared memory block, and the memory block linked list head points to the memory block linked list; the fixed memory block linked list head array stores the fixed memory block linked list head, including the free fixed memory total block linked list head and the allocated fixed memory total block linked list head; Figure 4-3 is the description of Figure 4-2 the fixed memory block linked list head array element.
[0063] Figure 5 contains Figure 5-1 (non-fixed memory block space distribution diagram), Figure 5-2 (non-fixed memory block space distribution diagram), and Figure 5-3 (non-fixed memory block tail space distribution diagram). The non-fixed memory block (large memory block) is the space where the non-fixed memory block is located, including free and allocated; the non-fixed memory block includes block type, block state, block allocation state, memory block size, predecessor pointer, successor pointer and tail, etc. Key fields are described in the figure; the non-fixed memory block tail includes block state information and block total size information, which is used to obtain the information of the adjacent non-fixed memory block.
[0064] Figure 6 contains Figure 6-1 (tail non-fixed free memory block space distribution diagram), Figure 6-2 (fixed memory total block space distribution diagram), and Figure 6-3 (fixed memory block space distribution diagram). The tail non-fixed free memory block, also known as the tail block, is the whole block of free memory that is not allocated and used in the shared memory space. When the initialization of the shared memory is completed, only the tail block is the free non-fixed memory block. In the later stage, whether it is a fixed memory block or a non-fixed memory block, it is allocated from the tail block. The difference is that the fixed memory block is divided from the rear part of the tail block, and the non-fixed memory block is divided from the front part of the tail block; the fixed memory total block is the sum of the space of the 60 small memory blocks and the total block head part divided from the rear part of the tail block at one time. The total block head part contains the block type, the allocation state information of the 60 small memory blocks, and the state information of the total block on the free fixed memory total block linked list or the allocated fixed memory total block linked list. The fixed memory total block is linked into the fixed memory total block linked list through the fixed memory total block front and rear driver node pointers; the fixed memory block contains the fixed block type, the fixed block index, the fixed block size and the fixed memory space part, which is described by Figure 6-3 .
[0065] Figure 7 is the write process flowchart, which describes the running process of the write process, and is specifically described in the part of the invention.
[0066] Figure 8is a write worker thread flow chart, describing the running flow of write worker thread, see the part of summary of the invention.
[0067] Figure 9 is a read process flow chart, describing the running flow of read process, see the part of summary of the invention.
[0068] Figure 10 is a read worker thread flow chart, describing the running flow of read worker thread, see the part of summary of the invention.
[0069] Figure 11 is an initialization shared container flow chart, describing the running flow of initialization shared container, see the part of summary of the invention.
[0070] Figure 12 is an initialization shared memory flow chart, describing the running flow of initialization shared memory, see the part of summary of the invention.
[0071] Figure 13 is a shared memory allocation flow chart, describing the running flow of shared memory allocation, see the part of summary of the invention.
[0072] Figure 14 is a fixed memory allocation flow chart, describing the running flow of fixed memory allocation, see the part of summary of the invention.
[0073] Figure 15 is a non-fixed memory allocation flow chart, describing the running flow of non-fixed memory allocation, see the part of summary of the invention.
[0074] Figure 16 is a shared memory release flow chart, describing the running flow of shared memory release, see the part of summary of the invention.
[0075] Figure 17 is a write process termination flow chart, describing the running flow of write process termination, see the part of summary of the invention.
[0076] Figure 18 is a read process termination flow chart, describing the running flow of read process termination, see the part of summary of the invention.
Claims
1. A method for inter-process communication by shared memory in a computer system, characterized by Two parts: shared memory allocator and C / C++ adaptation. The shared memory allocator is used to allocate and release memory space of shared memory for processes, for the processes to create C / C++ class objects on the shared memory space. The C / C++ adaptation uses the shared memory allocator to create container pools and containers in the shared memory space, and uses the container pools and containers to manage C / C++ container elements, so that the processes can communicate through direct reading and writing of C / C++ container elements using shared memory, and through a reading and writing synchronization mechanism, efficient and safe inter-process data sharing is achieved. This method is applicable to all computer software systems written in C / C++, such as unix, linux, windows, apple, mobile operating systems and various embedded systems.