Banker Algorithm Learning Notes

Source: Internet
Author: User

last week's operating system experiment, that is, simulation to implement the banker algorithm, first of all, we should be familiar with the banker algorithm.

The banker algorithm is the most representative algorithm to avoid deadlock. Because the algorithm was originally designed for the banking system, to ensure that the bank in the issuance of cash loans, not to meet the needs of all customers. It can also be implemented in the OS to avoid deadlocks.

Algorithm Overview:

In order to implement the banker algorithm, each process enters the system, it must state that during the run, the maximum number of units per resource type may be required, and the number should not exceed the total amount of resources that the system has, and when the process requests a set of resources, the system must first determine whether sufficient resources are allocated to the process. If so, further calculate whether the system will be in an unsafe state after assigning the group of resources to the process. If not, assign the resource to it, or let the process wait.


definition of the system security state 1. Security Status In the method of avoiding deadlocks, the process is allowed to request resources dynamically, but the security of this resource allocation should be calculated before the system can allocate resources. If this allocation does not cause the system to enter an unsafe state, the resource is assigned to the process; otherwise, the process waits. Although not all unsafe states will inevitably turn to deadlock state, but when the system enters an unsafe state, it is possible to enter the deadlock state, conversely, as long as the system is in a safe state, the system can avoid entering the deadlock state.  Therefore, the essence of avoiding deadlock is: How to make the system not enter the unsafe state when the system is allocating resources.
using banker algorithms to avoid deadlocks
  1. Data structures in bankers ' algorithms (1) resource vector available can be used. This is an array of M elements, each of which represents a class of available resources, whose initial value is the number of all available resources in the system that are configured, and whose values change dynamically with the allocation and recycling of that class of resources. If Available[j]=k, it represents the existing RJ class resource K in the system. (2) max requirement matrix Max. This is a nxm matrix that defines the maximum demand for M-class resources for each process in n processes in a system. If max[i,j]=k, indicates that process I requires the maximum number of RJ class resources to be K. (3) Distribution matrix allocation. This is also a matrix of nx m , which defines the number of resources that each class of resources in the system currently has allocated to each process. If allocation[i,j]=k, indicates that process I currently has the number of R-J classes of resources K. (4) demand matrix need. This is also a matrix of nx m , which represents the number of resources required for each process. If need[i,j]=k, then the process I also need R J class resource K, to complete its task.
The following relationships exist between the above three matrices: need[i, J]=max[i, J]-allocation[i, j]     according to the above description, we can implement: set the data structure, in C language description:      
typedef struct//According to the description of the book, 3 kinds of resources {    int A;    int B;    int C;} resource;const int m=5; Number of processes resource available;//available resources resource max[m];//maximum requirements resource allocation[m];//current allocated resources resource need[m];// A variety of resources are also required int safeseq[m]; Safe sequence

2. Banker AlgorithmSet Request I is the process pi requests vector, if Request i[j]=k, indicates that the process P I need K R J type of resources. When P I makes a resource request, the system checks as follows: (1) If request I[j]≤need[i,j], turn to step (2) , otherwise it is considered an error, because it requires more resources than it announces maximum value. (2) If Request I[j]≤available[j], then turn to step (3); Otherwise, there is not enough resources, PI must wait. (3) The system is tempted to allocate resources to process P I and modify the values in the following data structures: 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 performs a security algorithm to check whether the system is in a secure state after this resource allocation. If security, the resources are formally assigned to the process pi to complete the allocation; otherwise, the temptation allocation will be invalidated, restore the original resource allocation state, let the process pi wait.
as described here, we can quickly write down the architecture of the banker algorithm:       
BOOL Banker (int process,resource *res) {    if (res->a<=need[process]. A && res->b<=need[process]. B &&res->c<=need[process]. C)//The request vector needs to be less than the value in the need matrix    {        if (res->a<=available.a && res->b<=available.b &&res- >C<=AVAILABLE.C)//The requested resource needs to be less than the value of the Available matrix        {            probealloc (process,res);//tempted to allocate            if (security ())// Security judgment            {                return true;            }            else            {                printf ("Allocation failed, cause: The system will go into an unsafe state and may cause a deadlock.") \ n ");                Rollbock (process,res);//Rollback            }        }        else        {            printf ("Security check failed. Cause: The request vector is larger than the available resource vector. \ n ");}    }    else    {        printf ("Security check failed. Reason: The request vector is larger than the demand vector. \ n ");    }    return false;}

heuristic allocations and rollbacks involved:
void Probealloc (int process,resource *res)//heuristic assignment {    available.a-=res->a;    available.b-=res->b;    available.c-=res->c;    Allocation[process]. a+=res->a;    Allocation[process]. b+=res->b;    Allocation[process]. c+=res->c;    Need[process]. a-=res->a;    Need[process]. b-=res->b;    Need[process]. C-=res->c;} void Rollbock (int process,resource *res)//rollback {    available.a+=res->a;    if allocation fails available.b+=res->b;    available.c+=res->c;    Allocation[process]. a-=res->a;    Allocation[process]. b-=res->b;    Allocation[process]. c-=res->c;    Need[process]. a+=res->a;    Need[process]. b+=res->b;    Need[process]. C+=res->c;}

3. Security Algorithms the security algorithms performed by the system can be described as follows:
(1) set two vectors: ① working vector work, which represents the number of types of resources that the system can provide to the process to continue running, which contains m elements that are work:=available at the beginning of the execution of a security algorithm. ②finish, which indicates whether the system has sufficient resources to be allocated to the process to make it run complete. Do Finish[i]:=false first, and then finish[i]:=true when there is enough resources to allocate to the process.
(2) Find a process from the process collection that meets the following criteria: ①finish[i]=false; ②need[i,j]≤work[j]; If found, perform step (3), otherwise, perform step (4).
(3) when the process Pi obtains the resources, can execute smoothly, until completes, and releases the resources allocated to it, therefore should execute: work[j]:= work[j]+allocation[i,j]; finish[i]:=true ; go to step (2);
(4) If the finish[i]=true of all processes are satisfied, the system is in a secure state; otherwise, the system is in an unsafe state.
The above banker's algorithm architecture already contains security judgments, which are directly based on the description of the security algorithm implementation:         
BOOL Security ()//safety algorithm {    Resource work=available;    BOOL finish[m]= {false,false,false,false,false};    int i,j=0;    for (i=0; i<m; i++)    {        if (finish[i]==false)        {            if (need[i]. A<=work. A&&need[i]. B<=work. B&&need[i]. C<=work. C)//Determine if there is sufficient resources to allocate            {work                . A+=allocation[i]. A;                Work. B+=allocation[i]. B;                Work. C+=allocation[i]. C;                Finish[i]=true; Description can be assigned                safeseq[j++]=i;//record the location of the security sequence                i=-1;    }}} for (int i=0; i<m; i++)//security state if full allocation is complete.    {        if (Finish[i]==false)            return false;    }    return true;}


4. Banker Algorithm Example
assume that there are five processes {P0,P1,P2,P3,P4} and three classes of resources {A,B,C} in the system, the number of resources is 10, 5, 7, and the resource allocation at T0 Time is shown. (Omit the parentheses of the second line of P1 first)
(1) The security of T0 Time: Using the security algorithm to analyze the resource allocation of T0 moment, there is a safe sequence {p1,p3,p4,p2,p0} at t0. Therefore, the system is safe. (2)? P1 Request resource: P1 issue request Vector Request1 (1,0,2), the system is checked by banker algorithm: ①request1 (1,0,2) ≤need1 (1,2,2) ②request1 (1,0,2) ≤available1 (3,3,2) the ③ system assumes that resources can be allocated for P1 and modifies available,allocation1 and Need1 vectors, resulting in resource changes as shown in parentheses④ re-use the security algorithm to check if the system is secure at this time. (3)? P4 Request resource: P4 issue request Vector Request4 (3,3,0), the system is checked by banker algorithm: ①request4 (3,3,0) ≤need4 (4,3,1); ②request4 (3,3,0) ≥available (2,3,0), let P4 wait.
(4)? P0 Request resource: P0 issue request Vector Requst0 (0,2,0), the system is checked by banker algorithm: ①request0 (0,2,0) ≤need0 (7,4,3); ②request0 (0,2,0) ≤available (2,3,0); The ③ system temporarily assumes that resources can be allocated for P0 and modifies the data. (5) Security check: The available resources available (2,1,0) can not meet the needs of any process, so the system into an unsafe state, at this time the system does not allocate resources.
The banker algorithm written according to the above example test:      Complete program code: http://download.csdn.net/detail/u014253173/8233559Reference http://blog.csdn.net/cout_sev/article/details/24980627

Banker Algorithm Learning Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.