Process communication method, apparatus and device, and computer readable storage medium
By determining the target shared memory pool based on the basic block size and number of basic blocks in a multi-process system and setting a data callback function, the problem of low communication efficiency between processes is solved, and timely synchronization of data and efficient communication is achieved.
Patent Information
- Application Number
- CN202412000459.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-04-01
AI Technical Summary
In the prior art, inter-process communication efficiency is low and synchronization of data in shared memory cannot be achieved.
By determining the target shared memory pool based on the basic block size and number of basic blocks, setting the data callback function of the target process, starting the data processing thread, waiting for data change events to realize data communication between processes.
Improves the efficiency of inter-process communication and realizes timely synchronization of data without the need to continuously poll memory state.
Smart Images

Figure CN119739547A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of data communication technology, and in particular to a process communication method, device, equipment and computer-readable storage medium. Background Art
[0002] After adopting a multi-process system, a system performance bottleneck is immediately faced - the problem of inter-process communication; inter-process communication is the transmission or exchange of information between different processes. At present, the industry generally adopts the shared memory method to deal with the problem of inter-process communication. Shared memory is a piece of physical memory shared by multiple processes. However, when the current process uses shared memory, it cannot synchronize the data in the shared memory, resulting in low data communication efficiency.
[0003] It can be seen that how to improve the process communication efficiency is a technical problem that technical personnel in this field urgently need to solve. Summary of the invention
[0004] In view of this, an object of the present invention is to provide a method, apparatus, device and computer-readable storage medium for process communication, which solves the technical problem of low process communication efficiency in the prior art.
[0005] To solve the above technical problems, the present invention provides a process communication method, comprising:
[0006] Determine the target shared memory pool based on the basic block size and the number of basic blocks;
[0007] Determine a target process corresponding to the target shared memory pool, and set a data callback function corresponding to the target process; wherein the data callback function includes a function for processing an event in which data in the target shared memory pool changes;
[0008] The data processing thread is started, and waits for data change events triggered by other processes, so as to trigger the data callback function based on the data change events, thereby realizing data communication between processes.
[0009] On the one hand, determining the target shared memory pool based on the basic block size and the number of basic blocks includes:
[0010] Determine the space size of the shared memory pool based on the basic block size and the number of basic blocks;
[0011] Determine whether it is the first creation;
[0012] If it is the first creation, initialize the memory pool information at the starting position of the shared memory pool;
[0013] If it is not the first creation, check whether the flag is the shared memory pool flag;
[0014] If it is not a shared memory pool identifier, determine whether it is newly created;
[0015] If it is newly created, the process lock is seized, the reference count is increased, the process lock is released, and it is determined that the target shared memory pool is successfully created.
[0016] On the one hand, the process communication method further includes:
[0017] Determine whether the reference count of the target shared memory pool is 0; wherein the reference count is the number of processes using the target shared memory pool;
[0018] When the reference count of the target shared memory pool is 0, it is determined to destroy the target shared memory pool.
[0019] On the one hand, the process communication method further includes:
[0020] Seize the process lock, search for process information according to the process name, retrieve the process information, and release the process lock;
[0021] Stop the data consumption thread; wherein the data consumption thread refers to a thread specifically used to read, process and consume data;
[0022] Release the data in the data loop queue in sequence until it is empty, and release the data change event;
[0023] Release the space occupied by the process information of the target process.
[0024] On the one hand, determining a target process corresponding to the target shared memory pool and setting a data callback function corresponding to the target process include:
[0025] Seize the process lock, check whether the process information of the target process already exists in the registered process linked list; wherein the registered process linked list is a process linked list recording the registered data callback function; the process lock is to achieve the setting of mutually exclusive access configuration information;
[0026] If the process information does not exist, calling a shared memory space function to apply for space for process information of setting queue length; wherein the parameter of the shared memory space function is the number of memory bytes to be allocated, the output is a shared memory space and a shared memory base pointer, and a memory pointer is obtained by calculating based on the shared memory space and the shared memory base pointer through a buddy algorithm;
[0027] Set the name of the process information, the data change event, and initialize the data circular queue; wherein the data circular queue contains two fields: offset and data length;
[0028] Add the process information to the registered process list; wherein the process list is a process including a data callback function;
[0029] Set the process ID of the process information to the current process ID and release the process lock;
[0030] If the process information exists, directly set the process ID of the process information as the current process ID and release the process lock;
[0031] Set the data callback function and start the data processing thread.
[0032] On the one hand, the process communication method further includes:
[0033] Receive process name, data pointer and data length;
[0034] Calculate an offset based on the data pointer and the shared memory base pointer;
[0035] Seize the process lock and search for the process information corresponding to the process name. If no information is found, an error is returned.
[0036] If found, add data to the data loop queue and set the offset and data length;
[0037] Trigger a data change event.
[0038] On the one hand, the information stored in the target shared memory pool includes a shared memory base pointer, a memory pool information pointer and a data processing callback function pointer, and the data callback function pointer points to a callback function registered by the target process.
[0039] The embodiment of the present invention also provides a process communication method, including:
[0040] A shared memory pool determination module, used for determining a target shared memory pool based on a basic block size and a basic block number;
[0041] A data callback function determination module is used to determine the target process corresponding to the target shared memory pool and set the data callback function corresponding to the target process; wherein the data callback function includes a function for processing events of data changes in the target shared memory pool;
[0042] The communication module is used to start the data processing thread, wait for the data change event triggered by other processes, trigger the data callback function based on the data sending change event, and realize the communication of data between processes.
[0043] The embodiment of the present invention further provides a process communication device, including:
[0044] Memory for storing computer programs;
[0045] A processor is used to execute the computer program to implement the steps of the above process communication method.
[0046] An embodiment of the present invention further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the above-mentioned process communication method are implemented.
[0047] The present invention also provides a computer program product, comprising a computer program / instruction, which implements the steps of the above-mentioned data communication method when executed by a processor.
[0048] To solve the above technical problems, an embodiment of the present invention provides a method for data communication, which may include: determining a target shared memory pool based on a basic block size and a basic block number; determining a target process corresponding to the target shared memory pool, and setting a data callback function corresponding to the target process; wherein the data callback function includes a function for processing data change events in the target shared memory pool; starting a data processing thread, waiting for data change events triggered by other processes, triggering a data callback function based on the data change events, and realizing data communication between processes.
[0049] It can be seen from the above technical scheme that the beneficial effect of the present invention is that compared with the current inability to synchronize data between processes in a timely manner, the present invention sets a data callback function corresponding to the target process, so that a quick response can be made to data changes based on the data callback function without constantly polling the memory status, thereby improving the efficiency of inter-process communication. BRIEF DESCRIPTION OF THE DRAWINGS
[0050] In order to more clearly illustrate the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0051] Figure 1 A flowchart of a process communication method provided by an embodiment of the present invention;
[0052] Figure 2 A memory structure and direction diagram provided by an embodiment of the present invention;
[0053] Figure 3 A flow chart of creating a target shared memory pool provided by an embodiment of the present invention;
[0054] Figure 4 A flowchart of a registration data callback function provided by an embodiment of the present invention;
[0055] Figure 5 A schematic diagram of the structure of a process communication device provided by an embodiment of the present invention;
[0056] Figure 6 A schematic diagram of the structure of a process communication device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0057] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0058] The terms "including" and "having" in the specification of the present invention and the above-mentioned drawings, as well as any variations thereof, are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device including a series of steps or units is not limited to the listed steps or units, but may include steps or units that are not listed.
[0059] In order to enable those skilled in the art to better understand the solution of the present invention, the present invention is further described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0060] Next, a process communication method provided by an embodiment of the present invention is described in detail. Figure 1 A flowchart of a process communication method provided by an embodiment of the present invention, the method may include:
[0061] S101, determining a target shared memory pool based on a basic block size and a basic block number.
[0062] This embodiment does not limit the specific execution subject. For example, the execution subject in this embodiment can be a process, or the execution subject in this embodiment can be a node in a cluster. The basic block size (blockSize) of this embodiment generally refers to the size of the smallest unit block used when allocating memory. This size is usually a power of 2, such as 1KB (kilobytes), 2KB, 4KB, etc. The basic number of blocks (logNumBlocks) in this embodiment represents the number of memory blocks of different sizes. The target shared memory pool in this embodiment is a specific memory area managed by an operating system or an application, and this area can be accessed by multiple processes or threads. This embodiment can apply for a target shared memory of a specified size, and the necessary information such as the process lock and allocation information of the process memory pool is recorded in the shared memory, so that multiple processes can mutually exclusive access the allocation information through the process lock, thereby realizing pool management of shared memory by multiple processes, that is, the process can mutually exclusive apply for and release a certain part of the space in the same shared memory. This embodiment can establish a process corresponding data distribution table in the shared memory pool, set a process event and data queue for each process receiving data, and control the process to process the data in the data queue through events.
[0063] The information required by the target shared memory pool ProcessMemoryPool in this embodiment may include: 1) Process shared memory pool ProcessMemoryPool, which needs to be created according to the global name or key (keyword) before each process uses it. Its information includes (the information of the same process shared memory pool obtained by different processes is different): Shared memory base pointer basePtr: records the starting position of the shared memory, that is, the shared memory space is mapped to the virtual address space of this process. Memory pool information pointer poolPtr: points to the storage location of the memory pool information. The memory pool information exists in the shared memory, so the memory pool information pointer points to the address in the shared memory after initialization. Data processing callback function pointer callback: The default is empty. The current process has registered a callback function (a callback function is a function called through a function pointer, which is usually used to execute specific code when a specific event occurs. Purpose: In data processing, callback functions are often used in asynchronous operations or event-driven programming models to perform subsequent operations when data is ready or processing is completed). The function pointer points to its registered callback function. Data processing thread: Not created by default, the current process has registered a callback function, and a corresponding data processing thread is created to call back the received data to the registered callback function. 2) Memory pool information PoolInfo. The memory pool information must be stored in the shared memory managed by this memory pool. It is a critical resource and must be accessed after obtaining the process lock. All processes obtain the same information. Its information includes: initialization flag flag: After initialization, flag is the shared memory identifier; (applying for a piece of memory to become shared memory) process locker: All changes to variables in PoolInfo need to be performed in the lock (all changes to variables, if you want to modify the variables of the shared memory pool, you need to obtain the lock and perform it in the lock); reference count: record how many processes use the memory pool. Only when the reference count is 0, it will be destroyed. Registered process linked list head processHead: record the head of the process linked list of registered callbacks. The registered process information is also stored in the shared memory, so its value is the offset of the linked list head node in the shared memory. Information required by the buddy algorithm (startMem, blockSize, logNumBlocks, etc.): The buddy algorithm is a common algorithm for memory allocation and will not be described in detail. 3) Register process information ProcessInfo, which needs to record the process information that has registered the data processing callback so that the process can receive and process data, etc. The information includes: Process number: not used as an identifier, it will change after the process is restarted. Name: The globally unique name of each process (within the shared memory pool of this process), the identifier of process communication. Process event event: When there is no data to process, the data processing thread waits for the event to enter the blocking state. When data is received, the data processing thread is awakened through the event.The next process information nextNode in the linked list records the offset position of the next process information in the shared memory. Data circular queue dataQueue: The data object contains two fields, offset offset and data length len, which record the position and length of the data in the shared memory; the circular queue for receiving data consists of a continuous array and the head and tail index positions. For easy understanding, please refer to. Figure 2 , Figure 2 A memory structure and pointing diagram provided for an embodiment of the present invention. For example, when process A and process B access a shared memory pool, they point to the memory pool information according to pointer information, register process information, and point to specific shared memory (data 1, data 2). Each registered process information corresponds to each process, and a registered process linked list is applied for in the memory. Each process corresponds to a piece of process information, pointing to the shared memory, and the position and length of the data in the shared memory are recorded through a data circular queue.
[0064] It should be further explained that, in order to improve the accuracy of creating the target shared memory pool, the above-mentioned determination of the target shared memory pool based on the basic block size and the basic block number may include:
[0065] S1011, determining the space size of the shared memory pool based on the basic block size and the number of basic blocks.
[0066] This embodiment calculates the total space requirement of the shared memory pool according to the size of the basic block and the number of basic blocks, ensuring that the allocated shared memory pool can accommodate data exchange of all processes to avoid overflow or shortage.
[0067] S1012, determine whether it is the first creation.
[0068] In this embodiment, it is determined whether the current shared memory pool is created for the first time.
[0069] S1013: If it is the first creation, initialize the memory pool information at the starting position of the shared memory pool.
[0070] This embodiment initializes the memory pool information at the start position of the shared memory pool when the shared memory pool is created for the first time, including setting the initial state, management structure, etc., so that subsequent operations can be performed correctly.
[0071] S1014: If it is not the first creation, check whether the flag is a shared memory pool flag.
[0072] S1015: If it is not a shared memory pool identifier, determine whether it is newly created.
[0073] S1016, if it is a new creation, seize the process lock, increase the reference count, release the process lock, and determine that the target shared memory pool is created successfully.
[0074] The memory pool information in this embodiment is stored in the shared memory managed by this memory pool. It is a critical resource and must be accessed after obtaining the process lock. All processes obtain the same information. The information may include: initialization flag, process lock, reference count registration process linked list header and information required by the partner algorithm. In this embodiment, each process stores information about the process-specific shared memory pool in its memory space, stores information shared by the process shared memory pool in the shared memory space, and ensures secure access to critical areas through process locks. The embodiment of the present invention can ensure the correct creation and management of the shared memory pool through the above steps, thereby achieving efficient inter-process communication and data management.
[0075] For easier understanding, please refer to Figure 3 , Figure 3 A flow chart of creating a target shared memory pool is provided in an embodiment of the present invention. Figure 3 It can be seen that the process of creating the target shared memory pool may include applying to the system to create a shared memory named poolName, the space size = the size occupied by the memory pool information PoolInfo + the basic block size blockSize*(1<<the number of basic blocks logNumBlocks), << represents the address offset. Determine whether it is the first creation: If it is the first creation, initialize the memory pool information PoolInfo at the starting space of the shared memory pool (create a process lock, set the reference count to 1, set processHead (registered process list head) to empty, set the initial value of the partner algorithm record, etc.), seize the process lock, and finally set the flag to the shared memory pool identifier, release the lock, and set poolPtr (memory pool information pointer) to point to; if it has been created, check whether the flag is the shared memory pool identifier. If not, continue to determine whether it is a new creation. If so, seize the process lock, add 1 to the reference count, release the lock, set the basePtr (shared memory base pointer) value and poolPtr to point to, and return the information that the target shared memory pool has been successfully created.
[0076] S102, determining a target process corresponding to the target shared memory pool, and setting a data callback function corresponding to the target process; wherein the data callback function includes a function for processing events of data changes in the target shared memory pool.
[0077] The specific target process corresponding to the target shared memory pool is not limited in this embodiment. For example, the target process in this embodiment may be process A; or the target process in this embodiment may be process B. The data callback function in this embodiment refers to a function that is called and executed when a specific event occurs or a condition is met. For example, the data callback function in this embodiment may be that process B sends memory space to process A, and the data callback thread of process A is awakened and starts processing data.
[0078] It should be further explained that, in order to improve the accuracy of determining the data callback function, the above-mentioned determination of the target process corresponding to the target shared memory pool and setting the data callback function corresponding to the target process may include:
[0079] S1, seize the process lock, check whether the process information of the target process already exists in the registered process list; wherein the registered process list is a process list that records the registered data callback function; the process lock is to achieve the setting of mutually exclusive access configuration information.
[0080] In this embodiment, the process lock is seized to ensure that no other process interferes when checking and modifying the registered process list. The registered process list is a list that records all processes that have registered data callback functions. The process lock is used to achieve mutually exclusive access to these configuration information.
[0081] S2, if there is no process information, call the shared memory space function to apply for the space of the process information for setting the queue length; wherein, the parameter of the shared memory space function is the number of memory bytes to be allocated, the output is the shared memory space and the shared memory base pointer, and the memory pointer is obtained by calculating based on the shared memory space and the shared memory base pointer through the buddy algorithm.
[0082] S3, sets the name of the process information, the data change event, and initializes the data circular queue; wherein the data circular queue contains two fields: offset and data length.
[0083] S4, adding the process information to the registered process linked list; wherein the process linked list is a process including a data callback function.
[0084] S5, setting the process ID of the process information as the current process ID, and releasing the process lock.
[0085] S6: If there is process information, directly set the process ID of the process information as the current process ID and release the process lock.
[0086] S7, set the data callback function and start the data processing thread.
[0087] This embodiment describes the process of managing and using shared memory in a multi-process environment. This process involves multiple steps such as seizing and releasing process locks, checking and modifying the registered process list, applying for and initializing shared memory space, setting process information, adding processes to the list, setting process identifiers, and starting data processing threads. These steps are all to ensure that multiple processes can correctly share and use memory resources while ensuring data consistency and integrity. This embodiment is a process for registering a data callback function AddCallback, which may include: parameters: process name name, data callback function cb, data queue length dataCount. Execution process: a) Seize the process lock and check whether there is already process information of name in the registered process list. If not, continue execution. If so, jump to step d). b) Call the Alloc function (a function for obtaining shared memory space) to apply for a ProcessInfo (registered process information) space with a queue length of dataCount. Set the name and event (process event) of ProcessInfo, and initialize dataQueue (data circular queue). c) Add the applied ProcessInfo to the registered process list. d) Set the process id of ProcessInfo to the current process id and release the process lock (releasing the process lock means releasing or removing the lock on a resource so that other processes or threads can access and operate the resource). Set the callback (data callback function) in ProcessMemoryPool (target process shared memory pool) to the cb data callback function, start the data processing thread, and wait for the event event. See the flowchart corresponding to the above process. Figure 4 , Figure 4 A flowchart of a registration data callback function provided by an embodiment of the present invention.
[0088] S103, starting a data processing thread, waiting for a data change event triggered by other processes, triggering a data callback function based on the data change event, and realizing data communication between processes.
[0089] In this embodiment, when specific data change events occur, the data callback function will be triggered to process these events, which ensures that the system can respond to and process these events in a timely manner.
[0090] It should be further explained that, in order to release memory resources, the above-mentioned process communication method may also include: determining whether the reference count of the target shared memory pool is 0; wherein the reference count is the number of processes using the target shared memory pool; when the reference count of the target shared memory pool is 0, determining to destroy the target shared memory pool. In this embodiment, the target shared memory pool can be destroyed using a destroy function. It can be understood that the execution process of the destroy function Destroy includes: destroying the process shared memory pool, executing in the process lock, reducing the reference count by 1, determining whether the reference count is 0, and executing the destruction of the memory pool object when it is 0, that is, the shared memory is actually destroyed at the last destruction. This embodiment releases memory resources through the destruction operation.
[0091] It should be further explained that the above-mentioned process communication method may also include a process of canceling the data callback function RemoveCallback: seize the process lock, search for process information according to the process name, take out the process information, and release the process lock; stop the data consumption thread; wherein the data consumption thread refers to a thread specifically used to read, process and consume data; release the data in the data circular queue in sequence until it is empty, release the data change event; release the occupied space of the process information of the target process. In this embodiment, the data callback function RemoveCallback is cancelled: the parameter is the process name name. Execution process: a) seize the process lock, search for the process information ProcessInfo named name, take it out from the linked list, and release the lock; b) stop the data consumption thread (Data Consumer Thread): the data consumption thread refers to a thread specifically used to read, process and consume data. It usually corresponds to the data production thread, which is responsible for generating or providing data, and sets callback to empty. c) release (Free) the data in the data circular queue dataQueue in sequence until it is empty, and release the event event. d) release (Free) the occupied space of ProcessInfo.
[0092] It should be further explained that the above-mentioned process communication method may also include: receiving a process name, a data pointer and a data length; calculating an offset based on the data pointer and a shared memory base pointer; seizing the process lock, searching for process information corresponding to the process name, and returning an error if the information is not found; if the information is found, adding data to the data circular queue, setting the offset and data length; triggering a data change event. The above-mentioned process in this embodiment is a process of sending data between processes, and the input parameters are: receiving the process name processName, the data pointer ptr, and the data length len. Execution process: a) Calculate the offset offset using the data pointer ptr and the shared memory base pointer basePtr; b) Seize the process lock, search for the process information ProcessInfo named name, and return an error if the information is not found. If the information is found, continue. c) Add data to the data circular queue and set it to offset and len. d) Trigger event. Return success.
[0093] It should be further explained that the above-mentioned process communication method may also include: the data consumption thread execution process: a) blocking and waiting for event (event object), and continuing after success. b) judging whether it has ended, if yes, directly ending the thread loop. If no, continuing. c) taking a to-be-processed data from the data queue. judging whether the acquisition is successful, if successful, continuing; if there is no data, jumping to a). d) calculating the offset offset in the data and the shared memory base pointer basePtr, and converting them into a pointer. calling the data callback function, the parameters are the data pointer and the data length. after the function returns, releasing (Free) the pointer data. jumping to c). after the embodiment of the present invention obtains the data, the consumption thread will convert it into an appropriate format as needed (such as obtaining the actual data pointer by calculating the offset and the base pointer). then, it will call a data callback function to process the data, and this function usually receives the data pointer and the data length as parameters. after the processing is completed, releasing the memory allocated to the data for reuse. the above process will be repeated continuously during the life cycle of the consumption thread until the termination condition is met. this loop mechanism allows the consumption thread to continuously process multiple data items, thereby improving the throughput and efficiency of the system.
[0094] It should be further explained that in order to improve the accuracy of data communication, the operations performed in the target shared memory pool may include: first, create a data distribution table: create a data structure for recording the information of each process receiving data. This data structure is usually a mapping table or hash table, where the key is the process name or process ID, and the value is information related to the process, such as events and data queues. This mapping table allows the system to quickly find relevant information about a specific process, thereby achieving efficient inter-process communication. Second, set process events: set a unique event for each process receiving data. An event can be a semaphore, mutex, or other synchronization mechanism used to control the process's access to data. When new data arrives, the process can be notified to process it by triggering the corresponding event. Third, create a data queue: create a data queue for each process receiving data. The data queue is used to store data waiting to be processed to ensure the orderliness and integrity of the data. The data queue can be implemented based on a linked list, a circular array, or other data structures, depending on application requirements and performance considerations. Fourth, control the process to process the data in the data queue through events: when new data arrives, first add the data to the data queue of the corresponding process. Then, trigger the event associated with the process to notify the process that there is new data to be processed. After the process is awakened, it takes data from its data queue for processing. After the processing is completed, the status in the shared memory can be updated or other operations can be performed as needed. In general, by establishing a process-corresponding data distribution table in the shared memory pool and setting an event and data queue for each process receiving data, efficient inter-process communication and data management can be achieved. This method utilizes the high-speed access characteristics of shared memory and the synchronization mechanism of events to ensure data consistency and security.
[0095] For ease of understanding, the steps of using the process shared memory pool in the embodiment of the present invention are described as follows:
[0096] 1) Process A creates a shared memory pool Make() and registers the callback function AddCallback() of this process.
[0097] The shared memory pool in this embodiment is Make().
[0098] 2) Process B applies for memory space Alloc() from the shared memory pool and writes data to the memory space.
[0099] The Alloc() function in this embodiment is used to obtain the shared memory space Alloc: the parameter is the number of bytes of memory to be allocated; and the memory pointer obtained by operating the shared memory space applied for by the buddy algorithm with basePtr (shared memory base pointer) is returned.
[0100] 3) Process B sends the memory space to process A and calls Send to trigger the event Event (process name, memory space pointer, memory space data length).
[0101] The Send function in this embodiment is a function for sending data: parameters: receiving process name processName, data pointer ptr, data length len. Execution process: a) Calculate the offset offset by using the data pointer ptr and the shared memory base pointer basePtr; b) Seize the process lock and search for the process information ProcessInfo named name. If it cannot be found, return an error. If it is found, continue. c) Add data to the data circular queue and set it to offset and len. d) Trigger event. Return success.
[0102] 4) Process A triggers the data callback function based on event Event, and the data callback thread is awakened to start processing data.
[0103] In this embodiment, process B writes data to the shared memory and generates an event based on the Send method. Process A consumes the event and calls the data callback function. After writing, process A is notified that the event can be used.
[0104] The following is an introduction to a process communication apparatus provided in an embodiment of the present invention. The process communication apparatus described below and the process communication method described above may refer to each other.
[0105] Figure 5 A schematic diagram of the structure of a process communication device provided by an embodiment of the present invention may include:
[0106] A shared memory pool determination module 100 is used to determine a target shared memory pool based on a basic block size and a basic block number;
[0107] The data callback function determination module 200 is used to determine the target process corresponding to the target shared memory pool and set the data callback function corresponding to the target process; wherein the data callback function includes a function for processing events of data changes in the target shared memory pool;
[0108] The communication module 300 is used to start a data processing thread, wait for a data change event triggered by other processes, trigger the data callback function based on the data change event, and realize data communication between processes.
[0109] It should be further explained that the above-mentioned shared memory pool determination module 100 may include:
[0110] A space size determination module, used to determine the space size of the shared memory pool based on the basic block size and the number of basic blocks;
[0111] A judgment module is used to judge whether it is the first creation;
[0112] A memory pool information initialization module is used to initialize the memory pool information at the start position of the shared memory pool if it is created for the first time;
[0113] An identification determination module is used to check whether the flag is a shared memory pool flag if it is not created for the first time;
[0114] If it is not a shared memory pool identifier, determine whether it is newly created;
[0115] The target shared memory pool creation module is used to seize the process lock, increase the reference count, release the process lock, and determine that the target shared memory pool is successfully created if it is newly created.
[0116] Further, based on any of the above embodiments, the above process communication device may further include:
[0117] A reference count judgment module, used to judge whether the reference count of the target shared memory pool is 0; wherein the reference count is the number of processes using the target shared memory pool;
[0118] The target shared memory pool destruction module is used to determine to destroy the target shared memory pool when the reference count of the target shared memory pool is 0.
[0119] Further, based on any of the above embodiments, the above process communication device may further include:
[0120] A process lock release module is used to seize the process lock, search for process information according to the process name, retrieve the process information, and release the process lock;
[0121] A data consumption thread stopping module is used to stop a data consumption thread; wherein the data consumption thread refers to a thread specifically used to read, process and consume data;
[0122] Release data module, used to release the data in the data loop queue in sequence until it is empty, and release the data change event;
[0123] The occupied space releasing module is used to release the occupied space of the process information of the target process.
[0124] Further, based on any of the above embodiments, the data callback function determination module 200 may include:
[0125] A process information judgment unit, used to seize the process lock and check whether the process information of the target process already exists in the registered process linked list; wherein the registered process linked list is a process linked list recording the registered data callback function; and the process lock is a setting for realizing mutually exclusive access configuration information;
[0126] A space application unit, used for calling a shared memory space function to apply for space for process information of a set queue length if the process information does not exist; wherein the parameter of the shared memory space function is the number of memory bytes to be allocated, the output is a shared memory space and a shared memory base pointer, and a memory pointer is obtained by performing calculation based on the shared memory space and the shared memory base pointer through a buddy algorithm;
[0127] A data circular queue determination unit, used to set the name of the process information, the data change event, and initialize the data circular queue; wherein the circular queue includes two fields: an offset and a data length;
[0128] A registered process linked list determining unit, used to add process information to a registered process linked list; wherein the process linked list is a process including a data callback function;
[0129] A process identification determination unit, used to set the process identification of the process information as the current process identification and release the process lock;
[0130] A process identifier setting unit, configured to directly set the process identifier of the process information as the current process identifier if the process information exists, and release the process lock;
[0131] The data processing thread starting unit is used to set the data callback function and start the data processing thread.
[0132] Further, based on any of the above embodiments, the above process communication device may further include:
[0133] An information receiving unit, used for receiving a process name, a data pointer and a data length;
[0134] an offset determination unit, configured to calculate an offset based on the data pointer and a shared memory base pointer;
[0135] The error unit is returned, which is used to seize the process lock and find the process information corresponding to the process name. If it cannot be found, an error is returned;
[0136] A data adding unit is used to add data to the data circular queue if found, and to set the offset and data length;
[0137] The data change event determination unit is used to trigger a data change event.
[0138] Further, based on any of the above embodiments, the information stored in the target shared memory pool includes a shared memory base pointer, a memory pool information pointer and a data processing callback function pointer, and the data callback function pointer points to a callback function registered by the target process.
[0139] It should be noted that the order of the modules and units in the above-mentioned process communication device can be changed without affecting the logic.
[0140] Figure 5 The description of the features in the corresponding embodiments can be found in Figure 5 The relevant descriptions of the corresponding embodiments will not be repeated here one by one.
[0141] The process communication device provided by the embodiment of the present invention may include: a shared memory pool determination module 100, which is used to determine the target shared memory pool based on the basic block size and the number of basic blocks; a data callback function determination module 200, which is used to determine the target process corresponding to the target shared memory pool and set the data callback function corresponding to the target process; wherein the data callback function includes a function for processing the event of data change in the target shared memory pool; and a communication module 300, which is used to start the data processing thread, wait for the data change event triggered by other processes, and trigger the data callback function based on the data change event to realize the communication of data between processes. Compared with the current inability to synchronize data between processes in a timely manner, the present invention sets the data callback function corresponding to the target process, so that a quick response can be made to the change of data based on the data callback function without constantly polling the memory status, thereby improving the efficiency of communication between processes.
[0142] A process communication device provided by an embodiment of the present invention is introduced below. The process communication device described below and the process communication method described above can refer to each other.
[0143] Figure 6 A schematic diagram of a process communication device provided by an embodiment of the present invention is shown in FIG. Figure 6 As shown, the process communication device may include: a memory 60 for storing a computer program;
[0144] The processor 61 is used to implement the steps of the process communication method in the above embodiment when executing a computer program.
[0145] The process communication device provided in this embodiment may include but is not limited to a smart phone, a tablet computer, a laptop computer, or a desktop computer.
[0146] Among them, the processor 61 may include one or more processing cores, such as a 4-core processor, an 8-core processor, etc. The processor 61 can be implemented in at least one hardware form of digital signal processing (DSP), field-programmable gate array (FPGA), and programmable logic array (PLA). The processor 61 may also include a main processor and a coprocessor. The main processor is a processor for processing data in the awake state, also known as a central processing unit (CPU); the coprocessor is a low-power processor for processing data in the standby state. In some embodiments, the processor 61 may be integrated with a graphics processing unit (GPU), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 61 may also include an artificial intelligence (AI) processor, which is used to process computing operations related to machine learning.
[0147] The memory 60 may include one or more computer-readable storage media, which may be non-transitory. The memory 60 may also include a high-speed random access memory, and a non-volatile memory, such as one or more disk storage devices, flash memory storage devices. In this embodiment, the memory 60 is at least used to store the following computer program 601, wherein the computer program, after being loaded and executed by the processor 61, can implement the relevant steps of the method of process communication disclosed in any of the aforementioned embodiments. In addition, the resources stored in the memory 60 may also include an operating system 602 and data 603, etc., and the storage method may be temporary storage or permanent storage. Among them, the operating system 602 may include Windows, Unix, Linux, etc. Data 603 may include, but is not limited to, data required for process communication, etc.
[0148] In some embodiments, the process communication device may also include a display screen 62 , an input and output interface 63 , a communication interface 64 , a power supply 65 , and a communication bus 66 .
[0149] Those skilled in the art will understand that Figure 6 The structure shown in the figure does not constitute a limitation on the device for process communication, and may include more or less components than shown in the figure.
[0150] It is understandable that if the process communication method in the above embodiment is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the current technology, or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium to execute all or part of the steps of the methods of each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), electrically erasable programmable ROM, register, hard disk, removable disk, CD-ROM, magnetic disk or optical disk, etc. Various media that can store program codes.
[0151] Based on this, an embodiment of the present invention further provides a computer-readable storage medium, 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 above are implemented.
[0152] Based on this, an embodiment of the present invention further provides a computer program product, including a computer program / instruction, which implements the steps of the above-mentioned data communication method when executed by a processor.
[0153] The above is a detailed introduction to a process communication method provided by an embodiment of the present invention. The various embodiments in the specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same and similar parts between the various embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the method part description.
[0154] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0155] The above is a detailed introduction to a process communication method, device, equipment and computer-readable storage medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. It should be pointed out that for ordinary technicians in this technical field, without departing from the principle of the present invention, several improvements and modifications can be made to the present invention, and these improvements and modifications also fall within the scope of protection of the claims of the present invention.
Claims
1. A process communication method, characterized in that: include: Determine a target shared memory pool based on the basic block size and the number of basic blocks; Determine a target process corresponding to the target shared memory pool, and set a data callback function corresponding to the target process; wherein the data callback function includes a function for processing an event in which data in the target shared memory pool changes; The data processing thread is started, and waits for data change events triggered by other processes, so as to trigger the data callback function based on the data sending change events, thereby realizing data communication between processes.
2. The process communication method according to claim 1, characterized in that: The step of determining the target shared memory pool based on the basic block size and the number of basic blocks includes: Determine the space size of the shared memory pool based on the basic block size and the number of basic blocks; Determine whether it is the first creation; If it is the first creation, initialize the memory pool information at the starting position of the shared memory pool; If it is not the first creation, check whether the flag is the shared memory pool flag; If it is not a shared memory pool identifier, determine whether it is newly created; If it is newly created, the process lock is seized, the reference count is increased, the process lock is released, and it is determined that the target shared memory pool is successfully created.
3. The process communication method according to claim 1, characterized in that: The process communication method further includes: Determine whether the reference count of the target shared memory pool is 0; wherein the reference count is the number of processes using the target shared memory pool; When the reference count of the target shared memory pool is 0, it is determined to destroy the target shared memory pool.
4. The process communication method according to any one of claims 1 to 3, characterized in that: The process communication method further includes: Seize the process lock, search for process information according to the process name, retrieve the process information, and release the process lock; Stop the data consumption thread; wherein the data consumption thread refers to a thread specifically used to read, process and consume data; Release the data in the data loop queue in sequence until it is empty, and release the data change event; Release the space occupied by the process information of the target process.
5. The process communication method according to claim 1, characterized in that: Determining a target process corresponding to the target shared memory pool and setting a data callback function corresponding to the target process include: Seize the process lock, check whether the process information of the target process already exists in the registered process linked list; wherein the registered process linked list is a process linked list recording the registered data callback function; the process lock is to achieve the setting of mutually exclusive access configuration information; If the process information does not exist, calling a shared memory space function to apply for space for process information of setting queue length; wherein the parameter of the shared memory space function is the number of memory bytes to be allocated, the output is a shared memory space and a shared memory base pointer, and a memory pointer is obtained by calculating based on the shared memory space and the shared memory base pointer through a buddy algorithm; Set the name of the process information, the data change event, and initialize the data circular queue; wherein the data circular queue contains two fields: offset and data length; Add the process information to the registered process list; wherein the process list is a process including a data callback function; Set the process ID of the process information to the current process ID and release the process lock; If the process information exists, directly set the process ID of the process information as the current process ID and release the process lock; Set the data callback function and start the data processing thread.
6. The process communication method according to claim 1, characterized in that: The process communication method further includes: Receive process name, data pointer and data length; Calculate an offset based on the data pointer and the shared memory base pointer; Seize the process lock and search for the process information corresponding to the process name. If no information is found, an error is returned. If found, add data to the data loop queue and set the offset and data length; Trigger a data change event.
7. The process communication method according to claim 1, characterized in that: The information stored in the target shared memory pool includes a shared memory base pointer, a memory pool information pointer and a data processing callback function pointer, and the data callback function pointer points to a callback function registered by the target process.
8. A process communication method, characterized in that: include: A shared memory pool determination module, used for determining a target shared memory pool based on a basic block size and a basic block number; A data callback function determination module is used to determine the target process corresponding to the target shared memory pool and set the data callback function corresponding to the target process; wherein the data callback function includes a function for processing events of data changes in the target shared memory pool; The communication module is used to start the data processing thread, wait for the data change event triggered by other processes, trigger the data callback function based on the data change event, and realize the communication of data between processes.
9. A process communication device, characterized in that: include: Memory for storing computer programs; A processor, configured to execute the computer program to implement the steps of the process communication method as claimed in any one of claims 1 to 7.
10. 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 7 are implemented.
Citation Information
Patent Citations
Cross-process message notification method and device based on shared memory
CN114490110A
Inter-process communication method and apparatus, computer device and computer-readable medium
US20220309006A1
Cited By
Data real-time statistical method and system
CN121614351A
A data real-time statistics method and system
CN121614351B