If reproduced please specify the source: Http://blog.csdn.net/gophers
The banker algorithm is a detection algorithm that can be used to prevent deadlocks, as is the case with the name of the algorithm, where the allocation of resources is similar to that of bankers managing bank funds. Bankers need to reconcile the business between clients at the time of the loan, and the best thing is to allocate the current funds reasonably, leaving the remaining funds sufficient to deal with other recent business, and to ensure that the funds that have been lent before the new client is ready for the loan have been withdrawn. This approach can also be applied when the operating system coordinates resource consumption relationships between processes.
The banker algorithm mainly consists of two parts, one is Safety algorithm (security state detection algorithm), the other is Resource-request algorithm (Resource request algorithm). The two algorithms work together to prevent deadlocks in the operating system. The resource request algorithm is not used alone, and the security detection algorithm is usually called after each call to the resource request algorithm.
Before explaining the banker algorithm, let's define some global data structures that are essential to understanding the banker algorithm! Please be careful and understand that you should be able to react to these data structures at any time when you continue to go on:
Let's assume that the process in the system has p, the resource has r, we want to define the following array (or two-bit array)
1. Available
Available is an array whose length equals R, which is the same as the number of resources. The value of each element in the array represents the number of resources that can be obtained in the current system. In short, available is the number of resources available in the current system. Be sure to pay attention to the current Oh ~
2. Max
Max is a two-dimensional array, defined as Max[p][r], which represents the number of each resource in the R resources required for each process in the P process to complete its task. For example, max[0][3]=5 indicates that the NO. 0 process requires a third resource of 5 to complete its task.
3. Allocation
Allocation is a two-dimensional array, defined as Allocation[p][r], which represents the number of each resource that each process has been assigned to. For example, allocation[0][3]=2 indicates that the NO. 0 process has been allocated 2 third resources by the system.
4. need
Need is also a two-dimensional array, defined as need[p][r], which means that each process needs to complete its own task (in fact, the corresponding Max value) and how many each of these resources are required. For example, through the above Max and alloction we can know need[0][3]=3, because the No. 0 process to complete its own task requires a total of 5 (Max) A third resource, has been assigned to it 2 (Allocation), also need 3 (need) Before you can play as a task. In other words, need = max-allocation.
Of course, if you want to implement a specific banker algorithm program, you do not have to adhere to the problem is not to define the array, the specific situation according to their own implementation to decide.
After understanding these 4 data structures, we can begin to explain the two algorithms of the banker algorithm.
Security state Detection algorithm
The flow of the security state detection algorithm is as follows:
1. First initialize two arrays, one is finish (length p), the other is available (length R), finish indicates whether a process has finished its task, and at first set all the elements to false, Available is the one that was mentioned above, representing the number of available per resource in the current system.
2. constantly scan all processes, currently scanning to process I, if:
A. Finish[i] = = False
B. need[i] <= Available
If both of these conditions are true, the 3rd step is taken, otherwise the 4th step is executed.
3. Available + = Allocation[i] finish[i] = True to continue with step 2nd.
4. if the elements in finish are true, that is, all processes have been executed, you can determine that the system is in a safe state!
If you take a closer look, you'll find that the security state detection algorithm principle is:
The system first assigns all the remaining resources to a process like a loan, but make sure that the process takes the "loan" to complete its task (that is, allocation+available >= Max), after the process has finished its task, the system puts the "loan "And all the resources previously allocated to it are recycled, as if the banker had recycled the loan with interest, and the system's" principal "became" Loan + interest ", that is, the current available equals the previous available plus the completed process allocation .
The system can then use this more "principal" to continue to lend to some other process, similarly, after the process has been completed, the system will be collected with the resources previously allocated to it back, so constantly reciprocating, This allows the system to accumulate available resources to address more demanding processes by lending all available resources to low-demand processes and continuously reclaiming resources. If the "principal" of the system is not sufficient to bring to any one process, it indicates that the system is currently in an unsafe state and deadlocks may occur at any time.
Resource Request algorithm
If a process I initiates a resource allocation request:
1. if request[i] > Need[i] reject the application
2. if request[i] > Available[i] reject the application
3. if both steps 1 and 2 are passed, the following calculations are performed:
Available-= Request[i]
Allocation[i] + = Request[i]
Need[i]-= Request[i]
This calculation does not really change the original available, allocation, and need, these calculated values are temporary
4. Use the security state detection algorithm to detect whether the system state is in a secure state after the completion of the calculation, and if it is in a secure state, the request of process I will take effect, otherwise the application can only be postponed.
The principle of the resource request algorithm is that the resource requested by the process is not more than the system currently available resources, the first agreed to the allocation test , and then through the security state detection algorithm to verify the security of such a distribution system, if it is safe to really agree to such distribution, if unsafe to postpone this allocation.
If this is a bit more general, then an example is given to illustrate a specific example of a banker's algorithm, which can be found in the Operation System concepts of Abraham Silberschatz:
Assuming there are now 5 processes P0-P1, there are three kinds of resource ABC, the current resource allocation of the system is as follows this list:
Let's take a look at the security state detection algorithm to see if the current system state is secure:
Because NEED[P1] < Available, so Available = 3 3 2 + 2 0 0 = 5 3 2, finish[p1] = True
Then available is 5 3 2, keep looking down.
Found NEED[P3] < Available, so Available = 5 3 2 + 2 1 1 = 7 4 3,finish[p3] = True
In the next
NEED[P4] < Available, Available = 7 4 3 + 0 0 2 = 7 4 5,finish[p4] = True
NEED[P0] < Available, Available = 7 4 5 + 0 1 0 = 7 5 5,finish[p0] = True
NEED[P2] < Available, Available = 7 5 5 + 3 0 2 = Ten 5 7,finish[p2] = True
All the elements in the finish are set to true, which means that all the processes have been executed and the system is in a safe state ~
If P1 now makes a request, ask = 1 0 2
Since 1 0 2 is less than NEED[P1] and less than available, so we call the resource request algorithm, the result of the calculation is as follows:
Available = 3 3 2-1 0 2 = 2 3 0
Allocation = 2 0 0 + 1 0 2 = 3 0 2
Need = 1 2 2-1 0 2 = 0 2 0
The data marked in red is a different part of the previous state
Then we call the security state detection algorithm check whether the change of the system is in a safe state, the steps and one side exactly the same, through the test will find to P1 1 0 2 after the system can still be in a safe state, so agreed to this allocation.
The banker algorithm for preventing deadlock