CN104461729B - A kind of resource-sharing guard method and device - Google Patents
A kind of resource-sharing guard method and device Download PDFInfo
- Publication number
- CN104461729B CN104461729B CN201310431795.7A CN201310431795A CN104461729B CN 104461729 B CN104461729 B CN 104461729B CN 201310431795 A CN201310431795 A CN 201310431795A CN 104461729 B CN104461729 B CN 104461729B
- Authority
- CN
- China
- Prior art keywords
- shared resource
- lock
- judgment module
- instruction
- deadlock
- 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
Links
Landscapes
- Multi Processors (AREA)
- Debugging And Monitoring (AREA)
Abstract
The present invention proposes a kind of shared resource guard method and device, and wherein method includes: A, judges whether shared resource has been locked, if so, thening follow the steps B;B, CPU executes primary idle running instruction, and the idling times of CPU are added 1;Judge whether CPU idling times are less than pre-set idling times threshold value, if it is less, A is re-execute the steps, if it is not, thening follow the steps C;C, whether the lock for judging the shared resource is deadlock, if it is, discharging the lock of the shared resource, re-execute the steps A;Otherwise, executive process context switches, and the number that process context switches is added 1, current CPU is discharged, when process is by kernel dispatching next time, re-execute the steps A.The present invention can reduce the number of process context switching, reduce CPU usage, and solve the problems, such as Deadlock detection.
Description
Technical field
The present invention relates to shared resource protection technique field more particularly to a kind of shared resource guard method and devices.
Background technique
Mutual exclusion lock and spin lock are all to realize the lock mechanism protecting shared resource and proposing.Spin lock is compared with mutual exclusion lock
Similar, they are provided to solve the use of the mutual exclusion to a certain shared resource.Either mutual exclusion lock or spin lock, any
Can only at most there be a keeper at moment, that is to say, can only at most there be an execution unit to be locked at any time.But
The two is slightly different on scheduling mechanism.For mutual exclusion lock, if resource is occupied, resource bid person, which can only enter, to sleep
Dormancy state;But spin lock will not cause the sleep state of resource bid person, if spin lock is protected by other execution unit
It holds, then resource bid person, which can recycle always, checks whether free the keeper has released lock, and " spin " word is exactly
Therefore it gains the name.
Specifically, assume that process A is accessing shared resource, to guarantee that the shared resource is not seized by other processes, into
Journey A is shared resource setting mutual exclusion lock;When process B needs to access the shared resource, it is found that the shared resource has been set
Mutual exclusion lock, then executive process B and the context of other processes switch, and process B discharges current CPU, wait next time by kernel
Scheduling.As it can be seen that the number that incompatible lock mechanism will lead to process context switching is excessive.
It is assumed that process A is accessing shared resource, to guarantee that the shared resource is not seized by other processes, process A is should
Shared resource setting spin lock;When process B needs to access the shared resource, it is found that the shared resource has been provided with spin
Lock, then CPU can execute primary idle running instruction;Process B checks whether the spin lock has been released again later, if be not released
Then CPU executes primary idle running instruction again ... .., until process B accesses the shared resource after the spin lock has been released.It can
See, spin lock mechanism will lead to CPU occupy it is excessively high, CPU can be made to exhaust if there is deadlock.
Summary of the invention
The present invention provides a kind of shared resource guard methods, can reduce the number of process context switching, reduce CPU
Utilization rate, and solve the problems, such as Deadlock detection.
The present invention also provides a kind of shared resource protective devices, can reduce the number of process context switching, reduce
CPU usage, and solve the problems, such as Deadlock detection.
The technical scheme of the present invention is realized as follows:
A kind of shared resource guard method, comprising:
A, judge whether shared resource has been locked, if so, thening follow the steps B;
B, CPU executes primary idle running instruction, and the idling times of CPU are added 1;Judge whether CPU idling times are less than in advance
The idling times threshold value of setting, if it is less, A is re-execute the steps, if it is not, thening follow the steps C;
C, whether the lock for judging the shared resource is deadlock, if it is, discharging the lock of the shared resource, is held again
Row step A;Otherwise, executive process context switches, and the number that process context switches is added 1, current CPU is discharged, when next
When secondary process is by kernel dispatching, it re-execute the steps A.
In the above method, idling times threshold value is arranged according to the instruction strip number inside the shared resource.
Whether the lock for judging shared resource is that the mode of deadlock can be with are as follows: whether the number for judge that process context switches is big
In pre-set switching times threshold value, if it is greater, then the lock for determining shared resource is deadlock.
Above-mentioned steps A can also include, if it is judged that shared resource is not locked, then lock to the shared resource,
The shared resource is accessed, the lock of the shared resource is discharged after access.
In the above method, locks or the operation of release lock is atomic operation.
A kind of shared resource protective device, comprising:
Judgment module is locked, for judging whether shared resource has been locked, if it is, notice CPU executes primary idle running
The idling times of CPU are added 1 by instruction, and instruction idling times judgment module is judged;
Idling times judgment module, for judging whether CPU idling times are less than in advance according to the instruction of lock judgment module
The idling times threshold value of setting, if it is less, instruction lock judgment module re-starts judgement;If it is not, then instructing
Deadlock judgment module is judged;
Deadlock judgment module, for the instruction according to idling times judgment module judge the shared resource lock whether be
Deadlock, if it is, instruction unlocked state release lock;If it is not, then notice context switching module executes switching;
Unlocked state, the lock of the shared resource is discharged for the instruction according to deadlock judgment module, and lock is notified to judge
Module re-starts judgement;
Context switching module will be in process for being switched according to the instruction execution process context of deadlock judgment module
The number hereafter switched adds 1, discharges current CPU, and when process is by kernel dispatching next time, instruction lock judgment module is re-started
Judgement.
In above-mentioned apparatus, idling times threshold value is arranged according to the instruction strip number inside the shared resource.
Deadlock judgment module judges whether the lock of shared resource is that the mode of deadlock can be with are as follows: judge process context switching
Number whether be greater than pre-set switching times threshold value, if it is greater, then determine shared resource lock be deadlock.
Device can also include locking module;
The lock judgment module is also used to, if it is judged that shared resource is not locked, then locking module is instructed to be added
Lock;
The locking module is used for, and according to the instruction of the lock judgment module, is locked to the shared resource, is notified process
The shared resource is accessed, process discharges the lock of the shared resource after accessing.
In above-mentioned apparatus, locks or the operation of release lock belongs to atomic operation.
As it can be seen that shared resource guard method proposed by the present invention and device, judge shared resource be locked and
When CPU idling times are more than preset threshold value, judge whether the lock of shared resource is deadlock;If so, then discharging lock;Such as
It is not that then executive process context switches, to reduce the number of process context switching, reduces CPU usage, and solve
It has determined Deadlock detection problem.
Detailed description of the invention
Fig. 1 is shared resource guard method implementation flow chart proposed by the present invention;
Fig. 2 is the implementation flow chart of the embodiment of the present invention one;
Fig. 3 is shared resource protective device structural schematic diagram proposed by the present invention.
Specific embodiment
The present invention proposes a kind of shared resource guard method, if Fig. 1 is this method implementation flow chart, when a process needs
When accessing shared resource, following steps are executed:
Step 101: judging whether shared resource has been locked, if so, thening follow the steps 102;
Step 102:CPU executes primary idle running instruction, and the idling times of CPU are added 1;
Step 103: judging whether CPU idling times are less than pre-set idling times threshold value, if it is less, weight
It is new to execute step 101, if it is not, thening follow the steps 104;
Step 104: whether the lock for judging the shared resource is deadlock, if it is, the lock of the shared resource is discharged,
It re-execute the steps 101;Otherwise, executive process context switches, and the number that process context switches is added 1, release is current
CPU re-execute the steps 101 when process is by kernel dispatching next time.
In above-mentioned steps 103, pre-set idling times threshold value is according to the instruction strip number inside the shared resource
Setting, the numerical value should be slightly larger than the instruction strip number inside shared resource.
In above-mentioned steps 104, judge shared resource lock whether be deadlock mode are as follows: judge process context switch
Whether number is greater than pre-set switching times threshold value, if it is greater, then the lock for determining shared resource is deadlock.
In the above method, step 101 further include: if it is judged that shared resource is not locked, then the shared resource is added
Lock, accesses the shared resource, and the lock of the shared resource is discharged after access.
In the above method, locks or the operation of release lock is atomic operation.
Specific embodiment is lifted below to be discussed in detail.
Embodiment one:
In the present embodiment, the process for being currently accessing shared resource is known as process A, process A adds shared resource
Lock, is known as process B for the process for requesting access to shared resource.Such as the implementation flow chart that Fig. 2 is the embodiment of the present invention one, comprising:
Step 201: process B judges whether shared resource has been locked, if so, thening follow the steps 202;If it is not, then
Process B locks the shared resource, accesses the shared resource, and the lock of the shared resource is discharged after the completion of access.
Step 202:CPU executes primary idle running instruction, and the idling times of CPU are added 1.
Step 203: judging whether CPU idling times are less than pre-set idling times threshold value, in the present embodiment
The threshold value is 50 times, if it is less, re-executeing the steps 201;If it is not, thening follow the steps 204.
Step 204: whether the lock for judging the shared resource is deadlock.The standard of judgement is time of process context switching
Number sets switching times threshold value to 20 times in the present embodiment, if process context switches 20 times still without being shared
Resource, then the lock for being considered as the shared resource is deadlock.
When being determined as deadlock, step 205 is executed;It is no to then follow the steps 206.
Step 205: process B discharges process A before to lock added by shared resource, re-execute the steps 201.
Step 206: process B and other processes execute context switching, and process B discharges current CPU;When process B next time
When by kernel dispatching, it re-execute the steps 201.
By the above process as it can be seen that The present invention reduces process context switching times, and CPU usage is reduced, solved
Deadlock detection problem.Specifically, in the above-described embodiments, process B will not be immediately performed after judging that shared resource is locked
Process context switching, but every repetition executes a process context switching after judging 50 times, to reduce process or more
The number of text switching;And CPU will not execute idle running instruction infinitely, but switch 20 (every skies of CPU in process context
Turn 50 times and execute a process context switching) determine the lock afterwards for deadlock, current process release is locked, so that reducing CPU makes
With rate, solves the problems, such as Deadlock detection.
The present invention also proposes a kind of shared resource protective device, such as the structural schematic diagram that Fig. 3 is the device, comprising:
Judgment module 301 is locked, for judging whether shared resource has been locked, if it is, notice CPU executes primary sky
Turn instruction, the idling times of CPU is added 1, instruction idling times judgment module 302 is judged;
Idling times judgment module 302, for judging whether CPU idling times are small according to the instruction of lock judgment module 301
In pre-set idling times threshold value, if it is less, instruction lock judgment module 301 re-starts judgement;If not small
In then instructing deadlock judgment module 303 to be judged;
Deadlock judgment module 303 judges the lock of the shared resource for the instruction according to idling times judgment module 302
It whether is deadlock, if it is, the instruction release lock of unlocked state 304;If it is not, then notice context switching module 305 is held
Row switching;
Unlocked state 304, the lock of the shared resource is discharged for the instruction according to deadlock judgment module 303, and is notified
Lock judgment module 301 re-starts judgement;
Context switching module 305 will for being switched according to the instruction execution process context of deadlock judgment module 303
The number of process context switching adds 1, discharges current CPU, when process is by kernel dispatching next time, instruction lock judgment module
301 re-start judgement.
In above-mentioned apparatus, idling times threshold value is arranged according to the instruction strip number inside the shared resource.
Deadlock judgment module 303 judges whether the lock of shared resource is that the mode of deadlock can be with are as follows: judges process context
Whether the number of switching is greater than pre-set switching times threshold value, if it is greater, then the lock for determining shared resource is deadlock.
Above-mentioned apparatus can also include locking module 306;
Above-mentioned lock judgment module 301 is also used to, if it is judged that shared resource is not locked, is then instructed and is locked module 306
It is locked;
Lock module 306 be used for, according to it is described lock judgment module 301 instruction, to the shared resource lock, notify into
Journey accesses the shared resource, and process discharges the lock of the shared resource after accessing.
In above-mentioned apparatus, locks or the operation of release lock belongs to atomic operation.
To sum up, shared resource guard method proposed by the present invention and device can reduce time of process context switching
Number reduces CPU usage, and solves the problems, such as Deadlock detection.
The foregoing is merely illustrative of the preferred embodiments of the present invention, is not intended to limit the invention, all in essence of the invention
Within mind and principle, any modification, equivalent substitution, improvement and etc. done be should be included within the scope of the present invention.
Claims (8)
1. a kind of shared resource guard method, which is characterized in that the described method includes:
A, judge whether shared resource has been locked, if so, thening follow the steps B;
B, CPU executes primary idle running instruction, and the idling times of CPU are added 1;Judge whether CPU idling times are less than to preset
Idling times threshold value, if it is less, A is re-execute the steps, if it is not, thening follow the steps C;
C, whether the lock for judging the shared resource is deadlock, if it is, discharging the lock of the shared resource, re-executes step
Rapid A;Otherwise, executive process context switch, by process context switch number add 1, discharge current CPU, when next time into
When journey is by kernel dispatching, it re-execute the steps A, wherein
It is described judge shared resource lock whether be deadlock mode are as follows: judge process context switch number whether be greater than it is pre-
The switching times threshold value being first arranged, if it is greater, then the lock for determining shared resource is deadlock.
2. the method according to claim 1, wherein the idling times threshold value is according in the shared resource
The instruction strip number in portion is arranged.
3. method according to claim 1 or 2, which is characterized in that step A further includes, if it is judged that shared resource is not
It is locked, then the shared resource is locked, access the shared resource, the lock of the shared resource is discharged after access.
4. method according to claim 1 or 2, which is characterized in that the operation that the locking or release are locked is atomic operation.
5. a kind of shared resource protective device, which is characterized in that described device includes:
Judgment module is locked, for judging whether shared resource has been locked, if it is, notice CPU executes primary idle running instruction,
The idling times of CPU are added 1, instruction idling times judgment module is judged;
Idling times judgment module is preset for judging whether CPU idling times are less than according to the instruction of lock judgment module
Idling times threshold value, if it is less, instruction lock judgment module re-start judgement;If it is not, then instructing deadlock
Judgment module is judged;
Deadlock judgment module judges whether the lock of the shared resource is dead for the instruction according to idling times judgment module
Lock, if it is, instruction unlocked state release lock;If it is not, then notice context switching module executes switching;
Unlocked state, the lock of the shared resource is discharged for the instruction according to deadlock judgment module, and notifies lock judgment module
Re-start judgement;
Context switching module, for being switched according to the instruction execution process context of deadlock judgment module, by process context
The number of switching adds 1, discharges current CPU, and when process is by kernel dispatching next time, instruction lock judgment module, which re-starts, to be sentenced
It is disconnected,
Wherein, the deadlock judgment module judge shared resource lock whether be deadlock mode are as follows: judge that process context is cut
Whether the number changed is greater than pre-set switching times threshold value, if it is greater, then the lock for determining shared resource is deadlock.
6. device according to claim 5, which is characterized in that the idling times threshold value is according in the shared resource
The instruction strip number in portion is arranged.
7. device according to claim 5 or 6, which is characterized in that described device further includes locking module;
The lock judgment module is also used to, if it is judged that shared resource is not locked, then locking module is instructed to be locked;
The locking module is used for, and according to the instruction of the lock judgment module, is locked to the shared resource, notice process access
The shared resource, process discharge the lock of the shared resource after accessing.
8. device according to claim 5 or 6, which is characterized in that the locking or the operation of release lock belong to atom behaviour
Make.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201310431795.7A CN104461729B (en) | 2013-09-22 | 2013-09-22 | A kind of resource-sharing guard method and device |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201310431795.7A CN104461729B (en) | 2013-09-22 | 2013-09-22 | A kind of resource-sharing guard method and device |
Publications (2)
Publication Number | Publication Date |
---|---|
CN104461729A CN104461729A (en) | 2015-03-25 |
CN104461729B true CN104461729B (en) | 2019-02-05 |
Family
ID=52907828
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201310431795.7A Active CN104461729B (en) | 2013-09-22 | 2013-09-22 | A kind of resource-sharing guard method and device |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN104461729B (en) |
Families Citing this family (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN110018907A (en) | 2019-01-16 | 2019-07-16 | 阿里巴巴集团控股有限公司 | Promote the method and device and electronic equipment of cpu performance |
CN112231131B (en) * | 2020-09-28 | 2024-05-28 | 北京金山云网络技术有限公司 | Method, device and equipment for realizing database lock and readable storage medium |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1601478A (en) * | 2003-09-25 | 2005-03-30 | 国际商业机器公司 | Method and system for dynamically bounded spinning threads on a contested mutex |
CN1831779A (en) * | 2005-03-11 | 2006-09-13 | 华为技术有限公司 | Detection method of signal quantization deadlock |
CN102129391A (en) * | 2011-03-14 | 2011-07-20 | 华中科技大学 | Method for implementing spin lock in database |
US8453147B2 (en) * | 2006-06-05 | 2013-05-28 | Cisco Technology, Inc. | Techniques for reducing thread overhead for systems with multiple multi-threaded processors |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US6782440B2 (en) * | 2000-07-26 | 2004-08-24 | T.N.S. Holdings, Inc. | Resource locking and thread synchronization in a multiprocessor environment |
-
2013
- 2013-09-22 CN CN201310431795.7A patent/CN104461729B/en active Active
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN1601478A (en) * | 2003-09-25 | 2005-03-30 | 国际商业机器公司 | Method and system for dynamically bounded spinning threads on a contested mutex |
CN1831779A (en) * | 2005-03-11 | 2006-09-13 | 华为技术有限公司 | Detection method of signal quantization deadlock |
US8453147B2 (en) * | 2006-06-05 | 2013-05-28 | Cisco Technology, Inc. | Techniques for reducing thread overhead for systems with multiple multi-threaded processors |
CN102129391A (en) * | 2011-03-14 | 2011-07-20 | 华中科技大学 | Method for implementing spin lock in database |
Also Published As
Publication number | Publication date |
---|---|
CN104461729A (en) | 2015-03-25 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11314562B2 (en) | Systems and methods for performing concurrency restriction and throttling over contended locks | |
US20090037927A1 (en) | Apparatus and method for direct switching of software threads | |
CN105760216A (en) | Multi-process synchronization control method | |
CN109983805A (en) | WIFI memory power minimizes | |
CN104503828B (en) | A kind of process management method and terminal | |
CN1973266A (en) | Improvements in or relating to an operating system for a computing device | |
CN102999378A (en) | Read-write lock implement method | |
CN102495761A (en) | Method and device for closing applications | |
CN103761182A (en) | Method and device for deadlock detection | |
CN104461729B (en) | A kind of resource-sharing guard method and device | |
CN104794395A (en) | Architecture characteristic based lightweight multi-system safety management structure | |
CN105975349A (en) | Thread lock optimization method | |
CN103984898A (en) | Detection method and detection device for attack in spray pattern | |
CN103885565A (en) | Dormancy method and dormancy system for intelligent equipment | |
CN105824709B (en) | A kind of critical zone access method and device | |
CN102141932B (en) | Large critical region protection method | |
CN105718320B (en) | Clock task processing method, device and equipment | |
CN101470627B (en) | Method for implementing parallel multi-core configuration lock on MIPS platform | |
CN104063283A (en) | Resource-scheduling method for resource manager | |
TWI716074B (en) | Method and device for improving CPU performance and electronic equipment | |
Ozaktas et al. | Minimizing the cost of synchronisations in the WCET of real-time parallel programs | |
Enyioha et al. | Bio-inspired strategy for control of viral spreading in networks | |
US7689992B2 (en) | Sharing lock mechanism between protocol layers | |
CN107003954A (en) | Synchronization in computing device | |
CN106648898A (en) | Data management method and system applicable to mode of multiple producers and multiple consumers |
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 |