CN104424030B - Method and device for sharing memory by multi-process operation - Google Patents

Method and device for sharing memory by multi-process operation Download PDF

Info

Publication number
CN104424030B
CN104424030B CN201310370343.2A CN201310370343A CN104424030B CN 104424030 B CN104424030 B CN 104424030B CN 201310370343 A CN201310370343 A CN 201310370343A CN 104424030 B CN104424030 B CN 104424030B
Authority
CN
China
Prior art keywords
data
data area
shared memory
node
linked list
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Active
Application number
CN201310370343.2A
Other languages
Chinese (zh)
Other versions
CN104424030A (en
Inventor
赵文
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Tencent Technology Shenzhen Co Ltd
Original Assignee
Tencent Technology Shenzhen Co Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Tencent Technology Shenzhen Co Ltd filed Critical Tencent Technology Shenzhen Co Ltd
Priority to CN201310370343.2A priority Critical patent/CN104424030B/en
Publication of CN104424030A publication Critical patent/CN104424030A/en
Application granted granted Critical
Publication of CN104424030B publication Critical patent/CN104424030B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The invention provides a method for sharing a memory by multi-process operation, which comprises the following steps: when a first process needs to modify data in a first shared memory, copying the data in the first shared memory to a pre-allocated second shared memory with the same size as the first shared memory; the first process modifies the data in the second shared memory; and after the data in the second shared memory is modified, modifying the address of the stored current data area from the address of the first shared memory to the address of the second shared memory. The method can enable the read operation and the write operation of the shared memory to be executed concurrently, thereby improving the processing efficiency of the equipment. In addition, a device for sharing the memory by multi-process operation and another method and device for sharing the memory by multi-process operation are also provided.

Description

Method and device for sharing memory by multi-process operation
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for sharing a memory by multi-process operations.
Background
Shared memory refers to memory that can be shared by more processes, which is described as a mapping of one region (segment) of memory. A process is a running instance of an application and is a dynamic execution of the application. The shared memory is operated by multiple processes, namely, data in the shared memory is read or written into the shared memory, and the multiple processes can realize communication among the processes through the shared memory.
In the conventional technology, in order to maintain the accuracy of memory data, a resource lock is required to be used in the multi-process operation shared memory, that is: before reading data in the shared memory or writing data into the shared memory, a process needs to acquire a resource lock of the shared memory, only the process acquiring the resource lock can perform read operation or write operation on the shared memory, and the process incapable of acquiring the resource lock is blocked.
However, the inventors found that at least the following technical problems exist in the prior art:
since the processes are blocked when the resource lock cannot be acquired, when one process performs a read operation on the shared memory (the process acquires the resource lock), all other processes cannot perform the read operation or the write operation on the shared memory, and when one process performs the write operation on the shared memory, all other processes cannot perform the read operation or the write operation on the shared memory, which inevitably affects the processing efficiency of the device. Especially in the application scenario of single-process write and multi-process read (i.e. the number of read operations is much greater than the number of write operations), the serial processing mode makes the processing efficiency of the device low, thereby affecting the processing performance of the device.
Disclosure of Invention
Therefore, it is necessary to provide a method and an apparatus for sharing a memory by multi-process operation, aiming at the technical problem of low processing efficiency in the conventional technology.
A method of sharing memory for multi-process operations, the method comprising:
when a first process needs to modify data in a first shared memory, copying the data in the first shared memory to a pre-allocated second shared memory with the same size as the first shared memory;
the first process modifies the data in the second shared memory;
and after the data in the second shared memory is modified, modifying the address of the stored current data area from the address of the first shared memory to the address of the second shared memory.
An apparatus for sharing memory for multi-process operations, the apparatus comprising:
the data copying module is used for copying the data in the first shared memory to a pre-allocated second shared memory with the same size as the first shared memory when a first process needs to modify the data in the first shared memory;
the data modification module is used for modifying the data in the second shared memory through a first process;
and the address modification module is used for modifying the stored address of the current data area from the address of the first shared memory to the address of the second shared memory after the data in the second shared memory is modified.
According to the method and the device for operating the shared memory by the multiple processes, when the first process needs to modify the data in the first shared memory, the data in the first shared memory is copied to the pre-allocated second shared memory with the same size as the first shared memory, so that the data writing of the first process can be performed in the second shared memory. In this way, during the execution of the write operation of the first process, the second process can still read the data before modification, and thus the process of reading the data is not blocked. Because the resource lock is not set for the shared memory, the process can realize read operation and write operation on the shared memory without competing for resources, the process of the read operation cannot be blocked because the process executes the write operation, and the process of the write operation cannot be blocked because the process executes the read operation, so that the processing efficiency of the equipment can be improved, and the processing performance of the equipment can be improved.
A method of sharing memory for multi-process operations, the method comprising:
when a first process needs to modify data in a first data area corresponding to a first node in a linked list, writing the data into an idle available second data area;
after the data writing is finished, judging whether the reading operation of the second process is finished, if so, setting the linked list to be in an unreadable state, otherwise, waiting until the reading operation of the second process is finished;
inserting the second data area into the linked list to enable the first node to correspond to the second data area;
setting the linked list to a readable state.
An apparatus for sharing memory for multi-process operations, the apparatus comprising:
the data writing module is used for writing data into a second data area which has the same size as the first data area and is available in a free state when the first process needs to modify the data in the first data area corresponding to the first node in the linked list;
the read operation judging module is used for judging whether the read operation of the second process is finished or not after the data write-in module finishes the data write-in;
a state setting module, configured to set the linked list to an unreadable state when the reading operation of the second process is finished, and otherwise, wait until the reading operation of the second process is finished, and set the linked list to the unreadable state;
a linked list updating module, configured to insert the second data area into the linked list, so that the first node corresponds to the second data area;
the state setting module is further configured to set the linked list to a readable state after the linked list updating module inserts the second data area into the linked list.
According to the method and the device for operating the shared memory by the multiple processes, when the first process needs to modify the data in the first data area corresponding to the first node in the linked list, the data is written into the idle available second data area, and because the write operation of the first process can be executed in the second data area, the read operation of the second process cannot be influenced, the write operation and the read operation of the shared memory can be synchronously executed, and the process for reading the data cannot be blocked due to the write operation of the first process. Because the resource lock is not set for the shared memory, the process can realize read operation and write operation on the shared memory without competing resources, the process of the read operation cannot be blocked because the process executes the write operation, and the process of the write operation cannot be blocked because the process executes the read operation, so that the concurrent execution of the read operation and the write operation is realized, and the processing efficiency of the equipment is improved.
Drawings
FIG. 1 is a flowchart illustrating a method for operating a shared memory by multiple processes according to an embodiment;
FIG. 2 is a diagram illustrating allocation of shared memory;
FIG. 3 is a diagram illustrating a pointer pointing to a current data area;
FIG. 4 is a diagram of changing a pointer to a current data area;
FIG. 5 is a block diagram of an apparatus for sharing memory for multi-process operations according to one embodiment;
FIG. 6 is a block diagram of an apparatus for sharing memory for multi-process operations according to another embodiment;
FIG. 7 is a block diagram of an apparatus for sharing memory for multi-process operations according to yet another embodiment;
FIG. 8 is a flowchart illustrating a method for operating a shared memory by multiple processes according to another embodiment;
FIG. 9 is a diagram of a linked list used to manage shared memory in one embodiment;
FIG. 10 is a diagram of a linked list used to manage shared memory in another embodiment;
FIG. 11 is a block diagram illustrating an apparatus for sharing memory for multi-process operations according to an embodiment;
FIG. 12 is a block diagram of an apparatus for sharing memory for multi-process operations according to yet another embodiment;
FIG. 13 is a flowchart illustrating a method for operating a shared memory by multiple processes according to another embodiment.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
As shown in fig. 1, in an embodiment, a method for sharing a memory by multiple processes is provided, which includes:
step 102, when the first process needs to modify the data in the first shared memory, copying the data in the first shared memory to a pre-allocated second shared memory with the same size as the first shared memory.
The first shared memory and the second shared memory are pre-allocated shared memories with equal size. In this embodiment, when a certain size of shared memory is needed, twice the size of the shared memory is allocated, where half of the shared memory is the first shared memory, and the other half of the shared memory is the second shared memory.
In one embodiment, before step 102, the method further comprises: when a first-size shared memory is needed, allocating a first shared memory with the first size and a second shared memory with the same size as the first shared memory; creating a third shared memory, and storing the address of the first shared memory, the address of the second shared memory and the address of the current data area in the third shared memory; the address of the current data area is the address of the first shared memory.
As shown in fig. 2, when the shared memory a with size X is needed, the shared memories a and B with size 2 × X are allocated. In the embodiment of the present invention, the first shared memory may be any one of the two shared memories, and the second shared memory is the other shared memory, that is, the first shared memory and the second shared memory are relative concepts. After the shared memory is allocated, the data may be written into any one of the shared memories, in this embodiment, the first shared memory is taken as the shared memory of the current data area for example, and when the first process needs to modify the data in the current data area, step 102 is executed.
The third shared memory is used for managing the first shared memory and the second shared memory. Further, three pointers may be stored in the third shared memory, where the three pointers respectively point to the address of the first shared memory, the address of the second shared memory, and the address of the current data area. The address of the first shared memory may be a starting location of the first shared memory, and the address of the second shared memory may be a starting location of the second shared memory. In this embodiment, since the current data area is the first shared memory, the address of the current data area is the starting position of the first shared memory.
Specifically, in step 102, when the first process needs to modify data in the current data area (that is, data in the first shared memory), an address of the first shared memory and an address of the second shared memory stored in the third shared memory are obtained; and reading the data in the first shared memory according to the acquired address of the first shared memory, and writing the read data in the first shared memory into the second shared memory according to the acquired address of the second shared memory.
Step 104, the first process modifies the data in the second shared memory.
When the first process needs to modify the data in the first shared memory, the first process can modify the data in the second shared memory because the data in the first shared memory is copied to the second shared memory. For example, after 3 data in the first shared memory and the 2 nd data are required to be modified, and the 3 data are copied into the second shared memory, the first process may directly modify the 2 nd data in the second shared memory, and write the information required to be modified into the 2 nd data in the second shared memory.
And step 106, after the data in the second shared memory is modified, modifying the stored address of the current data area from the address of the first shared memory to the address of the second shared memory.
In one embodiment, before the data in the second shared memory is modified, the method further includes: and the second process acquires the stored address of the current data area as the address of the first shared memory and reads the data in the first shared memory according to the address of the first shared memory.
The second process is also a relative concept with respect to the first process, and the second process is one or more processes different from the first process, which need to read data in the current data area. In this embodiment, when the second process needs to read the data in the current data area, the address of the current data area may be obtained from the third shared memory, as described above, the address of the current data area is the address of the first shared memory, and the address of the current data area obtained by the second process is the address of the first shared memory, and the data in the first shared memory may be read according to the address. In this way, before the data in the second shared memory is modified, the data in the first shared memory (i.e., the old data before modification) is still read by the second process.
Further, after step 106, the method further includes: and the second process acquires the stored address of the current data area as the address of the second shared memory and reads the data in the second shared memory according to the address of the second shared memory.
In this embodiment, after the first process finishes modifying the data in the second shared memory, the first process may modify the address of the current data area stored in the third shared memory to the address of the second shared memory. As shown in fig. 3 and 4, the first shared memory is a shared memory a, the second shared memory is a shared memory B, before the data in the shared memory B is modified, the pointer pointing to the current data area stored in the third shared memory points to the shared memory a (see fig. 3), and after the data in the shared memory B is modified, the pointer pointing to the current data area stored in the third shared memory points to the shared memory B (see fig. 4), so that the address of the current data area is changed to the address of the new data area. Furthermore, when modifying the pointer pointing to the current data area in the third shared memory, protection can be added to the pointer to prevent the program from generating problems when modifying the pointer.
And then, when the second process needs to read the data in the current data area, the address of the current data area can be obtained from the third shared memory, and because the address of the current data area in the third shared memory is modified into the address of the second shared memory at this time, the second process can read the data in the second shared memory according to the obtained address of the current data area. Thus, after the data in the second shared memory is modified, the second process reads the data in the second shared memory (i.e., the modified new data).
As shown in fig. 5, in an embodiment, there is further provided an apparatus for sharing a memory by multiple processes, including:
the data copying module 502 is configured to copy data in the first shared memory to a pre-allocated second shared memory having a size equal to that of the first shared memory when the first process needs to modify the data in the first shared memory.
A data modification module 504, configured to modify data in the second shared memory through the first process.
An address modification module 506, configured to modify the stored address of the current data area from the address of the first shared memory to the address of the second shared memory after the data in the second shared memory is modified.
The first shared memory and the second shared memory are pre-allocated shared memories with equal size. When a certain size of shared memory is needed, double-size shared memory is pre-allocated, wherein one half of the shared memory is a first shared memory, and the other half of the shared memory is a second shared memory. In this embodiment, the current data area is taken as the first shared memory for example.
In an embodiment, as shown in fig. 6, on the basis of the embodiment shown in fig. 5, the apparatus for multi-process operation to share a memory further includes:
the memory allocation module 501 is configured to, when a first size of a shared memory is needed, allocate the first shared memory of the first size and a second shared memory that is equal to the first shared memory in size.
A third shared memory creating module 503, configured to create a third shared memory, and store the address of the first shared memory, the address of the second shared memory, and the address of the current data area in the third shared memory; the address of the current data area is the address of the first shared memory.
In this embodiment, the third shared memory is used to manage the first shared memory and the second shared memory. Further, three pointers may be stored in the third shared memory, where the three pointers respectively point to the address of the first shared memory, the address of the second shared memory, and the address of the current data area. The address of the first shared memory may be a starting location of the first shared memory, and the address of the second shared memory may be a starting location of the second shared memory. In this embodiment, since the current data area is the first shared memory, the address of the current data area is the starting position of the first shared memory.
Further, in an embodiment, the data copying module 502 is configured to obtain an address of the first shared memory and an address of the second shared memory stored in the third shared memory; and reading the data in the first shared memory according to the acquired address of the first shared memory, and writing the read data in the first shared memory into the second shared memory according to the acquired address of the second shared memory.
When the first process needs to modify the data in the first shared memory, since the data copying module 502 copies the data in the first shared memory to the second shared memory, the data modifying module 504 can modify the data in the second shared memory through the first process.
Further, in an embodiment, as shown in fig. 7, on the basis of the embodiment shown in fig. 6, the apparatus for operating a shared memory by multiple processes further includes:
a data reading module 508, configured to, before the data in the second shared memory is modified, obtain, through the second process, an address of the stored current data area as an address of the first shared memory, and read the data in the first shared memory according to the address of the first shared memory; and after the data in the second shared memory is modified, the second process is used for acquiring the stored address of the current data area as the address of the second shared memory, and reading the data in the second shared memory according to the address of the second shared memory.
The second process is one or more processes different from the first process, which need to read the data in the current data area. In this embodiment, when the second process needs to read the data in the current data area, the data reading module 508 may first obtain the address of the current data area from the third shared memory, as described above, the address of the current data area is the address of the first shared memory, and then the address of the current data area obtained by the data reading module 508 through the second process is the address of the first shared memory, and the data in the first shared memory may be read according to the address. In this way, before the data in the second shared memory is modified, the data in the first shared memory (i.e., the old data before modification) is still read by the second process.
Further, the address modification module 506 may be configured to modify the address of the current data area stored in the third shared memory to the address of the second shared memory after the data modification module 504 modifies the data in the second shared memory through the first process, so that the address of the current data area is changed to the address where the new data is located.
When the second process needs to read the data in the current data area, the data reading module 508 may be configured to obtain the address of the current data area from the third shared memory, and since the address of the current data area in the third shared memory is modified to the address of the second shared memory at this time, the data reading module 508 may read the data in the second shared memory according to the obtained address of the current data area through the second process. Thus, after the data in the second shared memory is modified, the second process reads the data in the second shared memory (i.e., the modified new data).
According to the method and the device for operating the shared memory by the multiple processes, when the first process needs to modify the data in the first shared memory, the data in the first shared memory is copied to the pre-allocated second shared memory with the same size as the first shared memory, so that the data writing of the first process can be performed in the second shared memory. In this way, during the execution of the write operation of the first process, the second process can still read the data before modification, and thus the process of reading the data is not blocked. And after the data in the second shared memory is modified, modifying the address of the stored current data area from the address of the first shared memory to the address of the second shared memory, so that after the data writing is finished, a process of reading the data can read new data. Therefore, the method and the device for operating the shared memory by multiple processes can improve the processing efficiency of the equipment, thereby improving the processing performance of the equipment. In addition, because the resource lock is not set for the shared memory, the process can realize read operation or write operation on the shared memory without competing for resources, the process of the read operation cannot be blocked because the process executes the write operation, and the process of the write operation cannot be blocked because the process executes the read operation, so that the concurrent execution of the read operation and the write operation is realized.
As shown in fig. 8, in an embodiment, another method for sharing a memory by multiple processes is provided, which includes:
step 802, when the first process needs to modify the data in the first data area corresponding to the first node in the linked list, the data is written into the idle available second data area.
Step 804, after the data writing is completed, judging whether the reading operation of the second process is finished, if so, entering step 806, otherwise, waiting, returning to step 804 until the reading operation of the second process is finished.
Step 806 sets the linked list to an unreadable state.
Step 808, inserting the second data area into the linked list, so that the first node corresponds to the second data area.
Step 810 sets the linked list to a readable state.
In this embodiment, the shared memory is managed by a linked list, each node in the linked list corresponds to one data area in the shared memory, and the data areas corresponding to the nodes form the shared memory. Fig. 9 is a schematic diagram of a linked list including 3 nodes, where the linked list is composed of a header and a plurality of nodes corresponding to data fields, and each node includes a forward pointer (prev) and a backward pointer (next), where the forward pointer points to the start position of the data field corresponding to the previous node and the backward pointer points to the start position of the data field corresponding to the next node. Taking node 2 in fig. 9 as an example, the forward pointer of node 2 points to the start position of data area 1 corresponding to node 1, the backward pointer of node 2 points to the start position of data area 3 corresponding to node 3, and the address of data area 1 and the address of data area 2 can be obtained according to the forward pointer and backward pointer in node 2.
In this embodiment, the first node is a node corresponding to the data area that needs to be modified by the first process, and when the first process needs to modify the data in the first data area corresponding to the first node in the linked list, a new idle available second data area is created. Preferably, the second data area is equal in size to the first data area. The first process then writes the modified data to the second data area. And after the data writing is finished, judging whether the reading operation of the second process is finished, and if so, setting the linked list to be in an unreadable state. Specifically, a flag bit may be set for the shared memory to indicate that the shared memory is unreadable, so that the process cannot perform a read operation or a write operation on the shared memory. Further, after the linked list is set to be in the unreadable state, a second data area can be inserted into the linked list, so that the first node corresponds to the second data area.
As shown in fig. 9, when the first process needs to modify the data in the data area 2 corresponding to the node 2, the modified data is written into the second data area, and after the data writing is completed and the reading operation of the second process is completed, the linked list is set to be unreadable, and the pointer information of the node 1 and the node 3 is modified, so that the backward pointer of the node 1 points to the start position of the second data area, and the forward pointer of the node 3 points to the start position of the second data area, so that the second data area is added into the linked list, and the node 2 corresponds to the second data area.
And after the node information in the linked list is updated, setting the linked list to be in a readable state. In one embodiment, when the linked list is in a readable state, the second process obtains an address in the data area corresponding to the node according to the pointer information of the node in the linked list, and reads data in the data area corresponding to the node according to the obtained address. In this embodiment, the second process is one or more processes that need to read data in the current data area, which is different from the first process. And when the linked list is in a readable state, reading the data in the shared memory according to the linked list by the second process.
In one embodiment, the method for sharing memory by multi-process operation further comprises: when the second process reads the data in the data area corresponding to the node, the reference count corresponding to the node is increased progressively; and after the second process finishes reading the data in the data area corresponding to the node, decrementing the reference count corresponding to the node. In this embodiment, each node further includes a corresponding reference count, and when a process reads data in the data area corresponding to the node, the reference count is incremented, and after the reading is completed, the reference count is incremented. As shown in FIG. 10, the reference count may be stored in a node.
Further, in an embodiment, the determining whether the read operation of the second process is finished includes: and judging whether the reference count corresponding to the node is a preset decreased numerical value or not, if so, judging that the reading operation of the second process on the data area corresponding to the node is finished, otherwise, judging that the reading operation of the second process on the data area corresponding to the node is not finished.
Preferably, an initial value of the reference count corresponding to each node may be set to be 0, when a process reads data in the data area corresponding to the node, the reference count is incremented by 1, and when the reading is completed, the reference count is decremented by 1. In this way, whether the reference count corresponding to the node is 0 or not is judged, if so, the reading operation of the second process on the data area corresponding to the node is judged to be finished, and if not, the reading operation is not finished.
In one embodiment, the memory space of the first data area may be released after the second data area is inserted into the linked list so that the second node corresponds to the second data area. After the linked list is inserted into the second data area and the linked list returns to the readable state, the process can read the data (i.e. new data) in the second data area according to the linked list, and the memory space of the first data area can be released, so that the memory resources can be saved, and more available free memory spaces can be provided.
As shown in fig. 11, in an embodiment, there is provided another apparatus for sharing a memory by multiple processes, including:
a data writing module 1102, configured to write data into a second data area that is equal to the first data area in size and available in idle, when the first process needs to modify data in the first data area corresponding to the first node in the linked list.
And a read operation determining module 1104, configured to determine whether the read operation of the second process is finished after the data writing module completes the data writing.
A state setting module 1106, configured to set the linked list to the unreadable state when the reading operation of the second process is finished, and otherwise, wait until the reading operation of the second process is finished, and set the linked list to the unreadable state.
The linked list updating module 1108 is configured to insert the second data area into the linked list, so that the first node corresponds to the second data area.
In this embodiment, the state setting module 1106 is further configured to set the linked list to the readable state after the linked list updating module inserts the second data area into the linked list.
In this embodiment, the shared memory is managed by a linked list, each node in the linked list corresponds to one data area in the shared memory, and the data areas corresponding to the nodes form the shared memory. The first node is a node corresponding to the data area that needs to be modified by the first process, and when the first process needs to modify the data in the first data area corresponding to the first node in the linked list, the data writing module 1102 is configured to create a new idle available second data area. Preferably, the size of the second data area opened by the data writing module 1102 is equal to that of the first data area. The modified data is then written to the second data area by the first process.
The read operation determining module 1104 is configured to determine whether the read operation of the second process is finished after the data writing module 1102 completes the data writing, and if so, the state setting module 1106 sets the linked list to be in the unreadable state. Specifically, the state setting module 1106 may be configured to set a flag bit for the shared memory, indicating that the shared memory is not readable currently, so that the process cannot perform a read operation or a write operation on the shared memory. Further, after the linked list is set to be in the unreadable state, the linked list updating module 1108 may insert the second data area into the linked list, so that the first node corresponds to the second data area. The state setting module 1106 sets the linked list to be in a readable state after the node information in the linked list is updated.
In an embodiment, as shown in fig. 12, the apparatus for sharing memory by multi-process operation further includes:
the data reading module 1107 is configured to, when the linked list is in a readable state, obtain, by the second process, an address in the data area corresponding to the node according to the pointer information of the node in the linked list, and read data in the data area corresponding to the node according to the obtained address.
A memory releasing module 1109, configured to release the memory space of the first data area after the linked list updating module 1108 inserts the second data area into the linked list.
In an embodiment, as shown in fig. 13, the apparatus for sharing memory by multi-process operation further includes:
a reference counting module 1110, configured to increment a reference count corresponding to the node when the second process reads data in the data area corresponding to the node; and after the second process finishes reading the data in the data area corresponding to the node, decrementing the reference count corresponding to the node.
Further, the read operation determining module 1104 is configured to determine whether the reference count corresponding to the node is a preset decremented value, if so, determine that the read operation of the data area corresponding to the node by the second process is ended, otherwise, determine that the read operation of the data area corresponding to the node by the second process is not ended.
Preferably, the reference counting module 1110 is configured to set an initial value of the reference count corresponding to each node to be 0, add 1 to the reference count when a process reads data in the data area corresponding to the node, and subtract 1 from the reference count after the process finishes reading. In this way, the read operation determining module 1104 is configured to determine whether the reference count corresponding to the node is 0, if so, determine that the read operation of the second process on the data area corresponding to the node is finished, otherwise, the read operation is not finished.
According to the method and the device for operating the shared memory by the multiple processes, when the first process needs to modify the data in the first data area corresponding to the first node in the linked list, the data is written into the idle available second data area, and because the write operation of the first process can be executed in the second data area, the read operation of the second process cannot be influenced, the write operation and the read operation of the shared memory can be synchronously executed, and the process for reading the data cannot be blocked due to the write operation of the first process. Because the resource lock is not set for the shared memory, the process can realize read operation and write operation on the shared memory without competing resources, the process of the read operation cannot be blocked because the process executes the write operation, and the process of the write operation cannot be blocked because the process executes the read operation, so that the concurrent execution of the read operation and the write operation is realized, and the processing efficiency of the equipment is improved.
After the data writing of the first process is finished and the reading operation of the second process is finished, the linked list is set to be in an unreadable state, and the second data area is inserted into the linked list, so that the reading operation and the writing operation of the shared memory can be unexecutable only when the linked list is updated, and the linked list is set to be in the unreadable state in the updating process of the linked list, so that data reading errors caused by the updating of the linked list can be prevented. And after the second data area is inserted into the linked list, the linked list is set to be in a readable state, so that the read operation and the write operation of the shared memory can be recovered to be executable, and new data can be read when the process reads the shared memory again. When the process needs to modify the data in the shared memory, only the second data area with the same size as the first data area corresponding to the first node needs to be opened up instead of opening up the whole shared memory, so that the memory resource can be saved.
It will be understood by those skilled in the art that all or part of the processes in the methods of the embodiments described above may be implemented by a computer program to instruct related hardware, where the program may be stored in a computer-readable storage medium, and in the embodiments of the present invention, the program is stored in the storage medium of the terminal and executed by at least one processor in the terminal, so as to implement the processes including the embodiments of the methods described above. The storage medium may be a magnetic disk, an optical disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), or the like.
The above-mentioned embodiments only express several embodiments of the present invention, and the description thereof is more specific and detailed, but not construed as limiting the scope of the present invention. It should be noted that, for a person skilled in the art, several variations and modifications can be made without departing from the inventive concept, which falls within the scope of the present invention. Therefore, the protection scope of the present patent shall be subject to the appended claims.

Claims (8)

1. A method of sharing memory for multi-process operations, the method comprising:
when a first process needs to modify data in a first data area corresponding to a first node in a linked list, writing the data into an idle available second data area;
after the data writing is finished, judging whether the reading operation of the second process is finished, if so, setting the linked list to be in an unreadable state, otherwise, waiting until the reading operation of the second process is finished;
inserting the second data area into the linked list to enable the first node to correspond to the second data area;
setting the linked list to a readable state;
when the second process reads data in a data area corresponding to a node, incrementing a reference count corresponding to the node;
and after the second process finishes reading the data in the data area corresponding to the node, decrementing the reference count corresponding to the node.
2. The method of claim 1, further comprising:
and when the linked list is in a readable state, the second process acquires the address in the data area corresponding to the node according to the pointer information of the node in the linked list, and reads the data in the data area corresponding to the node according to the acquired address.
3. The method of claim 1, wherein determining whether the read operation of the second process is finished comprises:
and judging whether the reference count corresponding to the node is a preset decreased numerical value or not, if so, judging that the reading operation of the second process on the data area corresponding to the node is finished, otherwise, judging that the reading operation of the second process on the data area corresponding to the node is not finished.
4. The method of claim 1, wherein after the inserting the second data area into the linked list such that the first node corresponds to the second data area, further comprising:
and releasing the memory space of the first data area.
5. An apparatus for sharing memory for multi-process operations, the apparatus comprising:
the data writing module is used for writing data into a second data area which has the same size as the first data area and is available in a free state when the first process needs to modify the data in the first data area corresponding to the first node in the linked list;
the read operation judging module is used for judging whether the read operation of the second process is finished or not after the data write-in module finishes the data write-in;
a state setting module, configured to set the linked list to an unreadable state when the reading operation of the second process is finished, and otherwise, wait until the reading operation of the second process is finished, and set the linked list to the unreadable state;
a linked list updating module, configured to insert the second data area into the linked list, so that the first node corresponds to the second data area;
the state setting module is further configured to set the linked list to a readable state after the linked list updating module inserts the second data area into the linked list;
the reference counting module is used for incrementing the reference count corresponding to the node when the second process reads the data in the data area corresponding to the node; and after the second process finishes reading the data in the data area corresponding to the node, decrementing the reference count corresponding to the node.
6. The apparatus of claim 5, further comprising:
and the data reading module is used for acquiring the address in the data area corresponding to the node according to the pointer information of the node in the linked list through a second process when the linked list is in a readable state, and reading the data in the data area corresponding to the node according to the acquired address.
7. The apparatus according to claim 5, wherein the read operation determining module is configured to determine whether the reference count corresponding to the node is a preset decremented value, if so, determine that the read operation of the second process on the data area corresponding to the node is finished, otherwise, determine that the read operation of the second process on the data area corresponding to the node is not finished.
8. The apparatus of claim 5, further comprising:
and the memory release module is used for releasing the memory space of the first data area after the linked list updating module inserts the second data area into the linked list.
CN201310370343.2A 2013-08-22 2013-08-22 Method and device for sharing memory by multi-process operation Active CN104424030B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201310370343.2A CN104424030B (en) 2013-08-22 2013-08-22 Method and device for sharing memory by multi-process operation

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201310370343.2A CN104424030B (en) 2013-08-22 2013-08-22 Method and device for sharing memory by multi-process operation

Publications (2)

Publication Number Publication Date
CN104424030A CN104424030A (en) 2015-03-18
CN104424030B true CN104424030B (en) 2020-01-14

Family

ID=52973095

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201310370343.2A Active CN104424030B (en) 2013-08-22 2013-08-22 Method and device for sharing memory by multi-process operation

Country Status (1)

Country Link
CN (1) CN104424030B (en)

Families Citing this family (10)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN107704325B (en) * 2016-08-08 2021-08-27 北京百度网讯科技有限公司 Method and device for transmitting messages between processes
CN106776145B (en) * 2016-12-29 2020-04-03 上海爱数信息技术股份有限公司 Method, system and server for controlling database backup performance
CN106980551A (en) * 2017-03-24 2017-07-25 山东浪潮商用系统有限公司 A kind of Way of Process Communication and device
CN109298935B (en) * 2018-09-06 2023-02-03 华泰证券股份有限公司 Method and application for multi-process write-once read-many lock-free shared memory
CN110020328A (en) * 2019-04-16 2019-07-16 北京字节跳动网络技术有限公司 Data processing method, device, electronic equipment and the storage medium of online table
US10699015B1 (en) 2020-01-10 2020-06-30 Alibaba Group Holding Limited Method and apparatus for data transmission in a tee system
CN110399235B (en) 2019-07-16 2020-07-28 阿里巴巴集团控股有限公司 Multithreading data transmission method and device in TEE system
CN110442462B (en) 2019-07-16 2020-07-28 阿里巴巴集团控股有限公司 Multithreading data transmission method and device in TEE system
CN111162947B (en) * 2019-12-30 2022-08-12 北京天融信网络安全技术有限公司 PCRE hot switching method, network device and storage medium
CN113806239A (en) * 2020-06-11 2021-12-17 中兴通讯股份有限公司 Memory operation control method, equipment and storage medium

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1908934A (en) * 2006-08-02 2007-02-07 华为技术有限公司 Online updating control method and device of embedded database
CN101470632A (en) * 2007-12-24 2009-07-01 华为软件技术有限公司 Sharing internal memory management method and apparatus
CN103164384A (en) * 2011-12-15 2013-06-19 中国银联股份有限公司 Synchronous achieving method and system of multimachine system shared memory

Family Cites Families (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JP4506292B2 (en) * 2004-06-10 2010-07-21 株式会社日立製作所 Cache control method, data processing system, and processing program therefor
JP4210318B1 (en) * 2007-11-28 2009-01-14 株式会社京都ソフトウェアリサーチ Data storage system and data storage program

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN1908934A (en) * 2006-08-02 2007-02-07 华为技术有限公司 Online updating control method and device of embedded database
CN101470632A (en) * 2007-12-24 2009-07-01 华为软件技术有限公司 Sharing internal memory management method and apparatus
CN103164384A (en) * 2011-12-15 2013-06-19 中国银联股份有限公司 Synchronous achieving method and system of multimachine system shared memory

Also Published As

Publication number Publication date
CN104424030A (en) 2015-03-18

Similar Documents

Publication Publication Date Title
CN104424030B (en) Method and device for sharing memory by multi-process operation
CN110287044B (en) Lock-free shared memory processing method and device, electronic equipment and readable storage medium
US6668291B1 (en) Non-blocking concurrent queues with direct node access by threads
US7673105B2 (en) Managing memory pages
US9128615B2 (en) Storage systems that create snapshot queues
CN107066498B (en) Key value KV storage method and device
EP3462324B1 (en) Pointers in a memory managed system
WO2013160724A1 (en) Data processing system and method for operating a data processing system
CN110781016A (en) Data processing method, device, equipment and medium
CN112925606B (en) Memory management method, device and equipment
JP2016533588A (en) Storage processing method, apparatus and terminal
CN107451070B (en) Data processing method and server
CN108829345B (en) Data processing method of log file and terminal equipment
EP3249540A1 (en) Method for writing multiple copies into storage device, and storage device
US9418175B2 (en) Enumeration of a concurrent data structure
CN108874560B (en) Method and communication device for communication
EP4134802A1 (en) Method and apparatus for data access of nand flash file, and storage medium
CN110874273B (en) Data processing method and device
CN114201331B (en) Method, device and equipment for detecting instruction conflict of solid state disk and storage medium
CN117406933B (en) Solid state disk data processing method and device, electronic equipment and storage medium
KR101881038B1 (en) Method for atomic update of memory mapped files stored in non-volatile memory and control apparatus thereof
CN112068948A (en) Data hashing method, readable storage medium and electronic device
KR20150087743A (en) Method and apparatus for controlling a memory synchronization on multi-threading processor
CN116204124B (en) Data processing method and system based on conflict lock and electronic equipment
CN113312522B (en) Management method and device of kernel object, storage medium and electronic equipment

Legal Events

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