Method and device for realizing service process recovery by accessing shared memory

By dividing the system into a multi-layer structure, initializing and managing shared memory areas, and replacing global operators, the communication difficulties, memory management inconsistent and inaccurate recovery of traditional shared memory services are solved, and efficient and reliable system recovery is achieved.

CN119512810APending Publication Date: 2025-02-25WEAPON EQUIP RES INST OF CHINA NAT WEAPON EQUIP GRP
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411401453.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-09
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

Traditional shared memory recovery services have problems such as communication difficulties between processes, inconsistent memory management, difficulty in ensuring data consistency, and ineffective recovery efficiency, which affects the stability and performance of the system.

Method used

By dividing the system into a business layer, a memory pool layer, a shared memory connection layer and an operating system layer, initializing and building the object in the shared memory area, replacing the global new/delete operator as a shared memory version, managing the shared memory area and calculating the offset for correction, to achieve consistency of memory management and data consistency.

Benefits of technology

It improves the stability and reliability of the system, accelerates the recovery speed, ensures the consistency of memory management and data consistency, and improves the overall efficiency and performance of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119512810A_ABST
    Figure CN119512810A_ABST
Patent Text Reader

Abstract

The invention provides a method and a device for realizing service process recovery by accessing a shared memory, and belongs to the field of storage configuration. Comprising the following steps: dividing a system into a business layer, a memory pool layer, a shared memory connection layer and an operating system layer; initializing all objects needing to be recovered, and constructing the objects on a shared memory area; replacing all global new / deelete operators in the system with a shared memory version; executing a fork operation in the system, and managing the shared memory area for the generated child process inheriting the parent process through a pointer type object of the child process; and in a multi-process model in which a parent process or child process relationship is not shared, calculating the mapping offset during the current call and the last call, and correcting all the pointer objects according to the offset. According to the method, the stability, reliability and efficiency of the system are effectively improved, quick recovery of the shared memory is realized in a multi-process environment, and powerful support is provided for high-performance operation of the system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of storage configuration, and specifically relates to a method and apparatus for restoring service processes by accessing shared memory. Background Technology

[0002] In traditional system design, shared memory recovery services often face several challenges and drawbacks, primarily including the following aspects:

[0003] Inter-process communication is challenging: In a multi-process environment, each process has its own independent address space, necessitating specific mechanisms such as shared memory for inter-process communication. However, traditional shared memory solutions suffer from complex API differences, leading to compatibility issues across different operating systems or versions.

[0004] Inconsistent memory management: Managing shared memory often involves issues such as memory allocation, deallocation, and data sharing. However, in traditional system designs, the lack of a unified memory management mechanism can easily lead to problems such as memory leaks and memory fragmentation, thereby affecting system stability and performance.

[0005] Data consistency is difficult to guarantee: In a multi-process environment, because each process has its own independent memory space, the consistency of shared data is difficult to guarantee. Especially during inter-process communication, frequent data read and write operations may lead to data inconsistency, thereby affecting the correct operation of the system.

[0006] Inefficient recovery: When a system malfunctions or encounters unexpected situations, it is necessary to recover data in shared memory to ensure the normal operation of the system. However, traditional recovery mechanisms are often inefficient, requiring a significant amount of time and resources, thus impacting system performance and response speed.

[0007] In summary, traditional shared memory recovery services suffer from numerous drawbacks and challenges, including difficulties in inter-process communication, inconsistent memory management, challenges in ensuring data consistency, and low recovery efficiency. Therefore, a new technical solution is needed to address these issues and improve system stability, reliability, and recovery efficiency. Summary of the Invention

[0008] In view of this, the present invention provides a method for restoring service processes by accessing shared memory, comprising:

[0009] Step 1: Divide the system into the business layer, memory pool layer, shared memory interface layer, and operating system layer;

[0010] Step 2: Initialize all objects that need to be restored and build them on a shared memory region;

[0011] Step 3: Replace all global new / delete operators in the system with their shared memory versions to ensure consistency and reliability in memory management;

[0012] Step 4: Perform a fork operation in the system, and manage the shared memory region through a pointer type object of the child process that inherits from the parent process.

[0013] Step 5: In a multi-process model where there is no shared parent or child process relationship between processes, calculate the offset between the current call and the previous call, and perform correction on all pointer objects based on the offset.

[0014] Specifically, the business layer is responsible for implementing specific business logic and consists of a single thread or multiple threads, designed according to specific requirements;

[0015] The memory pool layer is responsible for memory allocation and management, including the allocation of large and small memory blocks; the memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources.

[0016] The shared memory interface layer is used to connect the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of the underlying shared memory, and providing a recovery index for restoring business functions.

[0017] The operating system layer provides APIs for shared memory operations, including the POSIX and System V series, to provide underlying shared memory operation support.

[0018] Specifically, the shared memory interface layer assigns different recovery priorities to different business objects through an attention mechanism, wherein the following algorithm is used for the recovery of business objects.

[0019]

[0020] Where k represents the index number of the business object, and i represents the instance number of the business object that needs to be restored;

[0021] F i,k This represents the state of the i-th business object instance on the k-th business object; F i,t This represents the state of the i-th business object instance on the t-th business object;

[0022] W l Γl represents the weighting coefficient of the business function of each index in the l-th memory pool layer; Ul represents the weighting coefficient of the business state of each index in the l-th layer; Γl represents the soft control parameter of the l-th layer for the calculation of index weights; tanh(W l Fi,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of W for the business function. l The business function status code undergoes a nonlinear transformation; sigm(U l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of U for the business state. l The state code is normalized to the (0, 1) interval; α i,k This represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index term of the k-th business object, and the denominator calculates the sum of the weight index terms of all indices; when restoring multiple business objects, based on α of each object on different indices... i,k The overall weight score determines which objects will be restored first, with objects with higher weights being restored first.

[0023] In particular, during the process of replacing all global new / delete operators in the system with shared memory versions, different overloads were performed for single objects, arrays of objects, and constructors to cover all possible memory allocation scenarios. At the same time, pointer references and inheritance relationships across shared memory boundaries also needed to be handled.

[0024] Specifically, the offset mapped between the current call and the previous call is calculated, and corrections are performed on all pointer objects, including calculating the correction value for the pointer objects using the following linear interpolation formula:

[0025]

[0026] Among them, the original pointer value M k Mapped to the corrected pointer value P k The original pointer address range is [M min M max The corrected address range is [P] min P max Based on the above linear interpolation formula, the corrected pointer value is calculated, and P is calculated. k The value of M; in the linear interpolation formula, M max -M min M represents the span of the original address range. k -M min This represents the offset of the current original pointer value relative to the minimum value; calculate the original pointer value M. k The minimum range M relative to the original pointer value min The offset of P; max -P min Indicates the span of the corrected address range; This represents the scaling factor, used to scale the original range to the target range.

[0027] The present invention also discloses an apparatus for restoring service processes by accessing shared memory, comprising:

[0028] The system layering module is used to divide the system into a business layer, a memory pool layer, a shared memory interface layer, and an operating system layer;

[0029] The initialization module is used to initialize all objects that need to be restored and to build them on a shared memory area;

[0030] The operator replacement module is used to replace all global new / delete operators in the system with shared memory versions to ensure consistency and reliability of memory management.

[0031] The fork operation module is used to perform fork operations in the system, and manage the shared memory region through a pointer type object of the child process that inherits from the parent process.

[0032] The pointer object correction module is used in a multi-process model where there is no shared parent or child process relationship between them to calculate the offset between the current call and the previous call, and to correct all pointer objects based on the offset.

[0033] Specifically, the business layer is responsible for implementing specific business logic and consists of a single thread or multiple threads, designed according to specific requirements;

[0034] The memory pool layer is responsible for memory allocation and management, including the allocation of large and small memory blocks; the memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources.

[0035] The shared memory interface layer is used to connect the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of the underlying shared memory, and providing a recovery index for restoring business functions.

[0036] The operating system layer provides APIs for shared memory operations, including the POSIX and System V series, to provide underlying shared memory operation support.

[0037] Specifically, the shared memory interface layer assigns different recovery priorities to different business objects through an attention mechanism, wherein the following algorithm is used for the recovery of business objects.

[0038]

[0039] Where k represents the index number of the business object, and i represents the instance number of the business object that needs to be restored;

[0040] F i,k This represents the state of the i-th business object instance on the k-th business object; F i,t This represents the state of the i-th business object instance on the t-th business object;

[0041] W l Γl represents the weighting coefficient of the business function of each index in the l-th memory pool layer; Ul represents the weighting coefficient of the business state of each index in the l-th layer; Γl represents the soft control parameter of the l-th layer for the calculation of index weights; tanh(W l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of W for the business function. l The business function status code undergoes a nonlinear transformation; sigm(U l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of U for the business state. l The state code is normalized to the (0, 1) interval; α i,k This represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index term of the k-th business object, and the denominator calculates the sum of the weight index terms of all indices; when restoring multiple business objects, based on α of each object on different indices... i,k The overall weight score determines which objects will be restored first, with objects with higher weights being restored first.

[0042] In particular, during the process of replacing all global new / delete operators in the system with shared memory versions, different overloads were performed for single objects, arrays of objects, and constructors to cover all possible memory allocation scenarios. At the same time, pointer references and inheritance relationships across shared memory boundaries also needed to be handled.

[0043] Specifically, the offset mapped between the current call and the previous call is calculated, and corrections are performed on all pointer objects, including calculating the correction value for the pointer objects using the following linear interpolation formula:

[0044]

[0045] Among them, the original pointer value M k Mapped to the corrected pointer value P k The original pointer address range is [M min M max The corrected address range is [P] min P maxBased on the above linear interpolation formula, the corrected pointer value is calculated, and P is calculated. k The value of M; in the linear interpolation formula, M max -M min M represents the span of the original address range. k -M min This represents the offset of the current original pointer value relative to the minimum value; calculate the original pointer value M. k The minimum range M relative to the original pointer value min The offset of P; max -P min Indicates the span of the corrected address range; This represents the scaling factor, used to scale the original range to the target range.

[0046] Beneficial effects:

[0047] The technical solution of this invention achieves a highly efficient shared memory recovery service mechanism, which has the following technical effects:

[0048] 1. Improve system stability and reliability. By dividing the system into a business layer, a memory pool layer, a shared memory interface layer, and an operating system layer, effective management and operation of shared memory are achieved, thereby improving system stability and reliability.

[0049] 2. Accelerate recovery speed: By initializing and building all objects to be recovered in the shared memory area before performing the fork operation, the initialization and construction time during the recovery process is reduced, thereby accelerating the recovery speed.

[0050] 3. Ensure consistent memory management: By replacing all global new / delete operators in the system with shared memory versions, consistent and reliable memory management is ensured, avoiding memory leaks and memory fragmentation issues.

[0051] 4. Improve system efficiency: By managing shared memory areas through subprocesses, efficient reading, writing, and maintenance of shared data are achieved, thereby improving the overall efficiency and performance of the system.

[0052] 5. Ensure data consistency in a multi-process model. For multi-process models where there are no shared parent or child process relationships, the consistency and accuracy of data across multiple processes are ensured by calculating the offset of the mapping and correcting the pointer objects. Attached Figure Description

[0053] Figure 1 This is a schematic diagram of the method for restoring service processes by accessing shared memory in this invention;

[0054] Figure 2This is a schematic diagram of the device for restoring service processes by accessing shared memory in this invention. Detailed Implementation

[0055] The present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0056] This invention provides a method for restoring service processes by accessing shared memory, the process of which is as follows: Figure 1 As shown, it includes:

[0057] Step 1: Divide the system into a business layer, a memory pool layer, a shared memory interface layer, and an operating system layer; the business layer is responsible for implementing specific business logic and consists of single-threaded or multi-threaded components, designed according to specific requirements.

[0058] The memory pool layer is responsible for memory allocation and management, including the allocation of large and small memory blocks; the memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources.

[0059] The shared memory interface layer connects the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of underlying shared memory, and providing a recovery index for restoring business functions. The shared memory interface layer assigns different recovery priorities to different business objects through an attention mechanism, specifically employing the following algorithm for restoring business objects.

[0060]

[0061] Where k represents the index number of the business object, and i represents the instance number of the business object that needs to be restored;

[0062] F i,k This represents the state of the i-th business object instance on the k-th business object; F i,t This represents the state of the i-th business object instance on the t-th business object;

[0063] W l Γl represents the weighting coefficient of the business function of each index in the l-th memory pool layer; Ul represents the weighting coefficient of the business state of each index in the l-th layer; Γl represents the soft control parameter of the l-th layer for the calculation of index weights; tanh(W l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of W for the business function. l The business function status code undergoes a nonlinear transformation; sigm(U l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of U for the business state. lThe state code is normalized to the (0, 1) interval; α i,k This represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index term of the k-th business object, and the denominator calculates the sum of the weight index terms of all indices; when restoring multiple business objects, based on α of each object on different indices... i,k The overall weight score determines which objects will be restored first, with objects with higher weights being restored first.

[0064] The operating system layer provides APIs for shared memory operations, including the POSIX and System V series, to provide underlying shared memory operation support.

[0065] Step 2: Initialize all objects that need to be restored and build them in a shared memory area; Object initialization: For all objects that need to be used in the restoration process, including data structures, managers, state information, etc., perform initialization operations. This includes allocating memory space, setting initial values, and building data structures, etc.

[0066] Built on a shared memory region: Initialized objects are built on a shared memory region to ensure that these objects can be shared and accessed between different processes. This typically involves using shared memory mechanisms provided by the operating system, such as the mmap() function, to allocate space in the shared memory region and copy the object's data into the shared memory.

[0067] Step 3: Replace all global `new` / `delete` operators in the system with their shared-memory versions to ensure consistency and reliability in memory management. Replacing global `new` / `delete` operators with shared-memory versions is a crucial step, ensuring that memory allocation and deallocation throughout the system are performed using shared memory mechanisms. When overloading `new` / `delete` operators, it's typically necessary to overload them separately for different cases such as single objects, arrays of objects, and objects with constructors, to cover all possible memory allocation scenarios. Special cases such as pointer references across shared memory boundaries and inheritance relationships also need to be handled. Therefore, the implementation of this step is relatively complex.

[0068] This fundamentally solves the problems of consistency and reliability in memory management. Specifically, this step brings the following benefits: Regardless of whether a process is a parent or child process, they all use the same shared memory space. By overloading the `new` / `delete` operators, it ensures that all memory allocation operations uniformly follow the shared memory allocation path, avoiding memory space fragmentation. Shared memory allocators generally employ robust memory management strategies, such as memory pools and garbage collection. Compared to the traditional simple `malloc` / `free`, the shared memory version of `new` / `delete` has higher reliability and can better prevent memory leaks, fragmentation, and other problems. Through operator overloading, the upper-level application code does not need to make any special modifications to shared memory; the calls to `new` / `delete` are consistent with input and output, improving the transparency and versatility of the solution. In the future, if more complex shared memory allocation strategies or memory management mechanisms need to be supported, only the corresponding overloaded `new` / `delete` implementation needs to be modified, without changing the application code, improving scalability. This step minimizes the intrusion of shared memory into the existing system, enhances stability and reliability, and is an important guarantee for the successful implementation of the solution.

[0069] Replacement of the new / delete operators is achieved using the attribute setting method provided by gcc, __attribute__, in conjunction with alias.

[0070] In GCC, there are strong and weak symbolic attributes. If there are multiple strong symbols with the same name during linking, an error will be reported.

[0071] By default, the compiler treats functions and initialized global variables as strong symbols, while uninitialized global variables are weak symbols. However, you can also explicitly use __attribute((weak)) to use weak symbol attributes.

[0072] Declaring a weakly symbolic function but not defining it will allow linking to succeed. However, calling the function will cause a crash.

[0073] Command to check the symbol type of an object file: nm*.o

[0074] Rules for using strong and weak signs

[0075] 1) There can only be one strong symbol with the same name; otherwise, the compiler will report a duplicate definition.

[0076] 2) Allows one strong sign and multiple weak signs, but the definition will choose the strong sign.

[0077] 3) When there are multiple weak symbols, the compiler will choose the one that occupies the most space.

[0078] For projects using C++11, there are multiple versions of new / delete, including those that can throw exceptions and those that cannot, requiring careful replacement.

[0079] Step 4: Perform a fork operation in the system. The resulting child process, inheriting from the parent process, manages the shared memory region through a pointer-type object of the child process. After the fork operation, the child process inherits the parent process's address space, including the pointer to the shared memory region. To manage this shared memory region, the child process can use a pointer-type object. This process can be divided into the following steps:

[0080] Obtaining pointers: Child processes inherit pointers to shared memory regions from their parent processes and can obtain these pointers in appropriate ways.

[0081] Constructing a management object: In a child process, a specific pointer type object can be used to manage shared memory regions. This management object can be an instance of a data structure or class, containing pointers to the shared memory region and related management operations.

[0082] Managing shared memory regions: Through the methods or interfaces provided by the management object, child processes can perform read and write operations on shared memory regions, thereby managing and maintaining shared data. This includes operations such as reading data, writing data, and modifying data.

[0083] Synchronization and Coordination: Since shared memory regions may be accessed by multiple processes simultaneously, child processes need to consider the issue of concurrent access and take appropriate synchronization and coordination measures to ensure that operations on shared memory regions are safe and correct.

[0084] Through the above steps, the child process can effectively manage the shared memory area inherited from the parent process, realize the reading, writing and maintenance of shared data, and thus complete the management of the shared memory area in the system.

[0085] Step 5: In a multi-process model where there are no shared parent or child process relationships, calculate the offset between the current call and the previous call, and correct all pointer objects based on the offset. In this method, the restoration of member objects such as locks involves the following cases:

[0086] 1) Processes do not have affinities and need to update the address offset of related member objects (if the member object is a pointer type; otherwise, no further consideration is needed).

[0087] 2) Processes are related and do not need to make address offset corrections. This is because the new process created by fork completely copies the heap segment, stack segment, data segment, and BSS segment of the old process. That is, variables with the same name have the same virtual address.

[0088] For restoring derived classes with parent classes, the key points to focus on are the location of the virtual table and the virtual table pointer within the process's memory layout. The virtual table resides in the static area of ​​the data segment. The virtual table pointer's location follows the object; wherever the object is located, the virtual table pointer will also be located there.

[0089] The new process will completely copy the heap segment, stack segment, data segment, and BSS segment of the old process. In other words, the offset of the base class will also be copied to the new process. This means that when the derived class is restored in the new process, no additional adjustments need to be made to its virtual table pointer.

[0090] Whether or not memory addresses need to be corrected depends on two scenarios: a non-related multi-process model and a related multi-process model. For a related multi-process model, thanks to the characteristics of the fork system call, the shared memory address offset can be completely disregarded.

[0091] For multi-process models without affinity, since there is no affinity and the virtual address returned after each mmap call is different, it is necessary to consider correcting the memory address. This is done by calculating the offset between the starting address of the current mapping and the starting address of the previous mapping (note that errors in signed and unsigned value conversion should be avoided). This correction based on the offset is then performed on all subsequently used pointer type objects.

[0092] For lock inheritance between parent and child processes with kinship, the child process created by fork inherits the lock state of the parent process. Deadlock can occur if the lock does not have cross-process attributes (POSIX mutex not having the POSIX_PROESS_SHARED attribute set or the lock being created based on non-shared memory).

[0093] In other words, setting cross-process attributes for locks can avoid deadlock issues in multi-process scenarios. This is because the lock's state is visible to both parent and child processes (due to shared memory).

[0094] In a multi-process model where there are no shared parent or child process relationships, the offset mapped between the current call and the previous call is calculated, and all pointer objects are corrected based on this offset. Specifically, calculating the offset mapped between the current call and the previous call and correcting all pointer objects involves calculating the corrected value of the pointer object using the following linear interpolation formula:

[0095]

[0096] Among them, the original pointer value M k Mapped to the corrected pointer value P k The original pointer address range is [M min M max The corrected address range is [P] min P max Based on the above linear interpolation formula, the corrected pointer value is calculated, and P is calculated. k The value of M; in the linear interpolation formula, M max -M min M represents the span of the original address range. k -M min This represents the offset of the current original pointer value relative to the minimum value; calculate the original pointer value M. k The minimum range M relative to the original pointer value min The offset of P; max -P min Indicates the span of the corrected address range; This represents the scaling factor, used to scale the original range to the target range.

[0097] This invention also discloses a device for restoring service processes by accessing shared memory, such as... Figure 2 As shown, it includes:

[0098] The system layering module is used to divide the system into a business layer, a memory pool layer, a shared memory interface layer, and an operating system layer. The business layer is responsible for implementing specific business logic and consists of single-threaded or multi-threaded components, designed according to specific requirements.

[0099] The memory pool layer is responsible for memory allocation and management, including the allocation of large and small memory blocks; the memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources.

[0100] The shared memory interface layer connects the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of underlying shared memory, and providing a recovery index for restoring business functions. The shared memory interface layer assigns different recovery priorities to different business objects through an attention mechanism, specifically employing the following algorithm for restoring business objects.

[0101]

[0102] Where k represents the index number of the business object, and i represents the instance number of the business object that needs to be restored;

[0103] F i,kThis represents the state of the i-th business object instance on the k-th business object; F i,t This represents the state of the i-th business object instance on the t-th business object;

[0104] W l Γl represents the weighting coefficient of the business function of each index in the l-th memory pool layer; Ul represents the weighting coefficient of the business state of each index in the l-th layer; Γl represents the soft control parameter of the l-th layer for the calculation of index weights; tanh(W l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of W for the business function. l The business function status code undergoes a nonlinear transformation; sigm(U l F i,k This represents the i-th business object instance on the k-th business object, with a weight coefficient of U for the business state. l The state code is normalized to the (0, 1) interval; α i,k This represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index term of the k-th business object, and the denominator calculates the sum of the weight index terms of all indices; when restoring multiple business objects, based on α of each object on different indices... i,k The overall weight score determines which objects will be restored first, with objects with higher weights being restored first.

[0105] The operating system layer provides APIs for shared memory operations, including the POSIX and System V series, to provide underlying shared memory operation support.

[0106] The initialization module initializes all objects that need to be restored and constructs them in a shared memory area. It performs initialization operations on all objects used during the restoration process, including data structures, managers, and state information. This includes allocating memory space, setting initial values, and creating data structures.

[0107] Built on a shared memory region: Initialized objects are built on a shared memory region to ensure that these objects can be shared and accessed between different processes. This typically involves using shared memory mechanisms provided by the operating system, such as the mmap() function, to allocate space in the shared memory region and copy the object's data into the shared memory.

[0108] The operator replacement module replaces all global `new` / `delete` operators in the system with their shared-memory versions to ensure consistency and reliability in memory management. Replacing global `new` / `delete` operators with shared-memory versions is a crucial step, ensuring that memory allocation and deallocation throughout the system are performed using shared memory mechanisms. When overloading the `new` / `delete` operators, separate overloads are typically needed for different scenarios such as single objects, arrays of objects, and objects with constructors, to cover all possible memory allocation scenarios. Special cases such as pointer references across shared memory boundaries and inheritance relationships also need to be handled. Therefore, the implementation of this step is relatively complex.

[0109] This fundamentally solves the problems of consistency and reliability in memory management. Specifically, this step brings the following benefits: Regardless of whether a process is a parent or child process, they all use the same shared memory space. By overloading the `new` / `delete` operators, it ensures that all memory allocation operations uniformly follow the shared memory allocation path, avoiding memory space fragmentation. Shared memory allocators generally employ robust memory management strategies, such as memory pools and garbage collection. Compared to the traditional simple `malloc` / `free`, the shared memory version of `new` / `delete` has higher reliability and can better prevent memory leaks, fragmentation, and other problems. Through operator overloading, the upper-level application code does not need to make any special modifications to shared memory; the calls to `new` / `delete` are consistent with input and output, improving the transparency and versatility of the solution. In the future, if more complex shared memory allocation strategies or memory management mechanisms need to be supported, only the corresponding overloaded `new` / `delete` implementation needs to be modified, without changing the application code, improving scalability. This step minimizes the intrusion of shared memory into the existing system, enhances stability and reliability, and is an important guarantee for the successful implementation of the solution.

[0110] Replacement of the new / delete operators is achieved using the attribute setting method provided by gcc, __attribute__, in conjunction with alias.

[0111] In GCC, there are strong and weak symbolic attributes. If there are multiple strong symbols with the same name during linking, an error will be reported.

[0112] By default, the compiler treats functions and initialized global variables as strong symbols, while uninitialized global variables are weak symbols. However, you can also explicitly use __attribute((weak)) to use weak symbol attributes.

[0113] Declaring a weakly symbolic function but not defining it will allow linking to succeed. However, calling the function will cause a crash.

[0114] Command to check the symbol type of an object file: nm*.o

[0115] Rules for using strong and weak signs

[0116] 1) There can only be one strong symbol with the same name; otherwise, the compiler will report a duplicate definition.

[0117] 2) Allows one strong sign and multiple weak signs, but the definition will choose the strong sign.

[0118] 3) When there are multiple weak symbols, the compiler will choose the one that occupies the most space.

[0119] For projects using C++11, there are multiple versions of new / delete, including those that can throw exceptions and those that cannot, requiring careful replacement.

[0120] The fork operation module is used to perform a fork operation in the system. The resulting child process, inheriting from the parent process, manages the shared memory region through a pointer-type object of the child process. After the fork operation is performed, the child process inherits the parent process's address space, including the pointer to the shared memory region. To manage this shared memory region, the child process can use a pointer-type object. This process can be divided into the following steps:

[0121] Obtaining pointers: Child processes inherit pointers to shared memory regions from their parent processes and can obtain these pointers in appropriate ways.

[0122] Constructing a management object: In a child process, a specific pointer type object can be used to manage shared memory regions. This management object can be an instance of a data structure or class, containing pointers to the shared memory region and related management operations.

[0123] Managing shared memory regions: Through the methods or interfaces provided by the management object, child processes can perform read and write operations on shared memory regions, thereby managing and maintaining shared data. This includes operations such as reading data, writing data, and modifying data.

[0124] Synchronization and Coordination: Since shared memory regions may be accessed by multiple processes simultaneously, child processes need to consider the issue of concurrent access and take appropriate synchronization and coordination measures to ensure that operations on shared memory regions are safe and correct.

[0125] Through the above steps, the child process can effectively manage the shared memory area inherited from the parent process, realize the reading, writing and maintenance of shared data, and thus complete the management of the shared memory area in the system.

[0126] The pointer object correction module is used in a multi-process model where there is no shared parent or child process relationship between them to calculate the offset between the current call and the previous call, and to correct all pointer objects based on the offset.

[0127] In this scheme, the restoration of member objects, such as locks, involves the following situations:

[0128] 1) Processes do not have affinities and need to update the address offset of related member objects (if the member object is a pointer type; otherwise, no further consideration is needed).

[0129] 2) Processes are related and do not need to make address offset corrections. This is because the new process created by fork completely copies the heap segment, stack segment, data segment, and BSS segment of the old process. That is, variables with the same name have the same virtual address.

[0130] For restoring derived classes with parent classes, the key points to focus on are the location of the virtual table and the virtual table pointer within the process's memory layout. The virtual table resides in the static area of ​​the data segment. The virtual table pointer's location follows the object; wherever the object is located, the virtual table pointer will also be located there.

[0131] The new process will completely copy the heap segment, stack segment, data segment, and BSS segment of the old process. In other words, the offset of the base class will also be copied to the new process. This means that when the derived class is restored in the new process, no additional adjustments need to be made to its virtual table pointer.

[0132] Whether or not memory addresses need to be corrected depends on two scenarios: a non-related multi-process model and a related multi-process model. For a related multi-process model, thanks to the characteristics of the fork system call, the shared memory address offset can be completely disregarded.

[0133] For multi-process models without affinity, since there is no affinity and the virtual address returned after each mmap call is different, it is necessary to consider correcting the memory address. This is done by calculating the offset between the starting address of the current mapping and the starting address of the previous mapping (note that errors in signed and unsigned value conversion should be avoided). This correction based on the offset is then performed on all subsequently used pointer type objects.

[0134] For lock inheritance between parent and child processes with kinship, the child process created by fork inherits the lock state of the parent process. Deadlock can occur if the lock does not have cross-process attributes (POSIX mutex not having the POSIX_PROESS_SHARED attribute set or the lock being created based on non-shared memory).

[0135] In other words, setting cross-process attributes for locks can avoid deadlock issues in multi-process scenarios. This is because the lock's state is visible to both parent and child processes (due to shared memory).

[0136] In a multi-process model where there are no shared parent or child process relationships, the offset mapped between the current call and the previous call is calculated, and all pointer objects are corrected based on this offset. Specifically, calculating the offset mapped between the current call and the previous call and correcting all pointer objects involves calculating the corrected value of the pointer object using the following linear interpolation formula:

[0137]

[0138] Among them, the original pointer value M k Mapped to the corrected pointer value P k The original pointer address range is [M min M max The corrected address range is [P] min P max Based on the above linear interpolation formula, the corrected pointer value is calculated, and P is calculated. k The value of M; in the linear interpolation formula, M max -M min M represents the span of the original address range. k -M min This represents the offset of the current original pointer value relative to the minimum value; calculate the original pointer value M. k The minimum range M relative to the original pointer value min The offset of P; max -P min Indicates the span of the corrected address range; This represents the scaling factor, used to scale the original range to the target range.

[0139] In summary, the above are merely preferred embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

[0140] It will be apparent to those skilled in the art that the embodiments of the present invention are not limited to the details of the exemplary embodiments described above, and that the embodiments of the present invention can be implemented in other specific forms without departing from the spirit or essential characteristics of the embodiments of the present invention. Therefore, the embodiments should be considered exemplary and non-limiting in all respects, and the scope of the embodiments of the present invention is defined by the appended claims rather than the foregoing description. Therefore, all variations falling within the meaning and scope of equivalents of the claims are intended to be encompassed within the embodiments of the present invention. No reference numerals in the claims should be construed as limiting the scope of the claims. Furthermore, it is clear that the word "comprising" does not exclude other units or steps, and the singular does not exclude the plural. Multiple units, modules, or devices recited in the system, apparatus, or terminal claims may also be implemented by the same unit, module, or device through software or hardware. The terms "first," "second," etc., are used to indicate names and do not indicate any particular order.

[0141] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention and are not intended to limit them. Although the embodiments of the present invention have been described in detail with reference to the above preferred embodiments, those skilled in the art should understand that modifications or equivalent substitutions to the technical solutions of the embodiments of the present invention should not depart from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for accessing a shared memory to realize service process recovery, characterized in that: include: Step 1, divide the system into a business layer, a memory pool layer, a shared memory connection layer and an operating system layer; Step 2, initialize all objects that need to be restored and build them on the shared memory area; Step 3: Replace all global new / delete operators in the system with shared memory versions to ensure consistency and reliability of memory management. Step 4, executing a fork operation in the system, and managing the shared memory area for the generated child process that inherits the parent process through the pointer type object of the child process; Step 5: In a multi-process model where there is no shared parent process or child process relationship between each other, the offset mapped during the current call and the last call is calculated, and all pointer objects are corrected according to the offset.

2. The method for accessing shared memory to realize service process recovery according to claim 1, characterized in that: include: The business layer is responsible for implementing specific business logic, and is composed of single thread or multi-thread, and is designed according to specific needs; The memory pool layer is responsible for the allocation and management of memory, including the allocation of large and small blocks of memory; The memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources; The shared memory connection layer is used to connect the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of the underlying shared memory, and providing a recovery index for restoring business functions; The operating system layer provides APIs for shared memory operations, including posix series and systemV series, for providing underlying shared memory operation support.

3. The method for accessing shared memory to realize service process recovery as claimed in claim 2, characterized in that: include: The shared memory connection layer assigns different recovery priorities to different business objects through the attention mechanism, wherein the following algorithm is specifically used to recover business objects: Wherein, k represents the index number of the business object, and i represents the instance number of the business object to be restored; F i,k Indicates the state of the i-th business object instance on the k-th business object; F i,t Indicates the state of the i-th business object instance on the t-th business object; W l represents the weight coefficient of the business function of the l-th layer memory pool layer for each index; Ul represents the weight coefficient of the business status of each index at the l-th layer; Γl represents the soft control parameter of the index weight calculation at the l-th layer; tanh(W l F i,k ) indicates that the weight coefficient of the business function of the i-th business object instance on the k-th business object is W l The business function state coding is nonlinearly transformed; sigm(U l F i,k ) indicates that the weight coefficient of the business state of the i-th business object instance on the k-th business object is U l The state code of is normalized to the interval (0, 1); α i,k Represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index item of the k-th business object, and the denominator calculates the sum of the weight index items of all indexes; When restoring multiple business objects, the α values ​​of each object at different indexes are i,k The comprehensive weight score is used to decide which objects to restore first, and objects with high weights will be restored first.

4. The method for accessing a shared memory to realize service process recovery as claimed in claim 3, characterized in that: include: In the process of replacing all global new / delete operators in the system with shared memory versions, they are overloaded for different situations of single objects, object arrays and constructors to cover all possible memory allocation scenarios. At the same time, pointer references and inheritance relationships across shared memory boundaries need to be handled.

5. The method for accessing a shared memory to realize service process recovery as claimed in claim 4, characterized in that: include: Calculate the offset mapped in this call and the last call, and perform corrections on all pointer objects, including: calculating the correction value of the pointer object through the following linear difference formula: Among them, the original pointer value M k Mapped to the corrected pointer value P k , the original pointer address range is [M min , M max ], the corrected address range is [P min , P max ], according to the above linear difference formula, the corrected pointer value is calculated to calculate P k The value of; in the linear difference formula, M max -M min Indicates the span size of the original address range, M k -M min Indicates the offset of the current original pointer value relative to the minimum value; calculates the original pointer value M k The minimum range M relative to the original pointer value min The offset of P max -P min Indicates the span size of the corrected address range; Represents the scaling factor used to scale the original extent to the target extent.

6. A device for accessing a shared memory to realize service process recovery, characterized in that: include: System layering module, used to divide the system into business layer, memory pool layer, shared memory connection layer and operating system layer; The initialization module is used to initialize all objects that need to be restored and build them on the shared memory area; Operator replacement module, used to replace all global new / delete operators in the system with shared memory versions to ensure consistency and reliability of memory management; A fork operation module, used for executing a fork operation in the system, and managing the shared memory area of ​​the generated child process that inherits the parent process through the pointer type object of the child process; The pointer object correction module is used to calculate the offset mapped during the current call and the last call in a multi-process model without a shared parent process or child process relationship, and perform correction on all pointer objects according to the offset.

7. The device for accessing a shared memory to realize service process recovery according to claim 6, characterized in that: include: The business layer is responsible for implementing specific business logic, and is composed of single thread or multi-thread, and is designed according to specific needs; The memory pool layer is responsible for the allocation and management of memory, including the allocation of large and small blocks of memory; The memory allocated in the memory pool comes from shared memory, ensuring that multiple processes can share memory resources; The shared memory connection layer is used to connect the business layer and the operating system layer; it is responsible for shielding the differences in the API operation interface of the operating system layer, managing the allocation and release of the underlying shared memory, and providing a recovery index for restoring business functions; The operating system layer provides APIs for shared memory operations, including posix series and systemV series, for providing underlying shared memory operation support.

8. The device for accessing a shared memory to realize service process recovery as claimed in claim 7, characterized in that: include: The shared memory connection layer assigns different recovery priorities to different business objects through the attention mechanism, wherein the following algorithm is specifically used to recover business objects: Wherein, k represents the index number of the business object, and i represents the instance number of the business object to be restored; F i,k Indicates the state of the i-th business object instance on the k-th business object; F i,t Indicates the state of the i-th business object instance on the t-th business object; W l represents the weight coefficient of the business function of the l-th layer memory pool layer for each index; Ul represents the weight coefficient of the business status of each index at the l-th layer; Γl represents the soft control parameter of the index weight calculation at the l-th layer; tanh(W l F i,k ) indicates that the weight coefficient of the business function of the i-th business object instance on the k-th business object is W l The business function state coding is nonlinearly transformed; sigm(U l F i,k ) indicates that the weight coefficient of the business state of the i-th business object instance on the k-th business object is U l The state code of is normalized to the interval (0, 1); α i,k Represents the comprehensive weight score of the i-th business object instance on the k-th business object; in the algorithm, the numerator calculates the weight index item of the k-th business object, and the denominator calculates the sum of the weight index items of all indexes; When restoring multiple business objects, the α values ​​of each object at different indexes are i,k The comprehensive weight score is used to decide which objects to restore first, and objects with high weights will be restored first.

9. The device for accessing a shared memory to realize service process recovery according to claim 8, characterized in that: include: In the process of replacing all global new / delete operators in the system with shared memory versions, they are overloaded for different situations of single objects, object arrays and constructors to cover all possible memory allocation scenarios. At the same time, pointer references and inheritance relationships across shared memory boundaries need to be handled.

10. The device for accessing a shared memory to realize service process recovery according to claim 9, characterized in that: include: Calculate the offset mapped in this call and the last call, and perform corrections on all pointer objects, including: calculating the correction value of the pointer object through the following linear difference formula: Among them, the original pointer value M k Mapped to the corrected pointer value P k , the original pointer address range is [M min , M max ], the corrected address range is [P min , P max ], according to the above linear difference formula, the corrected pointer value is calculated to calculate P k The value of; in the linear difference formula, M max -M min Indicates the span size of the original address range, M k -M min Indicates the offset of the current original pointer value relative to the minimum value; calculates the original pointer value M k The minimum range M relative to the original pointer value min The offset of P max -P min Indicates the span size of the corrected address range; Represents the scaling factor used to scale the original extent to the target extent.

Citation Information

Cited By

  • Model construction method, deployment method and electronic equipment

    CN121168512A