# The Banker's Algorithm for Multiple Resources by hcj

VIEWS: 348 PAGES: 4

• pg 1
```									The Banker’s Algorithm for Multiple Resources.

This algorithm is very similar to deadlock detection algorithm. But in
deadlock detection, requested resources are granted to processes whenever
possible ( this algorithm is static ). In the banker’s algorithm decision is
This algorithm uses the same data ( as deadlock detection algorithm ): total
amount of each resource in the system {Ri} i = 1,m ; resources available
{Ai} i=1,m ; allocation matrix {Cij} i = 1,n; j = 1,m ; maximum matrix
(maximum requirement or request of each process for each resource).
In the algorithm steps, the auxiliary matrix Need = Maximum – Allocation is
used.
The strategy is as following: ensure that the system is in a safe state. When
process makes a request for a set of resources, assume that the request is
granted, update the system state accordingly, and then determine if the result
is a safe state. If so, grant the request and, if not, block the process until it is
safe to grant the request.

Safety algorithm (banker’s algorithm)
Step 1
Initialize a temporary vector W (Work) to equal the available vector A.
Step 2
Find an index i (row i) such that
Needi  = W
If no such row exists, the system will deadlock, since no process can run to
completion
Step 3
If such a row is found, mark this process as finished, and add all its
resources to W vector
W = W + Ci,*
Go to step 2, until either all processes are marked terminated ( in this case
initial state is safe ), or until a deadlock occurs, in which the state is not safe.

Resource – request algorithm
Let the request vector Rij = k is coming, meaning that process Pi wants k
instances of resource j . The following actions are taken:
Step 1
If Requesti < = Needi , go to step 2. Otherwise, error
Step 2
If Requesti < = Available , go to step 3. Otherwise, Pi must wait, since
resources are not available
Step 3
Modify the state ( the system pretend to have allocated the requested
resources to process Pi )
Available = Available - Requesti
Allocationi = Allocationi + Requesti
Needi = Needi - Requesti
If the resulting state is safe, the transaction is completed and process Pi is
allocated its resources. If new state is unsafe, then Pi must wait for Requesti
and the old state is restored.

Example
4 processes P1, P2, P3, P4 and 3 resources R1, R2, R3 ( with the total
amounts 9, 3, and 6 units respectively ) are given. The initial state is as:

Allocation        Maximum          Need
R1 R2 R3          R1 R2 R3        R1 R2    R3
P1   1 0 0             3 2 2           2 2      2
P2   6 1 2             6 1 3           0 0      1
P3   2 1 1             3 1 4           1 0      3
P4   0 0 2             4 2 2           4 2      0

Resource vector          R1 R2 R3
9 3 6
Available vector          0 1 1

Can any of the 4 processes be run to completion with the resources
available? ( is this a safe state? )
Yes, process P2 can run until completion after assigning 1 unit of R3 to him.
Let us assume that this is accomplished. The resulting state now is as ( after
P2 releases the resources! ):
Allocation         Maximum       Need
R1 R2 R3          R1 R2 R3 R1 R2 R3
P1 1 0 0                 3 2 2        2 2 2
P2 0 0 0                 0 0 0        0 0 0
P3 2 1 1                 3 1 4        1 0 3
P4 0 0 2                 4 2 2        4 2 0
Available vector        R1 R2 R3
6 2 3

Now, each of the remaining processes could be completed. Suppose, we
choose P1, allocate the required resources, complete P1, and return all of its
resources to the available pool. The resulting state is:

Allocation        Maximum         Need
R1 R2 R3          R1 R2 R3       R1 R2    R3
P1   0 0 0             0 0 0          0 0      0
P2   0 0 0             0 0 0          0 0      0
P3   2 1 1             3 1 4          1 0      3
P4   0 0 2             4 2 2          4 2      0

Available vector        R1 R2 R3
7 2 3

Next, we can complete P3, resulting the state below:

Allocation        Maximum         Need
R1 R2 R3          R1 R2 R3       R1 R2    R3
P1   0 0 0             0 0 0          0 0      0
P2   0 0 0             0 0 0          0 0      0
P3   0 0 0             0 0 0          0 0      0
P4   0 0 2             4 2 2          4 2      0

Available vector        R1 R2 R3
9 3 4

Finally, we can complete P4.
At this point, all of the processes have been run to completion. Thus, the
initial state is a safe state.

Consider the small different situation with the same example. Initial state is
defined by the matrices:
Allocation        Maximum          Need
R1 R2 R3          R1 R2 R3        R1 R2   R3
P1   1 0 0             3 2 2           2 2     2
P2   5 1 1             6 1 3           1 0     2
P3   2 1 1             3 1 4           1 0     3
P4   0 0 2             4 2 2           4 2     0

Resource vector         R1 R2 R3
9 3 6
Available vector         1 1 2

Suppose that P1 makes the request for one additional unit each of R1 and
R3; if we assume that request is granted, we will come to the new state:

Allocation        Maximum          Need
R1 R2 R3          R1 R2 R3        R1 R2   R3
P1   2 0 1             3 2 2           1 2     1
P2   5 1 1             6 1 3           1 0     2
P3   2 1 1             3 1 4           1 0     3
P4   0 0 2             4 2 2           4 2     0

R1 R2 R3
Available vector          0 1 1

Is this a safe state? No, because each process will need at least 1 additional
unit of R1, and there are none available. Thus, on the basis of deadlock
avoidance, the request by P1 should be denied and P1 should be blocked.

```
To top