The banker algorithm for avoiding deadlock

Source: Internet
Author: User
Tags data structures

The deadlock in the process management in the previous blog we talked about the various problems of deadlock in the process management, which left the famous banker algorithm in the deadlock avoidance algorithm, the following is a detailed interpretation for everyone.1. Safety sequenceBefore we talk about the banker algorithm, we first introduce the definition of a security sequence: the so-called system is secure, which means that all processes in the system can allocate resources in a certain order, and in turn, this process sequence {P1,P2,...,PN} is a safe sequence. If such a security sequence exists, the system is secure, and if the system does not have such a security sequence, the system is unsafe.
The security sequence {P1,P2,...,PN} is composed of: for each process pi, the additional resources it needs can be satisfied by the current available resources in the system plus all processes PJ currently occupies the sum of resources, then {P1,P2,...,PN} is a security sequence, then the system is in a secure state, Does not enter the deadlock state.
Although there must be no deadlock when there is a safe sequence, a deadlock may not occur when the system enters an unsafe state (the necessary conditions for four deadlocks occur simultaneously). Of course, after a deadlock occurs, the system must be in an unsafe state.

2. Banker Algorithm(Please forgive me to use the text on the wiki to describe it for the sake of familiarity with English) For the Banker's algorithm to work, it needs to know three things:
How much of all resource each process could possibly request[claims] what much of each resource each process is currently Holding[allocated] How much of each resource the system currently have available[available] Resources may is allocated to a Process only if it satisfies the following conditions:
Request≤max, Else set error condition as process have crossed maximum claim made by it. Request≤available, else process waits until resources is available.
BASIC data structures to is maintained to implement the Banker ' s algorithm:

Available:a vector of length m indicates the number of Available resources of each type. If Available[j] = k, there is k instances of resource type Rj Available. Max:an NXM Matrix defines the maximum demand of each process. If Max[i,j] = k, then Pi could request at most K instances of resource type Rj. Allocation:an NXM Matrix defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated K instance of resource type Rj. Need:an nxm Matrix Indicat Es the remaining resource need of each process. If Need[i,j] = k, then Pi could need k more instances of the resource type Rj to complete task. NOTE:NEED[I,J] = max[i,j]-allocation[i,j].


Banker algorithm: Set process I request request[j], then the banker algorithm according to the following rules to judge.
(1) If REQUEST[J]≤NEED[I,J], then turn (2), otherwise it is considered an error.
(2) if REQUEST[J]≤AVAILABLE[J], then turn to (3); otherwise, there is not enough resources, PI will wait.
(3) Assuming that the application for process I has been approved, the system status is modified:
Available[j]=available[j]-request[i]
ALLOCATION[I,J]=ALLOCATION[I,J]+REQUEST[J]
NEED[I,J]=NEED[I,J]-REQUEST[J]

(4) The system performs security checks, such as security, the allocation is set up; otherwise, the exploratory allocation is void, the system reverts to its original state, and the process waits.
Security check (1) set two working vectors work=available;finish[i]=false
(2) Find a process from the process collection that satisfies the following conditions,
Finish [I]=false;
NEED[I,J]≤WORK[J];
If found, execute (3); otherwise, execute (4)
(3) The establishment process obtains the resources, can carry on smoothly, until completes, thus releases the resources.
WORK[J]=WORK[I]+ALLOCATION[I,J];
Finish[i]=true;
Go to step 2;
(4) If all processes are finish[i]=true, it is safe, otherwise the system is unsafe.


Because of the time is not early on the wiki to borrow the C language implementation code, another day with Java implementation again.[CPP]  View plain copy print? /*program to implement banker ' s algorithm    *   ------------ --------------------------------*/   #include  <stdio.h>   int curr[5][5],  maxclaim[5][5], avl[5];   int alloc[5] = {0,0,0,0,0};   int  maxres[5], running[5], safe=0;   Int count = 0, i, j, exec,  r, p,k=1;       int main ()    {        printf ("\nenter the number of processes: ");       &NBSP;SCANF ("%d", &p);           for (i=0;i<p;i++)        {           running[i]=1;           count++;       }           printf ("\nenter the number of  resources:  ");       scanf ("%d ", &r);           for (i=0;i<r;i++)        {             printf ("\nenter the resource for instance %d:   ", k++);           scanf ("%d ", &maxres[i]);        }           printf ("\nenter  maximum resource table:\n ");       for (i=0;i<p;i++)        {           for (j=0;j<r;j++)             {             &nbsP;&NBSP;&NBSP;SCANF ("%d", &maxclaim[i][j]);           }        }           printf ("\nenter  allocated resource table:\n ");       for (i=0;i<p;i++)         {           for (

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.