Process communication method, device and electronic equipment

By setting the atomic variable process lock array variable in the core area of ​​the shared memory, the problem of low shared memory communication efficiency in the existing technology is solved, and more efficient and secure inter-process communication is achieved.

CN118860697BActive Publication Date: 2025-01-21STELIGHT INSTR CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411328761.X
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-24
Publication Date
2025-01-21
Estimated Expiration
2044-09-24

AI Technical Summary

Technical Problem

The existing shared memory communication methods are inefficient in communication, especially in the limitation of process locks and data competition problems.

Method used

By setting the process lock array variable in the core area of ​​the shared memory and setting it as an atomic variable, the active process is allowed to obtain access qualifications based on the access status of the allocated shared memory, and the target shared memory is locked and Unlock operation.

Benefits of technology

It improves the efficiency of inter-process communication, shortens communication time, enhances the execution efficiency and security of locking operations, avoids interaction with the system kernel, and improves the ability of multiple processes to access shared memory in parallel.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118860697B_ABST
    Figure CN118860697B_ABST
Patent Text Reader

Abstract

The present application provides a process communication method, device and electronic device, wherein the active process obtains access qualifications to the allocated shared memory according to the current access status of the allocated shared memory; the array member variable corresponding to the target shared memory is determined from the process lock array variable corresponding to the allocated shared memory; the data bit corresponding to the active process is determined from the array member variable, and the value stored in the data bit is set to the value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory; wherein the array member variables of the process lock array variable are respectively used to store the lock state of each access process of the allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and is set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space. The communication time is shortened and the communication efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a process communication method, device and electronic device. Background Art

[0002] In the use scenarios of instruments, there are often requirements for multiple clients, that is, one client (as the control end) is required to control the instrument, while one or more clients (as the monitoring end) only synchronize the configuration and information of the current instrument. However, there is often only one link connecting the instrument, so the only way is to try to synchronize the memory data of the control end to the monitoring end through inter-process communication.

[0003] At present, the basic multi-process communication methods mainly include: TCP, UDP, pipes, shared memory, signals, semaphores, message queues, etc. Considering the communication efficiency, among the above methods of inter-process communication, only shared memory is applicable. The current shared memory interfaces mainly include POSIX shared memory, Windows shared memory, further encapsulated Boost shared memory, and Nginx shared memory module.

[0004] POSIX shared memory is suitable for Linux development, while Windows shared memory is suitable for Windows environment development. Boost's shared memory is encapsulated on the basis of both, so that it can support multiple platforms. The above solutions are all the most basic function implementations, and more business methods need to be implemented in subsequent use. Compared with the above solutions, Nginx has achieved higher performance process lock conditions and event sending and receiving based on shared memory, but it still fails to meet the requirements. It is mainly reflected in the restrictions on process locks. Specifically, in order to avoid data competition and inconsistency, locks are inevitably needed, but basic shared memory does not support process locks; and the process read-write lock of the shared memory encapsulated by Boost actually uses semaphores, and the additional overhead of locking and unlocking is close to the millisecond (ms) level; and Nginx's process lock does not support read-write lock types. Summary of the invention

[0005] The embodiments of the present application provide a process communication method, device and electronic device to solve the problem of low communication efficiency of the existing shared memory communication method.

[0006] Based on the above problem, in a first aspect, a process communication method is provided, comprising:

[0007] The active process obtains access qualification to the allocated shared memory according to the current access status of the allocated shared memory;

[0008] Determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory;

[0009] Determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory;

[0010] Among them, the array member variables of the process lock array variable are respectively used to store the lock status of the access process of each allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space.

[0011] In combination with the first aspect, in a possible implementation, the active process obtains access qualifications to the allocated shared memory based on the current access status of the allocated shared memory, including: traversing each array member of the memory access status array variable in turn; when it is determined that the numerical value stored in any array member represents that the corresponding access qualification is not occupied, modifying the numerical value to represent that the corresponding access qualification is occupied; when the modification is successful, determining that the active process obtains access qualifications to the allocated shared memory; when the modification is unsuccessful, continuing to traverse the array members of the memory access status array variable; wherein, the memory access status array variable is used to store the allocation of access qualifications to the allocated shared memory; the memory access status array variable is an atomic variable and is set in the shared memory core area; the number of array members of the memory access status array variable represents the maximum number of processes allowed to access the allocated shared memory in parallel.

[0012] In combination with the first aspect, in a possible implementation, after the active process obtains access to the allocated shared memory, it also includes: modifying the numerical value to represent the serial number of the array member whose corresponding access qualification is occupied in the memory access status array variable, and determining it as the identification number of the active process.

[0013] In combination with the first aspect, in a possible implementation, the active process includes a writing process; after the writing process obtains the access qualification, it also includes: obtaining permission to perform write operations on the target shared memory, including: obtaining the first process number stored in the corresponding member of the target shared memory from the read-write permission array variable; determining whether the first process number is the second process number of the writing process; if so, determining that permission to perform write operations on the target shared memory is obtained; if not, replacing the first process number with the second process number; if replacing the first process number with the second process number is unsuccessful, returning to the step of obtaining the first process number stored in the corresponding member of the target shared memory; wherein the read-write permission array variable is used to store the process number of the process that has write permission to the corresponding shared memory; the read-write permission array variable is an atomic variable and is set in the shared memory core area; the number of array members of the read-write permission array variable represents the maximum amount of shared memory that can be applied for.

[0014] In combination with the first aspect, in a possible implementation, the active process includes a writing process; before locking the target shared memory, it also includes: reading the current value of the array member variable corresponding to the target shared memory in the process lock array variable; and determining, based on the current value, that no process is reading the target shared memory; wherein the number of members of the process lock array variable represents the maximum number of shared memories allowed to be allocated; the maximum number of processes supported by each shared memory for concurrently accessing the shared memory is determined based on the maximum number of bits supported by the operating system where the active process is running, and the number of data bits required to represent multiple lock states.

[0015] In combination with the first aspect, in a possible implementation, the following method is used to determine whether there is a process currently reading the target shared memory: a bitwise AND operation is performed on the current value of the array member variable and a preset mask; the preset mask is obtained by setting the value of each data bit in the array member variable of the process lock array variable to the value of the read lock; when the AND result is zero, it is determined that there is no process currently reading the target shared memory.

[0016] In combination with the first aspect, in a possible implementation, determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory includes: determining, from the process lock array variable, an array member variable with an identification number assigned to the target shared memory as an array member number; the active process includes a write process; determining the data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, to lock the target shared memory, and access the target shared memory, including: determining the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; determining the data bit corresponding to the active process according to the offset, and setting the value stored in the data bit to a value representing that the lock state is a write lock, to write lock the target shared memory, and write shared data to the target shared memory; and / or

[0017] The active process includes a reading process; determining the data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory, including: determining the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; determining the data bit corresponding to the active process according to the offset, and setting the value stored in the data bit to a value representing that the lock state is a read lock, so as to read the target shared memory and read the target shared memory.

[0018] In combination with the first aspect, in a possible implementation, the active process includes a reading process; the reading process includes multiple threads; determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory, including: when the first thread among the multiple threads needs to perform a read operation, determining the array member variable corresponding to the target shared memory from the process lock array variable; determining the data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory, and access the target shared memory, including: determining the data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing that the lock state is a read lock, so as to read the target shared memory, and read the target shared memory;

[0019] The method also includes: determining a count array member in a read lock count array variable for performing read lock counting for the reading process, and recording a unit value in the count array member; when a thread other than the first thread among the multiple threads needs to perform a read operation, accumulating the number of threads that read the target shared memory according to the unit value in the count array member; after any thread among the multiple threads completes reading, subtracting the unit value from the count of the count array member; when the count of the count array member is reduced to zero, setting the value stored in the corresponding data bit in the process lock array variable to a value representing the lock state as unlocked; wherein the read lock count array variable represents the number of threads currently accessing the corresponding shared memory by each process that has access qualifications to the corresponding shared memory; and the number of members of the read lock count array variable represents the maximum number of shared memories allowed to be allocated.

[0020] In combination with the first aspect, in a possible implementation, the method further includes: incrementing the value stored in the successfully modified member at a preset time interval; cleaning up the inactive process in the following manner: traversing each member of the memory access status array variable in turn, and performing the following operations in each traversal: determining whether the value stored in each member is the same as that in the previous traversal; if the same, accumulating the number of times the corresponding member is judged to be the same; determining whether the accumulated number reaches a preset number; if so, modifying the value stored in the corresponding member to represent that the corresponding access qualification is not occupied; in the case of successful modification, determining the target process represented by the value stored in the corresponding member; setting the corresponding lock state of the target process in the process lock array variable to unlocked; in the case where the target process shown is a write process, clearing the process number stored in the read and write permission array variable of the target process.

[0021] In combination with the first aspect, in a possible implementation, the active process includes a writing process; the writing process writes the shared data into the target shared memory in the following manner: determining a writing offset of the shared data in the target shared memory; writing the shared data into the target shared memory according to the writing offset, and updating the current writing offset; assigning a value to a writing index variable according to the number of units of the written shared data; wherein the writing offset, the writing data size, and the writing index are stored in the shared memory user area;

[0022] The active process includes a read process; the read process reads the target shared memory in the following manner: obtains the value of the current write index variable and the value of the read index variable; when the value of the write index variable is inconsistent with the value of the read index variable and the read index variable is valid, reads the shared data according to the write offset and the unit data amount represented by the write index variable; and updates the value of the read index variable.

[0023] In combination with the first aspect, in a possible implementation manner, the active process includes a control end process as a write process and a plurality of monitoring end processes as read processes.

[0024] In a second aspect, a process communication device is provided, comprising:

[0025] An access qualification acquisition module is used for an active process to acquire access qualifications to the allocated shared memory according to the current access status of the allocated shared memory;

[0026] The locking module is used to determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory; determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to the value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory;

[0027] Among them, the array member variables of the process lock array variable are respectively used to store the lock status of the access process of each allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space.

[0028] According to a third aspect, an electronic device is provided, comprising: a processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the process communication method described in the first aspect or in combination with any possible implementation of the first aspect are performed.

[0029] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the process communication method as described in the first aspect or in combination with any possible implementation of the first aspect are executed.

[0030] The beneficial effects of the embodiments of the present application include:

[0031] A process communication method, device and electronic device provided by the embodiment of the present application. The process communication method provided by the embodiment of the present application sets a process lock array variable for locking for access to shared memory, and sets the process lock array variable in the shared memory core area, and sets the shared memory in the shared memory user area. Since the shared memory core area and the shared memory user area are both set in the user space, not only the regional functional division is realized, but also the active process can set the process lock through the process lock array variable in the user space without interacting with the system kernel. Compared with the related art in which the shared memory does not support the process lock, or the supported process lock actually uses a semaphore, the communication time is shortened and the communication efficiency is improved; and the embodiment of the present application sets the process lock array variable as an atomic variable, so that the operation of the active process on the process lock array variable is an atomic operation, which further improves the execution efficiency and security of the locking operation. BRIEF DESCRIPTION OF THE DRAWINGS

[0032] Figure 1 One of the flowcharts of a process communication method provided in an embodiment of the present application;

[0033] Figure 2 A schematic diagram of the shared memory area division provided in an embodiment of the present application;

[0034] Figure 3 A second flowchart of a process communication method provided in an embodiment of the present application;

[0035] Figure 4 A schematic diagram of using memory access state array variables provided in an embodiment of the present application;

[0036] Figure 5 A third flowchart of a process communication method provided in an embodiment of the present application;

[0037] Figure 6 A schematic diagram of read and write permission array variables provided in an embodiment of the present application;

[0038] Figure 7 One of the process lock array variable schematic diagrams provided in the embodiment of the present application;

[0039] Figure 8 The second schematic diagram of the process lock array variable provided in the embodiment of the present application;

[0040] Fig. 9 A schematic diagram of multiple threads accessing a shared memory provided in an embodiment of the present application;

[0041] Fig.10 A fourth flowchart of a process communication method provided in an embodiment of the present application;

[0042] Fig.11One of the structural schematic diagrams of a process communication device provided in an embodiment of the present application;

[0043] Fig.12 A second schematic diagram of the structure of a process communication device provided in an embodiment of the present application;

[0044] Fig.13 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0045] The embodiments of the present application provide a process communication method, device and electronic device. The preferred embodiments of the present application are described below in conjunction with the drawings of the specification. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present application, and are not used to limit the present application. In addition, the embodiments in the present application and the features in the embodiments can be combined with each other if there is no conflict.

[0046] The present application embodiment provides a process communication method, such as Figure 1 As shown, including:

[0047] S101, the active process obtains access qualification to the allocated shared memory according to the current access status of the allocated shared memory;

[0048] S102, determining an array member variable corresponding to the target shared memory from the process lock array variables corresponding to the allocated shared memory;

[0049] S103, determining the data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory;

[0050] Among them, the array member variables of the process lock array variable are used to store the lock status of each access process of the allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space.

[0051] In the embodiment of the present application, the user can apply for the shared memory and use the relevant technology to create the shared memory. In the use of the shared memory in the embodiment of the present application, the shared memory can be divided into two types: the core area of ​​the shared memory and the user area of ​​the shared memory. The core area of ​​the shared memory is mainly used for memory management, including the allocation and recovery of shared memory resources, the management and recovery of process locks, and the access rights management of the shared memory user area. The user area of ​​the shared memory is mainly used for users to implement data transmission or message queues. When using, the user cannot directly access the shared memory core area, but an application programming interface (API, Application Programming Interface) can be provided for the user to operate the relevant variables of the shared memory core area to avoid memory management abnormalities caused by user misoperation. During implementation, the core area and user area of ​​the shared memory can be divided by different address spaces, or by setting different access rights. There is no restriction here.

[0052] Furthermore, in the embodiment of the present application, the core area of ​​the shared memory and the user area of ​​the shared memory are both set in the user space. Usually, in order to improve stability and security, the operating system will set the kernel space and the user space, the kernel space is the core of the operating system, and the user space is the operating environment of the application. As mentioned above, the process read-write lock of the shared memory in the related art actually still uses a semaphore, and the use of the semaphore requires the application to access the kernel space, which is more complicated and time-consuming than the access process to the user space, resulting in low communication efficiency. Therefore, the embodiment of the present application sets the core area of ​​the shared memory and the user area of ​​the shared memory in the user space, which not only realizes the functional division of the shared memory area, but also avoids the access to the kernel space when locking the shared memory, thereby improving the communication efficiency between processes.

[0053] In addition, in the embodiment of the present application, a process lock array variable can be set for the allocated shared memory, and each shared memory corresponds to one of the array member variables to store the process lock of the corresponding shared memory. When the process lock array variable is implemented, it can be set to the data type of the array (for example: integer array, then the array member variable is integer, which is not limited here), and the number of array member variables can be set according to the maximum number of shared memories allowed to be allocated, so that each array member variable can store the process lock state value set by the process accessing the corresponding shared memory, that is, the lock state value of the accessing process is stored through the data bit of the array member variable. When the active process locks the target shared memory through the corresponding array member variable in the process lock array variable, it only needs to read the array member variable, determine the data bit corresponding to the active process in the array member variable, and set the value representing the lock state for the data bit to complete the locking operation. It can be seen that the process lock array variable is an atomic variable, and the operation of the active process on the process lock array variable belongs to an atomic operation (reading the array variable and assigning values ​​to the members), which further improves the execution efficiency and security of the locking operation.

[0054] During implementation, an identification number can be assigned to the target shared memory when allocating the target shared memory, and an array member variable in the process lock array variable whose array member number is the same as the identification number is assigned to the target shared memory to store the lock state value of the target shared memory locked by the process accessing the target shared memory, that is, step S102 is implemented as: from the process lock array variable, determine the array member variable with the identification number allocated to the target shared memory as the array member number; or, when allocating the shared memory, directly determine the array member variable in the current process lock array variable that does not yet correspond to the shared memory, correspond to the allocated shared memory, and record the corresponding relationship, then step S102 can be implemented as: according to the corresponding relationship between the target shared memory and the array member variables in the process lock array variable established when allocating the target shared memory, determine the array member variable corresponding to the target shared memory.

[0055] Furthermore, before performing read and write operations on the process lock array variable, it is also necessary to obtain access qualifications to access the shared memory user area. The process communication method provided in the embodiment of the present application aims to achieve efficient access of multiple processes to the shared memory user area in parallel. For the sake of management, it is necessary to first limit the access qualifications to the allocated shared memory according to the access status of the currently allocated shared memory. The current access status of the allocated shared memory can be evaluated by the number of currently accessing processes, the ratio of the number of allocated shared memory to the number of currently accessing processes, etc., which is not limited here.

[0056] Figure 2 A schematic diagram of the shared memory area division provided in the embodiment of the present application. Figure 2As shown, the shared memory is divided into a shared memory core area 201 and a shared memory user area 202. When needed, the user can apply for shared memory from the shared memory core area 201. Figure 2 In the example, N pieces of shared memory are applied for. Process 1 to process N can perform read and write operations on the shared memory of the shared memory user area 202, and perform preset atomic operations on the shared memory core area (for example, locking the shared memory to be accessed through the process lock array variable, etc.). In addition, the upper limit of the number of shared memories can be set according to actual conditions (for example, 32 blocks), which is not limited here.

[0057] Taking the development environment of Win10 X64 && Ubuntu 64, the basic API: boost::interprocess is used to implement this application as an example. Boost provides some basic interfaces for shared memory, such as shared memory creation, shared memory variable creation, shared memory deletion, etc. In addition, boost also provides shared memory atomic operations, which can apply for an atomic variable in shared memory. The atomic variable can be safely shared and operated in multiple processes without the need for additional process locks or semaphores, which can effectively reduce data competition and deadlock problems in multiple processes, and can further improve the function execution speed.

[0058] In another embodiment provided in the present application, Figure 3 As shown, the above step S101 "the active process obtains the access qualification to the allocated shared memory according to the current access status of the allocated shared memory" can be implemented as follows:

[0059] S301, traverse each array member of the memory access status array variable in sequence;

[0060] S302, determine whether there is any array member corresponding to the stored numerical representation corresponding to the access qualification is not occupied; if so, go to step S303; if not, go to step S301;

[0061] S303, when it is determined that the value stored corresponding to any array member indicates that the corresponding access qualification is not occupied, modify the value to indicate that the corresponding access qualification is occupied;

[0062] S304, determine whether the modification is successful; if the modification is successful, proceed to step S305; if the modification is unsuccessful, proceed to step S306;

[0063] S305. If the modification is successful, determine whether the active process obtains access qualifications to the allocated shared memory; and this process ends.

[0064] S306: If the modification is unsuccessful, continue to traverse the array members of the memory access status array variable.

[0065] Among them, the memory access status array variable is used to store the allocation of access qualifications to the allocated shared memory; the memory access status array variable is an atomic variable and is set in the shared memory core area; the number of array members of the memory access status array variable represents the maximum number of processes allowed to access the allocated shared memory in parallel.

[0066] In the embodiment of the present application, the shared memory core area uses atomic variables to manage the access of each process to the allocated shared memory. It mainly includes the process lock array variable for managing the process lock, the memory access status array variable for managing the access qualification, and the read-write permission array variable for managing the write permission to be introduced later.

[0067] The memory access status array variable is mainly used to store the allocation of access qualifications to the allocated shared memory. Before accessing any shared memory, the active process needs to first obtain the access qualifications to the allocated shared memory. During implementation, each array member of the memory access status array variable can be traversed in sequence. If there is a corresponding stored value (for example: value 0) of any array member that represents that the corresponding access qualification is not occupied, the value can be modified to a value (for example: value 1) that represents that the corresponding access qualification is occupied. Since there may be multiple active processes that simultaneously seize the access qualification, and the operation on the memory access status array variable is an atomic operation, there may be a situation where the value modification is unsuccessful. In the case of unsuccessful modification, the active process needs to continue to traverse the array members of the memory access status array variable. If the access qualification is still not obtained after traversing the remaining array members, it can also return to step S301 to loop through the array members of the memory access status array variable until an array member representing that the corresponding access qualification is not occupied is found, and its value is successfully modified. The above steps S302 and S303 are atomic operations.

[0068] The number of array members of the memory access status array variable can represent the maximum number of processes that are allowed to access the allocated shared memory in parallel. A process that obtains access qualifications can access the specified shared memory or all the allocated shared memory, which can be set according to actual needs. In order to facilitate management and achieve efficient parallel access, the number of processes that access the allocated shared memory in parallel can be limited by setting the number of array members of the memory access status array variable.

[0069] In another embodiment provided by the present application, after the above step S305 is executed, that is, after the active process obtains the access qualification to the allocated shared memory, the following steps may also be included:

[0070] The numerical value is modified to represent the serial number of the array member whose corresponding access qualification is occupied in the memory access status array variable, and is determined as the identification number of the active process.

[0071] In the embodiment of the present application, in order to facilitate the management of processes that obtain shared memory access qualifications, the serial number of the corresponding array member in the memory access status array variable when the active process obtains the access qualification can be determined as the identification number of the active process. The execution subject of this step can be the active process or the management process of the shared memory core area, which is not limited here.

[0072] In another embodiment provided by the present application, after the above step S305 is executed, that is, after the active process obtains the access qualification to the allocated shared memory, the following steps may also be included:

[0073] The values ​​stored in the successfully modified members are incremented at preset time intervals.

[0074] In the embodiment of the present application, the memory access status array variable can also represent the current activity status of accessing the shared memory. After the active process obtains the access qualification and modifies the value of the corresponding array member to a value representing that the corresponding access qualification is occupied (for example, value 1), in order to represent the subsequent activity status of the active process, the active process can increment the value stored in the successfully modified member at a preset time interval. For example, the number of units (such as value 1) is incremented every 1s, so by checking the increment of the value stored in each array member, the activity status of the corresponding active process can be known.

[0075] Figure 4 A schematic diagram of the use of memory access state array variables provided in the embodiment of the present application. Figure 4As shown in the figure, it is assumed that the type of the memory access status array variable is set to an atomic variable (atomic) 32-bit integer (int32) array, including 32 array members (array member serial numbers from 0 to 31). Process A (process number (PID, Process Identification) is 1456) obtains access qualifications through the array member with serial number 0, and serial number 0 is assigned to process A as the identification number. Process B (process number PID is 1578) obtains access qualifications through the array member with serial number 1, and serial number 1 is assigned to process B as the identification number. Similarly, process C (process number PID is 1264) obtains access qualifications through the array member with serial number 2, and serial number 2 is assigned to process C as the identification number. Taking the value 0 representing unoccupied access qualifications as an example, the array member values ​​0, 0, and 0 corresponding to process A, process B, and process C respectively represent that identification numbers are being assigned to the processes, and the array member values ​​23, 43, and 87 represent the active status of the processes using shared memory, respectively. The 6th to 31st array members 0 represent array members that have not yet been assigned. Figure 4 The solid arrow in the middle represents the allocation of currently available identification number resources to the process, and the dotted arrow represents the periodic increment of the storage value of the array member.

[0076] In another embodiment provided by the present application, the active process includes a writing process; after the writing process completes the above step S305 and obtains the access qualification, the following steps may be further performed:

[0077] Obtain permission to write to the target shared memory;

[0078] like Figure 5 As shown, the step of "obtaining permission to perform write operations on the target shared memory" can be implemented as follows:

[0079] S501, obtaining the first process number stored in the corresponding member of the target shared memory from the read-write permission array variable;

[0080] S502, determine whether the first process number is the second process number of the writing process; if so, proceed to step S503; if not, proceed to step S504;

[0081] S503: Determine that the permission to perform the write operation on the target shared memory is obtained; this process ends;

[0082] S504, replacing the first process number with the second process number;

[0083] S505, determine whether the replacement is successful; if successful, proceed to step S503; if unsuccessful, proceed to step S501.

[0084] Among them, the read-write permission array variable is used to store the process number of the process that has write permission to the corresponding shared memory; the read-write permission array variable is an atomic variable and is set in the shared memory core area; the number of array members of the read-write permission array variable represents the maximum amount of shared memory that can be applied for.

[0085] In an embodiment of the present application, after obtaining access qualifications, the writing process also needs to obtain permission for write operations before it can write lock through the process lock array variable. Therefore, in order to manage the read and write permissions of the shared memory, the embodiment of the present application sets a read and write permission array variable. The read and write permission array variable represents the process that currently has write permission to the shared memory, that is, the array members of the read and write permission array variable can correspond to a piece of shared memory respectively, and store the process number that has write operation permission to the corresponding shared memory. In actual use, there may be a process that has write permission to multiple shared memories (that is, multiple array members store the process number of the same process), while some processes only have read permission for multiple processes.

[0086] Figure 6 A schematic diagram of a read and write permission array variable provided in an embodiment of the present application, such as Figure 6 As shown in the figure, taking the maximum number of shared memories allowed to be applied for as 32 as an example, array numbers 0-31 identify 32 blocks of memory respectively. Process A has write permission for the 0th and 1st blocks of shared memory, and the corresponding array members (array member numbers are 0 and 1 respectively) store the process ID PID (1345) of process A; process B has write permission for the 2nd block of shared memory, and the corresponding array member (array member number is 2) stores the PID (576) of process B; process C has write permission for the 3rd block of shared memory, and the corresponding array member (array member number is 3) stores the PID (345) of process C; the array member storing the value 0 indicates that no process has obtained write permission on the corresponding shared memory.

[0087] Furthermore, when a process needs to obtain write permission for a piece of shared memory, it is necessary to modify the process number stored in the array member corresponding to the read-write permission array variable of the shared memory to its own process number, that is, execute the above steps S501~S505. The read-write permission array variable is an atomic variable. If there are multiple processes that obtain write permission, then according to the atomic operation characteristics, only one process can modify the previously stored process number to its current process number. That is, the above steps S501, S502 and S504 are atomic operations. In the case of unsuccessful modification, it is necessary to repeat the step of obtaining the first process number until the first process number is successfully replaced.

[0088] In implementation, in order to facilitate the implementation, an identification number can be assigned to the shared memory when allocating the shared memory, and the array member with the same array number as the identification number in the read-write permission array variable stores the process number with write permission corresponding to the shared memory. In addition, the number of array members of the read-write permission array variable represents the maximum number of shared memories that can be applied for (for example: 32 blocks).

[0089] In another embodiment provided by the present application, the active process includes a writing process; then before locking the target shared memory, the following steps may also be performed:

[0090] Step 1: read the current value of the array member variable corresponding to the target shared memory in the process lock array variable; and

[0091] Step 2: Determine, based on the current value, that no process is reading the target shared memory;

[0092] Among them, the number of members of the process lock array variable represents the maximum amount of shared memory allowed to be allocated; the maximum number of processes supported by each shared memory for parallel access to the shared memory is determined according to the maximum number of bits supported by the operating system where the active process runs and the number of data bits required to represent multiple lock states.

[0093] In the embodiment of the present application, for the writing process, before setting the write lock, it is necessary to ensure that there is no process currently reading the target shared memory. If other processes are reading the target shared memory, and the writing process modifies the content of the target shared memory, it will cause a reading error in the reading process. Since a process lock array variable is allocated for the allocated shared memory, and the array member variable of the process lock array variable is used to characterize the lock state of the corresponding shared memory, that is, a value representing the corresponding lock state of the access made by the process is stored, for example: read lock, write lock, and different lock states have different corresponding values. Therefore, the lock state of each process accessing the target shared memory can be determined by reading the current value of the corresponding array member variable of the process lock array variable, that is, by determining whether there is no process reading the target shared memory through the current value.

[0094] In addition, the number of members of the process lock array variable represents the maximum number of shared memories that can be allocated, and the maximum number of processes that each shared memory supports for parallel access to the shared memory can be determined based on the maximum number of bits supported by the operating system where the active process runs, and the number of data bits required to represent multiple lock states. For example, if the process lock is a read-write lock and requires three lock states, the lock state requires two data bits (bits), assuming that 0 represents unlocked (corresponding to binary 00), 1 represents read lock (corresponding to binary 01), and 2 represents write lock (corresponding to binary 10). According to the maximum number of bits supported by the operating system, the largest type of atomic variables is uint64_t, and one atomic variable corresponds to one shared memory, which can support up to 32 processes to perform read and write access operations on the same shared memory.

[0095] Figure 7 A schematic diagram of a process lock array variable provided in an embodiment of the present application. Figure 7 As shown, taking the maximum number of bits supported by the operating system as 64 bits and the lock status representation requiring 2 bits as an example, a maximum of 32 processes can access in parallel. Figure 7 In the illustrated implementation, taking the maximum number of shared memory applications as 32 blocks as an example, the process lock array variable includes 32 array member variables of uint64_t atomic variable type (array numbers 0-31). Each array member variable is 64 bits, and every two bits store a lock status value. The process lock status of 32 processes can be stored for the corresponding shared memory.

[0096] In another embodiment provided by the present application, the following steps may be used to determine whether there is a process currently reading the target shared memory:

[0097] Step 1: Perform a bitwise AND operation on the current value of the above array member variable and the preset mask; the preset mask is obtained by setting the value of each data bit in the array member variable of the process lock array variable to the value of the read lock;

[0098] Step 2: When the result of the AND operation is zero, it is determined that no process is reading the target shared memory.

[0099] In an embodiment of the present application, in order to facilitate determining whether a process is reading the target shared memory, a preset mask can be set. The preset mask can be set based on the array member variable of the process lock array variable, and the value of each data bit is set to the value of the read lock. Taking the array member variable of the process lock array variable as 64 bits and the read lock value as 1 as an example (corresponding to binary 01), the mask can be set to: 0x5555555555555555 (16 decimal 5s correspond to 32 binary 01s), that is, the process locks of the 32 processes are all set to read locks. In this way, when the AND result is 0, it means that there is no process that has set a read lock on the target shared memory; and when the AND result is not 0, it means that there is a process reading the target shared memory, and it is necessary to wait for the reading to be completed before setting the write lock.

[0100] In another embodiment provided by the present application, the above step S102 "determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory" can be implemented as follows:

[0101] From the process lock array variable, determine the array member whose array member number is the identification number allocated for the target shared memory; or

[0102] Determine the array member variable corresponding to the target shared memory according to the corresponding relationship between the target shared memory and the array member variable in the process lock array variable established when allocating the target shared memory;

[0103] The active process includes a writing process; accordingly, the above step S103 "determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory" can be implemented as follows:

[0104] Step 1, according to the identification number assigned to the active process and the number of bits representing the lock state data bits, determine the offset of the data bit corresponding to the active process in the array member variable;

[0105] Step 2: determine the data bit corresponding to the active process according to the offset, and set the value stored in the data bit to a value indicating that the lock state is a write lock, so as to write lock the target shared memory and write the shared data into the target shared memory.

[0106] Applying for an active process includes a reading process; accordingly, the above step S103 "determining a data bit corresponding to the active process from the array member variable, and setting the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, to lock the target shared memory, and access the target shared memory" can be implemented as follows:

[0107] Step 1, according to the identification number assigned to the active process and the number of bits representing the lock state data bits, determine the offset of the data bit corresponding to the active process in the array member variable;

[0108] Step 2: determine the data bit corresponding to the active process according to the offset, and set the value stored in the data bit to a value representing that the lock state is a read lock, so as to read lock the target shared memory and read the target shared memory.

[0109] In an embodiment of the present application, in order to facilitate searching for the array member variables corresponding to the target shared memory from the process lock array variable, an identification number can be assigned to the target shared memory when allocating the target shared memory, and the array member variables whose array member numbers in the process lock array variable are the same as the identification number are allocated to the target shared memory to store the lock state value of the target shared memory locked by the process accessing the target shared memory. In this way, the positioning of the array member variables in the process lock array variable is achieved according to the identification number of the target shared memory. Alternatively, when allocating the target shared memory, it is determined that the array member variables in the current process lock array variable do not yet correspond to the shared memory, and correspond to the allocated shared memory, and the corresponding relationship is recorded so that the array member variables corresponding to the shared memory can be searched for later according to the corresponding relationship.

[0110] Furthermore, it is necessary to determine the data bit corresponding to the active process from the array member variable. In order to facilitate the search for the data bit corresponding to the active process from the array member variable, the offset of the data bit corresponding to the active process in the array member variable can be determined based on the identification number of the active process (assigned to the active process after the active process obtains the access qualification). Since the array member variable stores the lock status of multiple processes accessing the target shared memory, and the number of data bits used by each process to store the lock status is fixed, the number of data bits can be used as a unit, the identification number of the process as the number of offsets, and the offset number can be offset according to the number of unit bits each time, so as to obtain the position of the data bit corresponding to the process in the array member variable. In this way, the corresponding data bit in the array member variable corresponding to the target shared memory can be quickly determined according to the identification number of the process, and the setting of the write lock is completed by storing the value representing the upper write lock in the data bit, and the setting of the read lock is completed by storing the value representing the upper read lock.

[0111] Furthermore, due to the limitation of the maximum number of bits supported by the system (for example, 64 bits) and the number of bits required to represent the lock state (for example, 2 bits), the number of processes that can access the same shared memory in parallel is basically deterministic (for example, 64÷2=32). Then for the memory access state array variable, the number of bits of its array member variables limits the maximum number of processes that are allowed to access the allocated shared memory in parallel, and this maximum number is basically consistent with the number of processes that can access the shared memory in parallel here (for example, 32).

[0112] Figure 8 A schematic diagram of a process lock array variable provided in an embodiment of the present application. Figure 8 As shown in the figure, it is the array member variable with the array number 0 in the process lock array variable corresponding to the first block of shared memory (identification number 0). Continuing with the maximum number of bits supported by the operating system being 64 bits, and the lock status requiring 2 data bits (bits) as an example, for process A with identification number 0, its offset in the array member variable is the data bit at the position offset 0 times in units of 2 bits, that is, bit0 and bit1. If process A needs to perform a write operation on the first block of shared memory, it can store the corresponding lock status value at bit0 and bit1; for process B with identification number 1, its offset in the array member variable is the data bit at the position offset 0 times in units of 2 bits, that is, bit0 and bit1. The offset in the group member variable is the data bit at the position offset by 2 bits, that is, bit2 and bit3. If process B needs to perform a write operation on the first block of shared memory, it can store the corresponding lock status value at bit2 and bit3. For process C with identification number 2, its offset in the array member variable is the data bit at the position offset by 2 bits, that is, bit4 and bit5. If process C needs to perform a write operation on the first block of shared memory, it can store the corresponding lock status value at bit4 and bit5. The same applies to the following. I will not go into details here.

[0113] In another embodiment provided by the present application, the active process includes a reading process; the reading process includes a plurality of threads;

[0114] Then the above step S102 "determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory" can be implemented as the following step 1:

[0115] Step 1: When the first thread among multiple threads needs to perform a read operation, determine the array member variable corresponding to the target shared memory from the process lock array variable;

[0116] The above step S103 "determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to the value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory" can be implemented as the following step 2;

[0117] Step 2: determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value indicating that the lock state is a read lock, so as to set a read lock for the target shared memory and read the target shared memory;

[0118] The method may further comprise the steps of:

[0119] Step 3: determine the count array member for reading lock counts for the read process in the read lock count array variable, and record the unit value in the count array member;

[0120] Step 4: when a non-first thread among multiple threads needs to perform a read operation, the number of threads that read the target shared memory is accumulated according to the unit value in the counting array member;

[0121] Step 5: after any thread among the multiple threads completes reading, the count of the member of the counting array is reduced by a unit value;

[0122] Step 6: When the count of the counting array member is reduced to zero, the value stored in the corresponding data bit in the process lock array variable is set to a value representing the lock state as unlocked;

[0123] The read lock count array variable represents the number of threads of each process that has access qualification to the corresponding shared memory and currently accesses the corresponding shared memory; the number of members of the read lock count array variable represents the maximum amount of shared memory allowed to be allocated.

[0124] In an embodiment of the present application, since a process may include multiple threads, the process can start a thread to access the shared memory after obtaining the access qualification to the shared memory, and multiple threads may access the same shared memory. This requires multiple threads to set a read lock on the shared memory, but the multiple threads use the access qualification of the same process, and the lock status value representing the read lock cannot reflect the number of read locks. If a thread completes data reading and clears the lock status value of the read lock, other threads that are still reading data cannot let the outside world know that there are still threads reading data. Once a write process writes data, it will cause errors in reading data by other threads.

[0125] In order to solve this problem, an embodiment of the present application provides a read lock count array variable, which can be set for the allocated shared memory. The read lock count array variable can represent the number of threads that currently access the shared memory of each process that has access qualifications to each shared memory. Then, the number of members of the read lock count array variable can represent the maximum number of shared memories allowed to be allocated (for example: 32 in the previous example), and each count array member can count the number of threads accessing the corresponding shared memory.

[0126] During implementation, when the first thread of the process needs to perform a read operation, the read lock value can be written to the corresponding data bit of the corresponding array member variable of the process lock array variable. When subsequent threads other than the first thread need to read the same shared memory, there is no need to assign a value to the data bit again. Instead, a cumulative count is performed on the corresponding count array member of the read lock count array variable. Similarly, when any thread completes the read operation, a unit value (for example: 1) can be subtracted from the corresponding count array member of the read lock count array variable. When the count of the corresponding count array member of the read lock count array variable is reduced to zero, it means that all threads have completed the reading, and the value stored in the corresponding data bit in the process lock array variable can be set to a value representing the lock state as unlocked.

[0127] Fig. 9 A schematic diagram of multiple threads accessing shared memory provided in an embodiment of the present application. Fig. 9 As shown in the figure, it is an array member variable with an array number of 0 in the process lock array variable corresponding to a shared memory (identification number 0). Taking the maximum number of bits supported by the operating system as 64 bits and the lock state requiring 2 bits as an example, multiple threads (threads A to C) of process 1 with identification number 0 are reading the shared memory. The corresponding data bits (i.e., bit0 and bit1) of the corresponding array member variable of the process lock array variable (i.e., the array member with sequence number 0) store the read lock (taking the read lock value as 1 as an example), and the corresponding count array member (i.e., the array member with sequence number 0) in the read lock count array variable counts the threads that are performing the read operation. For process 1, there are three threads performing the read operation, so the count array member value is 3. In addition, thread D needs to perform a write operation, which can only be performed after waiting for the three threads AC to complete the read operation.

[0128] In another embodiment provided by the present application, Fig.10 As shown, it also includes steps to clean up inactive processes:

[0129] S1001. Traverse each member of the memory access status array variable in sequence, and perform the following operations in each traversal:

[0130] S1002, determine whether the value stored in each member is the same as that in the last traversal; if the value is the same, proceed to step S1003; if the value is different, proceed to step S1001;

[0131] S1003, accumulating the number of times the corresponding member is judged the same; and determining whether the accumulated number reaches the preset number; if so, proceeding to step S1004; if not, proceeding to step S1001;

[0132] S1004, modifying the value stored in the corresponding member to indicate that the corresponding access qualification is not occupied;

[0133] S1005. If the modification is successful, determine the target process represented by the corresponding member storage value; and set the lock state corresponding to the target process in the process lock array variable to unlocked;

[0134] S1006. When the target process is a writing process, the process number corresponding to the target process stored in the read-write permission array variable is cleared.

[0135] In the embodiment of the present application, under normal circumstances, the process should release the corresponding resources by itself after completing the read and write operations. For example, the process should clear the write lock on the process after completing the write operation. In order to avoid the inability to release the corresponding resources due to abnormal termination of the process, the embodiment of the present application provides a method for cleaning up inactive processes. As mentioned above, after the process obtains the access qualification, it will increment the value stored in the successfully modified member at a preset time interval to indicate that the process is still active. The activity state of the process can be determined by detecting this increment count. In other words, if the increment count is normal, it means that the process is active, and if the increment count of the process stops, it can be said that the process is inactive or abnormally terminated. At this time, it is necessary to clean up the resources occupied by the inactive process through this process. During implementation, after the process obtains access qualifications, a thread can be started in the background (the thread can be managed by the shared memory core area) to traverse each member of the memory access status array variable at a preset period (for example, 100ms) to detect whether there is a process that performs the self-increment operation for a preset number of times (for example, 20 times). If so, the values ​​corresponding to the process in the memory access status array variable, the process lock array variable, and the read-write permission array variable (when the target process is a write process) are cleared.

[0136] In addition, if the modification of the value stored in the corresponding member to indicate that the corresponding access qualification is not occupied fails, there may already be a process releasing resources, and the process can return to step S1001. Steps S1002, S1004, and S1005 are all atomic operations.

[0137] In another embodiment provided by the present application, the active process includes a writing process; the writing process writes the shared data into the target shared memory in the following manner:

[0138] Step 1: Determine the write offset of the shared data in the target shared memory;

[0139] Step 2: Write the shared data into the target shared memory according to the write offset, and update the current write offset;

[0140] Step 3: Assign a value to the write index variable according to the number of units of the shared data to be written;

[0141] Wherein, the write offset, the write data size, and the write index are stored in the shared memory user area;

[0142] The active process includes the reading process; the reading process reads the target shared memory in the following way:

[0143] Step 1: Get the value of the current write index variable and the value of the read index variable;

[0144] Step 2: When the value of the write index variable is inconsistent with the value of the read index variable and the read index variable is valid, read the shared data according to the write offset and the unit data amount represented by the write index variable;

[0145] Step 3: Update the value of the read index variable.

[0146] In the embodiment of the present application, when using user shared memory, data can be sent and received through a lock-free message queue (here, lock-free means not using the lock provided by the relevant technology). In one implementation of multiple processes accessing shared memory, there is a writing process and multiple reading processes; that is, a writing process synchronizes data and fills data into the shared memory, while other reading processes read data from the shared memory.

[0147] The members of the lock-free message queue mainly include: write offset, unit data size (for example, in frames, current frame size), unit data volume (number of frames), data type (frame type), write index variable, read index variable, etc. It should be noted that the read index variable can be set as a private variable of the read process, while the write index variable requires all read processes to refer to it for data reading, so it can be set as a global variable. After the write process writes data to the shared memory, the write index variable can be modified to a value corresponding to the written unit data volume (such as the number of frames). Since the write index variable is a global variable, each read process can obtain the updated value of the write index variable and compare the value of the write index variable with the value of its own read index variable. Since the write offset and the write data size are also stored in the shared memory user area, when the value of the write index variable is inconsistent with the value of the read index variable, and the read index variable is valid, the shared data can be read and the read index value can be updated to indicate that the acquisition of the new shared data has been completed. In addition, the writing of the write index variable by the write process and the acquisition of the write index variable by the read process in the above steps are both atomic operations.

[0148] It should be noted that if the read index variable value is invalid, the write index variable value can be assigned to the read index variable. In addition, how the read process knows the shared memory where the write process writes data can be notified when calling the read process, which will not be repeated here.

[0149] In another embodiment provided by the present application, in the use scenario of instruments and meters, the active process includes a control end process as a writing process and multiple monitoring end processes as reading processes. That is, one control end process performs data synchronization and fills data into a shared memory, while other monitoring end processes read data from the shared memory.

[0150] Based on the same application concept, the embodiments of the present application also provide a process communication device. Since the principles of the problems solved by these devices are similar to those of the aforementioned process communication methods, the implementation of the device can refer to the implementation of the aforementioned methods, and the repeated parts will not be repeated.

[0151] The present application embodiment provides a process communication device, such as Fig.11 As shown, it includes the following modules:

[0152] An access qualification acquisition module 1101 is used for an active process to acquire access qualifications to the allocated shared memory according to the current access status of the allocated shared memory;

[0153] The locking module 1102 is used to determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory; determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to the value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory;

[0154] Among them, the array member variables of the process lock array variable are respectively used to store the lock status of the access process of each allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space.

[0155] In another embodiment provided in the present application, an access qualification acquisition module 1101 is used to traverse each array member of the memory access status array variable in sequence; when it is determined that the numerical value stored in any array member represents that the corresponding access qualification is not occupied, the numerical value is modified to represent that the corresponding access qualification is occupied; when the modification is successful, it is determined that the active process obtains the access qualification to the allocated shared memory; when the modification is unsuccessful, continue to traverse the array members of the memory access status array variable; wherein, the memory access status array variable is used to store the allocation of access qualifications to the allocated shared memory; the memory access status array variable is an atomic variable and is set in the shared memory core area; the number of array members of the memory access status array variable represents the maximum number of processes allowed to access the allocated shared memory in parallel.

[0156] In another embodiment provided in the present application, the access qualification acquisition module 1101 is also used to modify the numerical value to represent the serial number of the array member whose corresponding access qualification is occupied in the memory access status array variable after the active process obtains the access qualification to the allocated shared memory, and determine it as the identification number of the active process.

[0157] In another embodiment provided in the present application, Fig.12 As shown, the active process includes a write process; the device further includes: a write permission acquisition module 1103;

[0158] A write permission acquisition module 1103 is used for acquiring permission to perform a write operation on the target shared memory after the write process acquires the access qualification;

[0159] A write permission acquisition module 1103 is used to obtain the first process number stored in the corresponding member of the target shared memory from the read-write permission array variable; determine whether the first process number is the second process number of the writing process; if so, determine that the permission to perform write operations on the target shared memory is obtained; if not, replace the first process number with the second process number; if the replacement of the first process number with the second process number is unsuccessful, return to the step of obtaining the first process number stored in the corresponding member of the target shared memory; wherein the read-write permission array variable is used to store the process number of the process that has write permission to the corresponding shared memory; the read-write permission array variable is an atomic variable and is set in the shared memory core area; the number of array members of the read-write permission array variable represents the maximum amount of shared memory that can be applied for.

[0160] In another embodiment provided by the present application, the active process includes a writing process; the locking module 1102 is also used to read the current value of the array member variable corresponding to the target shared memory in the process lock array variable before locking the target shared memory; and determine based on the current value that there is no process reading the target shared memory; wherein the number of members of the process lock array variable represents the maximum number of shared memories allowed to be allocated; the maximum number of processes supported by each shared memory to access the shared memory in parallel is determined based on the maximum number of bits supported by the operating system where the active process runs, and the number of data bits required to represent multiple lock states.

[0161] In another embodiment provided by the present application, the locking module 1102 is used to determine whether there is a process currently reading the target shared memory in the following manner: performing a bitwise AND operation on the current value of the array member variable and a preset mask; the preset mask is obtained by setting the value of each data bit in the array member variable of the process lock array variable to the value of the read lock; when the AND result is zero, it is determined that there is no process reading the target shared memory.

[0162] In another embodiment provided by the present application, the locking module 1102 is used to determine, from the process lock array variable, an array member variable whose identification number allocated to the target shared memory is used as an array member number;

[0163] The active process includes a write process; the locking module 1102 is used to determine the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; determine the data bit corresponding to the active process according to the offset, and set the value stored in the data bit to the value representing that the lock state is a write lock, so as to write lock the target shared memory and write the shared data into the target shared memory; and / or

[0164] The active process includes a read process; the locking module 1102 is used to determine the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; determine the data bit corresponding to the active process according to the offset, and set the value stored in the data bit to a value representing that the lock state is a read lock, so as to read lock the target shared memory and read the target shared memory.

[0165] In another embodiment provided by the present application, the active process includes a read process; the read process includes multiple threads; the locking module 1102 is used to determine the array member variable corresponding to the target shared memory from the process lock array variable when the first thread among the multiple threads needs to perform a read operation; and determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value representing that the lock state is a read lock, so as to read lock the target shared memory and read the target shared memory;

[0166] The locking module 1102 is also used to determine the counting array member for reading the lock count for the reading process in the read lock count array variable, and record the unit value in the counting array member; when a thread other than the first thread among the multiple threads needs to perform a read operation, the number of threads that read the target shared memory is accumulated in the counting array member according to the unit value; after any thread among the multiple threads completes the reading, the unit value is subtracted from the count of the counting array member; when the count of the counting array member is reduced to zero, the value stored in the corresponding data bit in the process lock array variable is set to a value representing the lock state as unlocked; wherein, the read lock count array variable represents the number of threads currently accessing the corresponding shared memory by each process that has access qualifications to the corresponding shared memory; the number of members of the read lock count array variable represents the maximum number of shared memories allowed to be allocated.

[0167] In another embodiment provided by the present application, the access qualification acquisition module 1101 is further used to increment the value stored in the successfully modified member according to a preset time interval; and

[0168] In another embodiment provided by the present application, inactive processes are cleaned up in the following manner: traverse each member of the memory access status array variable in turn, and perform the following operations in each traversal: determine whether the value stored in each member is the same as that in the previous traversal; if the same, accumulate the number of times the corresponding member is judged to be the same; determine whether the accumulated number reaches a preset number; if so, modify the value stored in the corresponding member to represent that the corresponding access qualification is not occupied; if the modification is successful, determine the target process represented by the value stored in the corresponding member; set the corresponding lock state of the target process in the process lock array variable to unlocked; when the target process shown is a write process, clear the process number stored in the read and write permission array variable of the target process.

[0169] In another embodiment provided by the present application, it further includes: a writing module 1104 and a reading module 1105; the writing module 1104 is used to write the shared data into the target shared memory in the following manner: determine the write offset of the shared data in the target shared memory; write the shared data into the target shared memory according to the write offset, and update the current write offset; assign a value to a write index variable according to the number of units of the written shared data; wherein the write offset, the write data size, and the write index are stored in the shared memory user area;

[0170] The reading module 1105 is used to read the target shared memory in the following manner: obtain the value of the current write index variable and the value of the read index variable; when the value of the write index variable and the value of the read index variable are inconsistent and the read index variable is valid, read the shared data according to the write offset and the unit data amount represented by the write index variable; and update the value of the read index variable.

[0171] In another embodiment provided by the present application, the active process includes a control end process as a write process and a plurality of monitoring end processes as read processes.

[0172] Based on the same technical concept, the embodiment of the present application also provides an electronic device. Fig.13As shown, it is a schematic diagram of the structure of the electronic device 1300 provided in the embodiment of the present application, including a processor 1301, a memory 1302, and a bus 1303. Among them, the memory 1302 is used to store execution instructions, including a memory 13021 and an external memory 13022; the memory 13021 here is also called an internal memory, which is used to temporarily store the operation data in the processor 1301, and the data exchanged with the external memory 13022 such as a hard disk. The processor 1301 exchanges data with the external memory 13022 through the memory 13021. When the electronic device 1300 is running, the processor 1301 communicates with the memory 1302 through the bus 1303, so that the processor 1301 executes the following instructions:

[0173] The active process obtains access qualification to the allocated shared memory according to the current access status of the allocated shared memory;

[0174] Determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory;

[0175] Determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory;

[0176] Among them, the array member variables of the process lock array variable are respectively used to store the lock status of the access process of each allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space.

[0177] The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, the steps of the process communication method described in the above method embodiment are executed. The storage medium can be a volatile or non-volatile computer-readable storage medium.

[0178] Through the description of the above implementation methods, those skilled in the art can clearly understand that the embodiments of the present application can be implemented by hardware, or by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the embodiments of the present application can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (which can be a CD-ROM, a USB flash drive, a mobile hard disk, etc.), including a number of instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0179] Those skilled in the art will appreciate that the accompanying drawings are merely schematic diagrams of a preferred embodiment, and the modules or processes in the accompanying drawings are not necessarily required for implementing the present application.

[0180] Those skilled in the art can understand that the modules in the device in the embodiment can be distributed in the device in the embodiment according to the description of the embodiment, or can be changed accordingly and located in one or more devices different from the present embodiment. The modules in the above embodiment can be combined into one module, or can be further divided into multiple sub-modules.

[0181] The serial numbers of the above-mentioned embodiments of the present application are for description only and do not represent the advantages or disadvantages of the embodiments.

[0182] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.

Claims

1. A process communication method, characterized in that: include: The active process obtains access qualification to the allocated shared memory according to the current access status of the allocated shared memory; Determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory; Determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory; The array member variables of the process lock array variable are respectively used to store the lock status of the access process of each allocated shared memory; the process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and is set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space; The step of determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory comprises: Determine, from the process lock array variables, an array member variable having an identification number allocated for the target shared memory as an array member number; or The array member variable corresponding to the target shared memory is determined according to the corresponding relationship between the target shared memory and the array member variable in the process lock array variable established when the target shared memory is allocated.

2. The method according to claim 1, characterized in that The active process obtains access qualifications to the allocated shared memory according to the current access status of the allocated shared memory, including: Traverse each array member of the memory access status array variable in turn; When it is determined that the value stored in the corresponding member of any array represents that the corresponding access qualification is not occupied, the value is modified to represent that the corresponding access qualification is occupied; If the modification is successful, determining that the active process obtains access qualifications to the allocated shared memory; If the modification is unsuccessful, continue to traverse the array members of the memory access status array variable; Among them, the memory access status array variable is used to store the allocation of access qualifications to the allocated shared memory; the memory access status array variable is an atomic variable and is set in the shared memory core area; the number of array members of the memory access status array variable represents the maximum number of processes allowed to access the allocated shared memory in parallel.

3. The method according to claim 2, characterized in that After the active process obtains the access qualification to the allocated shared memory, the method further includes: The numerical value is modified to represent the serial number of the array member whose corresponding access qualification is occupied in the memory access status array variable, and is determined as the identification number of the active process.

4. The method according to claim 1, characterized in that The active process includes a write process; After the writing process obtains the access qualification, the process further includes: Obtaining permission to perform write operations on the target shared memory, including: Obtain the first process number of the member storage corresponding to the target shared memory from the read-write permission array variable; Determine whether the first process number is the second process number of the writing process; if so, determine whether the permission to perform the write operation on the target shared memory is obtained; if not, replace the first process number with the second process number; if replacing the first process number with the second process number is unsuccessful, return to the step of obtaining the first process number stored in the member corresponding to the target shared memory; Among them, the read-write permission array variable is used to store the process number of the process that has write permission to the corresponding shared memory; the read-write permission array variable is an atomic variable and is set in the shared memory core area; the number of array members of the read-write permission array variable represents the maximum amount of shared memory that can be applied for.

5. The method according to claim 1, characterized in that The active process includes a writing process; and before locking the target shared memory, it also includes: Reading the current value of the array member variable corresponding to the target shared memory in the process lock array variable; and Determining, according to the current value, that no process is reading the target shared memory; Among them, the number of members of the process lock array variable represents the maximum number of shared memories allowed to be allocated; the maximum number of processes supported by each shared memory to access the shared memory in parallel is determined according to the maximum number of bits supported by the operating system where the active process runs, and the number of data bits required to represent multiple lock states.

6. The method according to claim 5, characterized in that Determine whether there is a process currently reading the target shared memory in the following manner: Performing a bitwise AND operation on the current value of the array member variable and a preset mask; the preset mask is obtained by setting the value of each data bit in the array member variable of the process lock array variable to the value of the read lock; When the AND result is zero, it is determined that no process is reading the target shared memory.

7. The method according to claim 3, characterized in that The active process includes a write process; determining a data bit corresponding to the active process from the array member variable, and setting a value stored in the data bit to a value representing a lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory, including: Determine the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; Determine the data bit corresponding to the active process according to the offset, and set the value stored in the data bit to a value indicating that the lock state is a write lock, so as to write lock the target shared memory and write the shared data into the target shared memory; and / or The active process includes a reading process; determining a data bit corresponding to the active process from the array member variable, and setting a value stored in the data bit to a value representing a lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory, including: Determine the offset of the data bit corresponding to the active process in the array member variable according to the identification number assigned to the active process and the number of bits representing the lock state data bit; The data bit corresponding to the active process is determined according to the offset, and the value stored in the data bit is set to a value indicating that the lock state is a read lock, so as to set a read lock for the target shared memory and read the target shared memory.

8. The method according to claim 1, characterized in that The active process includes a reading process; the reading process includes multiple threads; Determining the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory includes: When a first thread among the multiple threads needs to perform a read operation, determining an array member variable corresponding to the target shared memory from the process lock array variable; Determining a data bit corresponding to the active process from the array member variable, and setting a value stored in the data bit to a value representing a lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory, including: Determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to a value indicating that the lock state is a read lock, so as to set a read lock for the target shared memory and read the target shared memory; The method further comprises: Determine a count array member in a read lock count array variable for performing read lock counting for the read process, and record a unit value in the count array member; In the case that a thread other than the first thread among the multiple threads needs to perform a read operation, the number of threads that read the target shared memory is accumulated according to the unit value in the counting array member; After any thread among the plurality of threads completes reading, the count of the member of the count array is reduced by a unit value; When the count of the counting array member is reduced to zero, the value stored in the corresponding data bit in the process lock array variable is set to a value representing the lock state as unlocked; The read lock count array variable represents the number of threads of each process having access qualification to the corresponding shared memory that currently accesses the corresponding shared memory; the number of members of the read lock count array variable represents the maximum amount of shared memory allowed to be allocated.

9. The method according to claim 2, characterized in that The method further comprises: Increment the value stored in the successfully modified member at a preset time interval; To clean up inactive processes, use the following method: Each member of the memory access status array variable is traversed in turn, and the following operations are performed in each traversal: Determine whether the value stored in each member is the same as that in the last traversal; If they are the same, the corresponding members will be judged the same number of times; Determine whether the accumulated number of times reaches the preset number; if so, modify the value stored in the corresponding member to indicate that the corresponding access qualification is not occupied; if the modification is successful, determine the target process represented by the value stored in the corresponding member; set the corresponding lock state of the target process in the process lock array variable to unlocked; if the target process shown is a write process, clear the process number stored in the read and write permission array variable of the target process.

10. The method according to claim 1, characterized in that The active process includes a writing process; the writing process writes the shared data into the target shared memory in the following manner: Determine a write offset of the shared data in the target shared memory; Writing the shared data into the target shared memory according to the write offset, and updating the current write offset; Assign values ​​to the write index variable according to the number of units of the shared data being written; Wherein, the write offset, write data size, and write index are stored in the shared memory user area; The active process includes a reading process; the reading process reads the target shared memory in the following manner: Get the value of the current write index variable and the value of the read index variable; When the value of the write index variable is inconsistent with the value of the read index variable and the read index variable is valid, the shared data is read according to the write offset and the unit data amount represented by the write index variable; and the value of the read index variable is updated.

11. The method according to claim 1, characterized in that The active processes include a control end process as a writing process and a plurality of monitoring end processes as reading processes.

12. A process communication device, characterized in that: include: An access qualification acquisition module is used for an active process to acquire access qualifications to the allocated shared memory according to the current access status of the allocated shared memory; The locking module is used to determine the array member variable corresponding to the target shared memory from the process lock array variable corresponding to the allocated shared memory; determine the data bit corresponding to the active process from the array member variable, and set the value stored in the data bit to the value representing the lock state corresponding to the access made by the active process, so as to lock the target shared memory and access the target shared memory; Wherein, the array member variables of the process lock array variable are respectively used to store the lock status of each access process that has been allocated shared memory; The process lock array variable is an atomic variable and is set in the shared memory core area; the allocated shared memory is pre-allocated by the shared memory core area and is set in the shared memory user area; the shared memory core area and the shared memory user area are both set in the user space; The locking module is used to determine, from the process lock array variable, an array member variable whose array member number is the identification number allocated for the target shared memory; or to determine the array member variable corresponding to the target shared memory based on the corresponding relationship between the target shared memory and the array member variables in the process lock array variable established when the target shared memory is allocated.

13. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate via the bus, and when the machine-readable instructions are executed by the processor, the steps of the process communication method as described in any one of claims 1 to 11 are performed.

14. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the process communication method according to any one of claims 1 to 11 are executed.

Citation Information

Patent Citations

  • Method and device for accessing shared memory by process in multi-system environment

    CN115756876A

  • Method for realing sharing internal stored data base and internal stored data base system

    CN1740978A