Summary of the invention
Technical matters to be solved by this invention is in the two times scheduling system of preemptive priority scheduling, how to realize lock mechanism.
Technical scheme of the present invention is as follows:
A kind of locking method of lock mechanism, it may further comprise the steps: the pre-approximate number of A1, each resource of initialization is respectively preset value, is used for each state of resources of mark and is not reservation; A2, when applying for each resource, the pre-approximate number of each resource of apply for is added an operation, the lock node that current process is relevant joins the lock waiting list, and judges whether all apply for successfully, if do not apply for successfully then carry out A3 less than whole; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; A3, for each resource of application failure, the described lock waiting list of reverse search, judge whether to search for therein and obtain required resource, be then to treat the scheduling queue identification item what Search Results shut node mutually, add the process identification (PID) of current process, and the resource that search obtains is added to the resource items to be applied for of the relevant lock node of current process; Whether all each resource of A4, judgement application failure search is finished, otherwise continues execution A3.
Described locking method, wherein, the steps A 1 concrete following steps of carrying out: the Resources list is set, is used to manage the pre-approximate number of each resource; The pre-approximate number of each resource of the described the Resources list of initialization is respectively preset value; And, in the steps A 2, when applying for each resource, be in described the Resources list, the pre-approximate number of each resource of apply for is added an operation.
A kind of unlock method of lock mechanism, it may further comprise the steps: B1, resources allocation is given lock node in the lock waiting list in order; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; B2, for each resource that a certain lock node is applied for, judge whether it all applies for successfully, be its corresponding process that then unblocks, the pre-approximate number of corresponding resource is subtracted one.
Described unlock method wherein, is also carried out following steps: B0 before the step B1, is judged that the head node of described lock waiting list is invalid, judges that perhaps affairs have carried out to the end, then execution in step B1.
A kind of implementation method of lock mechanism, it comprises that the following step reconciliation lock-step that locks is rapid;
The described step that locks may further comprise the steps: the pre-approximate number of C1, each resource of initialization is respectively preset value, is used for each state of resources of mark and is not reservation; C2, when applying for each resource, the pre-approximate number of each resource of apply for is added an operation, the lock node that current process is relevant joins the lock waiting list, and judges whether all apply for successfully, if do not apply for successfully then carry out C3 less than whole; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; C3, for each resource of application failure, the described lock waiting list of reverse search, judge whether to search for therein and obtain required resource, be then to treat the scheduling queue identification item what Search Results shut node mutually, add the process identification (PID) of current process, and the resource that search obtains is added to the resource items to be applied for of the relevant lock node of current process; Whether all each resource of C4, judgement application failure search is finished, otherwise continues execution C3;
Described release step may further comprise the steps: C5, resources allocation is given lock node in the lock waiting list in order; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; C6, for each resource that a certain lock node is applied for, judge whether it all applies for successfully, be its corresponding process that then unblocks, the pre-approximate number of corresponding resource is subtracted one.
Described implementation method, wherein, step C1 specifically carries out following steps: the Resources list is set, is used to manage the pre-approximate number of each resource; The pre-approximate number of each resource of the described the Resources list of initialization is respectively preset value; And, among the step C2, when applying for each resource, be in described the Resources list, the pre-approximate number of each resource of apply for is added an operation; And, among the step C6, the pre-approximate number of corresponding resource is subtracted for the moment, be in described the Resources list, the pre-approximate number of corresponding resource is subtracted an operation.
Described implementation method wherein, is also carried out following steps: C50 before the step C5, is judged that the head node of described lock waiting list is invalid, judges that perhaps affairs have carried out to the end, then execution in step C5.
Described implementation method, wherein, also carry out following steps before the step C50: adopting periodic monitor mechanism, judge whether overtime not response, is then to carry out C50.
Described implementation method wherein, among the step C6, before the process of the current lock node correspondence that unblocks, is also carried out following steps: judge whether this process is effective, otherwise do not make subsequent treatment.
Described implementation method, wherein, step C2 is to the operation of step C4, and step C5 adopts respectively and monopolizes the realization of task form to the operation of step C6.
Adopt such scheme, the present invention proposes the lock mechanism of not waiting for, in preemptive priority two times scheduling system, realized quick lock mechanism effectively, for concurrent the providing efficiently of affairs supported based on semaphore.
Embodiment
Below in conjunction with the drawings and specific embodiments, the present invention is described in detail.
Purport of the present invention is to propose a kind of non-semaphore lock realization mechanism based on the process two times scheduling, and this mechanism is not waited for based on semaphore; The present invention is by being pre-approximate number of each conservation of resources that can lock, and safeguards a lock waiting list, realizes above-mentioned mechanism.
As shown in Figure 1, the invention provides a kind of locking method of lock mechanism, it may further comprise the steps:
The pre-approximate number of A1, each resource of initialization is respectively preset value, is used for each state of resources of mark and is not reservation; For example, the preset value of a certain resource is 0 o'clock, represents this state of resources to be not reservation; Perhaps, also can be defined as, the preset value of a certain resource is 1 o'clock, represents this state of resources to be not reservation; The present invention does not impose any restrictions preset value.
A2, when applying for each resource, the pre-approximate number of each resource of apply for is added an operation, the lock node that current process is relevant joins the lock waiting list, and judges whether all to apply for successfully, otherwise execution A3.For example, preset value is 0 o'clock, represents this state of resources to be not reservation; When a certain resource of a certain process application, then the pre-approximate number to this resource adds an operation, and its pre-approximate number is made as 1; When another process was also applied for this resource, then the pre-approximate number to this resource added an operation again, and its pre-approximate number is made as 2.
Wherein, described lock waiting list, be also referred to as the release collection, an example is as shown in the table, it is provided with following respectively for each lock node: process identification (PID) (PID, process identity, the i.e. sign of Suo Ding process), resource to be applied for (WAIT_RES), apply for successful resource (HAVE_RES) and treat scheduling queue sign (PEND_QUE).
Wherein, the process identification (PID) item is used for the storage process sign, should lock the process of node correspondence with mark; Apply for successful resource items be used for this process of mark the successful resource that obtains of application; Resource items to be applied for is used for the not success of resource that this process of mark is applied for, needs to wait for scheduling; Treat that the scheduling queue identification item is used for the process identification (PID) of other process of mark, expression: the resource when other process is applied for, taken by the process of this lock node correspondence, treat this process carry out discharge after, resource of its needs is allotted to described other process.
Suppose to have 9 resources to be respectively 1,2,3,4,5,6,7,8,9, suppose process 1 is at first blocked, and needing resource is 1,2,4,6,9.
LOCK |
WAIT_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
|
A3, for each resource of application failure, the described lock waiting list of reverse search, judge whether to search for therein and obtain required resource, be then to treat the scheduling queue identification item what Search Results shut node mutually, add the process identification (PID) of current process, and the resource that search obtains is added to the resource items to be applied for of the relevant lock node of current process.
Whether all each resource of A4, judgement application failure search is finished, otherwise continues execution A3.
In concrete the application, can adopt the Resources list, manage the pre-approximate number of each resource, perhaps manage each resource and pre-approximate number thereof; An embodiment is the steps A 1 concrete following steps of carrying out: the Resources list is set, is used to manage the pre-approximate number of each resource; The pre-approximate number of each resource of the described the Resources list of initialization is respectively preset value; Like this, in steps A 2,, then in described the Resources list, the pre-approximate number of each resource of apply for is added an operation when application during each resource.
Each above-mentioned embodiment can adopt respectively and monopolize the realization of task form.
Accordingly, as shown in Figure 2, the invention provides a kind of unlock method of lock mechanism, it may further comprise the steps: B1, resources allocation is given lock node in the lock waiting list in order; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; B2, for each resource that a certain lock node is applied for, judge whether it all applies for successfully, be its corresponding process that then unblocks, the pre-approximate number of corresponding resource is subtracted one.
An embodiment is, also carries out following steps: B0 before the step B1, judges that the head node of described lock waiting list is invalid, judges that perhaps affairs have carried out to the end, then execution in step B1.And for example, on the basis of this embodiment, can also carry out following steps before B0: adopting periodic monitor mechanism, judge whether overtime not response, is then to carry out B0.
Another embodiment is among the step B2, before the process of the current lock node correspondence that unblocks, also to carry out following steps: judge whether this process is effective, otherwise do not make subsequent treatment, the process and the subsequent operation thereof of the current lock node correspondence that promptly need not to unblock.In general, can judge whether this process also exists according to PID, otherwise think that it lost efficacy.
Be more preferably, same as above, can adopt the Resources list, manage the pre-approximate number of each resource, perhaps manage each resource and pre-approximate number thereof; An embodiment is, at first, pre-sets the Resources list, is used to manage the pre-approximate number of each resource; The pre-approximate number of each resource of the described the Resources list of initialization is respectively preset value; Like this, in step B2, after the corresponding process that unblocks, then in described the Resources list, the pre-approximate number of corresponding resource is subtracted an operation.
Same, each above-mentioned embodiment can adopt respectively and monopolize the realization of task form.
Comprehensive above-mentioned locking method and unlock method, the present invention also provides a kind of implementation method of lock mechanism, and it comprises that the following step reconciliation lock-step that locks is rapid.
The described step that locks may further comprise the steps:
The pre-approximate number of C1, each resource of initialization is respectively preset value, is used for each state of resources of mark and is not reservation; Same, for example, the preset value of a certain resource is 0 o'clock, represents this state of resources to be not reservation; Perhaps, also can be defined as, the preset value of a certain resource is 100 o'clock, represents this state of resources to be not reservation; The present invention does not impose any restrictions preset value.
C2, when applying for each resource, the pre-approximate number of each resource of apply for is added an operation, the lock node that current process is relevant joins the lock waiting list, and judges whether all to apply for successfully, otherwise execution C3; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign; Every implication is identical with the description of above-mentioned locking method, does not repeat them here.
C3, for each resource of application failure, the described lock waiting list of reverse search, judge whether to search for therein and obtain required resource, be then to treat the scheduling queue identification item what Search Results shut node mutually, add the process identification (PID) of current process, and the resource that search obtains is added to the resource items to be applied for of the relevant lock node of current process.
Whether all each resource of C4, judgement application failure search is finished, otherwise continues execution C3.
Wherein, step C2 can adopt and monopolize the realization of task form to the operation of step C4.
Described release step may further comprise the steps:
C5, resources allocation is given lock node in the lock waiting list in order; Wherein, described lock waiting list is provided with following respectively for each lock node: process identification (PID), resource to be applied for, the successful resource of application and treat the scheduling queue sign.
For example, can also carry out following steps: C50 before the step C5, judge that the head node of described lock waiting list is invalid, judge that perhaps affairs have carried out to the end, then execution in step C5.And for example, on this basis, also carry out following steps before the step C50: adopting periodic monitor mechanism, judge whether overtime not response, is then to carry out C50.
C6, for each resource that a certain lock node is applied for, judge whether it all applies for successfully, be its corresponding process that then unblocks, the pre-approximate number of corresponding resource is subtracted one.For example, before the process of current lock node correspondence that unblocks, also carry out following steps: judge whether this process is effective, otherwise do not make subsequent treatment.
When adopting the Resources list, an embodiment is that step C1 specifically carries out following steps: the Resources list is set, is used to manage the pre-approximate number of each resource; The pre-approximate number of each resource of the described the Resources list of initialization is respectively preset value; And, among the step C2, when applying for each resource, be in described the Resources list, the pre-approximate number of each resource of apply for is added an operation; And, among the step C6, the pre-approximate number of corresponding resource is subtracted for the moment, be in described the Resources list, the pre-approximate number of corresponding resource is subtracted an operation.
Wherein, step C5 can adopt and monopolize the realization of task form to the operation of step C6.
It below is a complete example of implementation method of the present invention.
1, the flow process that locks, as shown in Figure 1, its parameter comprises PID, the Resources list.
1)TaskLock;
2) apply for all not reserve resources;
3) add one for the pre-approximate number of all resources in the Resources list;
4) if resource has applied for fully that then TaskUnlock continues transaction operation;
5) if resource does not meet the demands, the resource in the reverse search waiting list is then added oneself at this release collection afterbody if a certain item contains the resource of needs, up to all resources notify object has been arranged all;
7)TaskUnlock;
8) hang up oneself.
2. release flow process, as shown in Figure 2, its parameter is in the transaction queues; Release opportunity, be generally the invalid or affairs of group head node and carry out to the end.
1)TaskLock;
2) the lock node of successively resources allocation in the Resources list being concentrated to release, if certain lock node has been applied for all resources, this process that then unblocks can judge whether earlier before unblocking effectively, invalidly then do not deal with, the pre-approximate number of corresponding resource is subtracted one;
3)TaskUnlock。
A special case of implementation method or unlock method is to carry out Deadlock Detection.
If some process instances have been visited database, and its affairs come the transaction queues head, then after this example is killed by other process, its effectively release discharge resource, can cause the deadlock of database like this.
Therefore the present invention has introduced periodic monitor mechanism, if team's head node is invalid, and unlocking force head node then.
If some process instances are in the centre of formation, if after then it was killed unusually, affairs were not deleted, need wait until that all affairs of its front just carry out release after finishing, can be detected by Deadlock Detection mechanism, such situation is very rare, just a kind of preventive means that adopts.
Provide a concrete example below again, each method of the present invention is elaborated.
Suppose to have 9 resources to be respectively 1,2,3,4,5,6,7,8,9; It is 0 that each resource has an initial pre-approximate number.
Suppose process 1 is at first blocked, and it needs resource is 1,2,4,6,9.
Process 1 is blocked as shown in the table:
LOCK |
WAIT_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
|
Process 1 needs resource 1,2,4,6,9; At this time pre-approximate number all is 0, so all resource all can be met, process 1 adds 1 with the pre-approximate number of resource 1,2,4,6,9, and blockade is finished, and affairs are carried out.
Suppose that process 2 is seized operation, needs resource 1,2,5,8 when process 1 is uncompleted.
It is as shown in the table that process 2 is blocked the back:
LOCK |
WAIT_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
PID2 |
PID2 |
1,2, |
5,8 |
|
Process 2 is not come when the affairs of process 1 are finished, this time, resource 5,8 pre-approximate number was 0, process 2 can be applied for resource 5,8, but the pre-approximate number of resource 1,2 is 1, so need to wait for resource 1,2, at this moment, therefore reverse the Resources list of searching in the formation has been found resource 1,2 at the lock node PID1 place of process 1, treats the scheduling queue identification item at its this lock node end, add PID2, the pre-approximate number of resource 1,2,5,8 is added 1.
Suppose process 3 needs three resources 3,4,8.
It is as shown in the table that process 3 is blocked the back:
LOCK |
WAIT_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
PID2,PID3 |
PID2 |
1,2, |
5,8, |
PID3 |
PID3 |
4,8 |
3 |
|
Process 3 finds that the pre-approximate number of resource 3 is 0, resource 4 and 8 pre-approximate number are not 0, therefore need to wait for resource 4,8, reverse search resource queue, for the first time found resource 8 at the PID2 place, therefore in the obstruction formation that joins PID2 of the PID of oneself, find resource 4 at the PID1 place for the first time, therefore the PID of oneself has been joined the obstruction rear of queue of PID1.
Process 1 release is as shown in the table: TASKLOCK;
LOCK |
WAIT_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
PID3 |
PID2 |
|
1,2,5,8, |
PID3 |
PID3 |
4,8 |
3 |
|
Process 1 complete release at first finds release to concentrate first element PID2, gives him with its resource that needs 1,2, finds that PID2 can call in operation, calls in.
LOCK |
NEED_RES |
HAVE_RES |
PEND_QUE |
PID1 |
|
1,2,4,6,9 |
|
PID2 |
|
1,2,5,8 |
PID3 |
PID3 |
8 |
4,3 |
|
[0093]Next take off release element PID3 one time, the resource 4 that it needs, PID1 can provide, but detecting its resource does not obtain fully, does not do any action.
LOCK |
NEED_RES |
HAVE_RES |
PEND_QUE |
PID2 |
|
1,2,5,8, |
PID3 |
PID3 |
8 |
4,3 |
|
PID1 is deleted from formation, and the pre-approximate number of its used resource 1,2,4,6,9 subtracts one.
TASKUNLOCK。
Adopt said method, in preemptive priority two times scheduling system, realize quick lock mechanism effectively, for concurrent the providing efficiently of affairs supported.
Should be understood that, for those of ordinary skills, can be improved according to the above description or conversion, and all these improvement and conversion all should belong to the protection domain of claims of the present invention.