The famous banker algorithm, which was first proposed by Dijkstra. It is one of the most representative algorithms for avoiding deadlocks . In the avoid deadlock method allows the process to request resources dynamically, but before the allocation of resources, the allocation of resources should be calculated before the security, if the allocation will not cause the system to enter an unsafe state, then allocate, otherwise wait.
The most important thing about the banker's algorithm is that the judgment is the relationship between the available resources and the resources that are still needed, and if the number of available resources is greater than the number of human resources, then we think that the process is enforceable and secure, and conversely, it is unsafe. So the most serious is to find a variety of resources.
The process is judged according to the following steps:
1. The number of resources at the beginning of the computing system, that is, the number of resources available;
2. Compare the number of resources that are still required and the number of available resources, find the eligible processes, and finally modify the number of resources available to the system when the process has finished executing;
3. Continue to compare the remaining processes and the number of available resources to find the processes that can be implemented below;
4. and so on;
The following example to explain these obscure words, be sure to take a serious look O (∩_∩) o~
"Example" assumes that there are 3 types of mutually exclusive resources R1, R2, R3 in the system, the available resources are 9, 8, 5, respectively. In the T0 time system there are five processes in P1, P2, P3, P4, and P5, which have the greatest demand for resources and the number of allocated resources as shown in the following table, and how the process executes is secure.
2
1
1
2
resources Span style= "font-family:kaiti_gb2312; Font-size:18px "> process |
maximum demand |
The number of allocated resources |
r1 |
r2 |
r3 |
r1 |
r2 |
r3 |
p1 |
6 |
5 |
2 |
1 | TD valign= "Top" >
1 |
p2 |
2 |
2 |
1 |
2 | TD valign= "Top" >
1 |
p3 |
8 |
0 |
1 |
2 | TD valign= "Top" >
0 |
p4 |
1 |
2 |
1 |
1 | TD valign= "Top" >
0 |
P5 |
3 |
4 |
4 |
1 |
1 |
3 |
It is emphasized here that no matter what conditions are given in the topic, we will be able to cope with the changes as soon as we find the following information:
Chinese Source Into Ride |
Maximum Demand |
Number of allocated resources |
Resources still needed |
Available resources |
Practice Yes Shun Order |
R1 |
R2 |
R3 |
R1 |
R2 |
R3 |
R1 |
R2 |
R3 |
R1 |
R2 |
R3 |
P1 |
6 |
5 |
2 |
1 |
2 |
1 |
5 |
3 |
1 |
7 |
7 |
5 |
④ |
P2 |
2 |
2 |
1 |
2 |
1 |
1 |
0 |
1 |
0 |
4 |
2 |
1 |
① |
P3 |
8 |
0 |
1 |
2 |
1 |
0 |
6 |
-1 |
1 |
9 |
8 |
5 |
⑤ |
P4 |
1 |
2 |
1 |
1 |
2 |
0 |
0 |
0 |
1 |
5 |
4 |
1 |
Ii |
P5 |
3 |
4 |
4 |
1 |
1 |
3 |
2 |
3 |
1 |
6 |
5 |
4 |
③ |
"Note":
available resources : Indicates that the appropriate process has finished executing (that is, releasing the resources used by the process), the resources available for the formula available = available resources + allocated resources, (because the allocated resources will be released after the process has finished executing, so the available resources will be increased, The process is completely freed when it executes, and it is also a resource that is available when the next process executes.
* * Note that depending on the execution of the process, the available resources will not be the same each time you fill in the table (because the resources allocated by each process are different), then the order of execution will vary and reasonable.
Resources Still required: number of resources required = maximum demand-The number of allocated resources, whereby the formula can calculate the number of resources that R1, R2, R3 still need in different processes, as shown in the table above.
follow the previous steps to achieve the following:
R1 the total number of resources allocated is 1+2+2+1+1=7
R2 the total number of resources allocated is 2+1+1+2+1=7
R3 the total number of resources allocated is 1+1+0+0+3=5
The number of available resources for R1 R2 R3 is 9-7=2, respectively,
8-7=1,
5-5=0,
1. The number of resources started R1 R2R3 is 2, 1, 0, so from the still need to find resources (need to indicate that the minimum number of resources as a constraint to find the results), only the P2 process meets the conditions, when the available resources become 4, 2, 1;
2. Next find the qualifying process in the rest of the process, only execute P4, at which time the available resources become 5, 4, 1, and so on, follow the steps above to find out the order of all Process execution P2->p4->p5->p1->p3 ;
The above is the calculation process of the banker algorithm, in general, the banker algorithm is more on the concept of the inspection, to find out the constraints between the conditions of the relationship can be solved, I hope to be helpful!
Banker algorithm--a study of soft test (IV.)