Avoid deadlock with banker's algorithm

1. System security status

The safe state means that the system can allocate the resources it needs to each process in a certain process advancement order (P1, P2, ..., Pn) until the maximum demand for resources of each process is met, so that each process can successfully completed. At this time, the sequence (P1,P2,...,Pn) is a safe sequence. If the system cannot find such a sequence, the system is said to be in an unsafe state.
While not all unsafe states necessarily turn into deadlock states, when the system enters an unsafe state, it is possible to enter a deadlock state. Conversely, as long as the system is in a safe state, it will not enter a deadlock state. Therefore, the essence of avoiding deadlock is that when the system allocates resources, the system should not enter a safe state.

Therefore, as long as there is an allocation sequence, the system is safe.
For example:
Suppose there are three processes P1, P2, P3 in the system, with a total of 12 tape drives. Process P1 requires a total of 10 tape drives, process P2 requires 4 tape drives, and process P3 requires a total of 9 tape drives. Suppose that at time T0, the processes P1, P2, and P3 have obtained 5, 2, and 2 units respectively, and there are still 3 units to be allocated. As shown below:
write picture description here
Such a system is safe at time T0, and there is a safe sequence P2->P1->P3. Process: Take out 2 of the remaining tape drives and distribute them to P2, so that they can continue to run. After P2 is completed, 4 tape drives can be released. At this time, the available resources are 4+(3-2)=5, and then these 5 tape drives can be released. The resources of the stations are allocated to P1, and 10 stations can be released after the operation of P1 is completed, and P3 can obtain enough resources, so that each process of P1, P2, and P3 can be completed.

The banker's algorithm

The representative algorithm for solving the deadlock problem is the banker's algorithm. The reason for the name is to ensure that the bank will not fail to meet the needs of customers when issuing cash loans.

In order to implement the banker's algorithm, when each process enters the system, the process must tell the system that the maximum number of units of each resource type may be required in the running process, and the number cannot exceed the total resources possessed by the system. When a process requests a resource, the system must first determine whether there are enough resources allocated to the process. If so, it will calculate whether the system will be in an unsafe state after these resources are allocated to the process. The system will only allocate resources to it if it will not be in an unsafe state, otherwise it will make the process wait.

1. The data structure in the banker's algorithm
(1) The
available resource vector is an array containing m elements, each element represents the number of available resources of a class, and the initial value is the class allocated in the system. The number of all available resources, whose value varies with the allocation and reclamation of such resources.
(2) The Max
maximum demand matrix is ​​an n*m matrix that defines the maximum demand for m-type resources for each of the n processes in the system.
(3) The Allocation
allocation matrix is ​​an n*m matrix that defines the number of resources currently allocated to each process for each type of resource in the system.
(4) Need
demand matrix, which is an n*m matrix, indicating the number of various resources that each process still needs.

Has the following relationship:
Nedd[i,j]=Max[i,j]-Allocation[i,j]

2. Banker's Algorithm
Let Request_i be the request vector of process Pi. If Request_i[j]=K, it means that process Pi needs K resources of type Rj. When the PI sends a resource request, the system checks according to the following steps:
(1) If Request_i[j]<=Need[i,j], turn to (2); otherwise, an error occurs because the required resources have exceeded the announced the maximum value of .
(2) If Request_i[j]<=Available[j], turn to (3); otherwise, it means that there are not enough resources, and Pi needs to wait.
(3) The system tentatively allocates resources to process Pi, and modifies the values ​​in the following data structure:
Available[j]=Available[j]-Request_i[j];
Allocation[i,j]=Allocation[i,j] +Request_i[j];
Need[i,j]=Need[i,j]-Request_i[j];
(4) The system executes the security algorithm to check whether the system is in a safe state after the resource allocation. If it is safe, the resource will be officially allocated to the process Pi to complete the allocation; otherwise, the tentative allocation will be invalidated, the original resource allocation state will be restored, and the process Pi will be allowed to wait.

3. Security algorithm
(1) Set two vectors, the work vector work, which represents the number of various resources that can be provided to the process to continue running, and contains m elements. At the beginning of the execution of the security algorithm, work:=Available;finish , indicating whether the system has enough resources allocated to the process to make it run to completion. Start finish[i]:=false, when there are resources allocated to it, finish[i]:=true.
(2) Find a process from the process set that satisfies the following conditions:
finish[i]:=false;
Need[i,j]<=work[j];
If found, execute (3); otherwise, execute ( 4).
(3) After the process Pi obtains the resources, it can be executed sequentially until it is completed, and the resources allocated to it are released, so it should be executed:
work[j]=work[j]+Allocation[i,j];
finish[i ]=true;
go to (2).
(4) If all processes finish[i]=true are satisfied, the system is in a safe state; otherwise, the system is in an unsafe state.

Example:
If there are five processes P1, P2, P3, P4, P5, three types of resources A, B, C, and the number of various types of resources in decibels is 10, 5, and 7, the resource allocation at time
write picture description here
T0 is as follows: Security: Use security algorithm to analyze time T0, there is a security sequence P1, P3, P4, P2, P0 at time T0, so the system is safe.
write picture description here
P1 requests resources: P1 sends a request vector Resquest1(1,0,2), and the system checks it according to the banker's algorithm:
Resquest1(1,0,2)<=Need(1,2,2);
Resquest1(1,0, 2)<=Available(3,3,2);
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325643759&siteId=291194637
Recommended