Sie sind auf Seite 1von 6

AIM: IMPLEMENTATION OF DEADLOCK USING BANKER’S ALGORITHM.

THEORY : BANKER’S ALGORITHM

• The Banker’s algorithm is best known of avoidance strategies. The resource allocation
graph algorithm suitable to resource allocation system with single instance of each
resource type. It is not suitable for multiple instance of each resource type.
• Banker’s algorithm is suitable for resource allocation system with multiple instance of
each resource type. Algorithm makes decision on granting resource best on wether or
not granting request will put system into an unsafe state.
• Several data structure maintained in Banker’s algorithm, say Available, Max,
Allocation, Need.

ALGORITHM :

• The Banker’s algorithm is run by operating system whenever a process request


resource. The algorithm avoid deadlock by denying or postponing the request if it
determine that accepting the request could put the system in an unsafe state. When a
new process enter a system, it declare maximum number of instance of each resource
type that may not exceed total number of resources in the system. Also, when a
process gets all its requested resources it must return them in finite amount of time.
• Banker’s algorithm works on three strategies:
I. How much of each resource each process could possible request
II. How much of each resource each process it currently holding
III. How much of each resource the system currently has available

Safe and Unsafe State


• A state is consider safe if it possible for all processes to finish executing .Since can-
not know when process terminate, or how many resource it will have requested by
then, the system assume that all processes will eventually attempt to acquire their
stated maximum resource and terminate soon afterward.
• This is a reasonable assumption in most case since the system is not particularly
concerned with how long each process runs. Also, if a process terminate without
acquiring its maximum resource ,it only make it easier on the system.
• Given that assumption, the algorithm determine if a state is safe by trying to find a
hypothetical set of request by the processes that would allow each to acquire its
maximum resource and then terminate. Any state where no such set exists is an
unsafe state.
Request
• When the system receive a request for resources, it runs the Banker’s algorithm to
determine if it is safe to grant the request. The algorithm is fairly straight forward
once the distinction between safe and unsafe state is understood.
I. Can the request be granted?
o If not ,the request is impossible and must either be denied or put on
waiting list.
II. Assume that request is granted
III. Is the new state safe?
o If so grant the request
o If not, either deny the request or put it on a waiting list

1
• Whether the system deny or postponed an impossible or unsafe request is a decision
specific to operating system.

PROGRAM :

import java .util.*;


import java.io.*;

class Banker_algo
{
public static void main(String args[])throws IOException
{
Scanner sc = new Scanner(System.in);
int Allocation[][]=new int[5][3];
int Max[][]=new int[5][3];
int Need_req[][]=new int[5][3];
int Available[]=new int[3];
int p[]={0,1,2,3,4};
System.out.println("Enter the Allocation");
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
Allocation[i][j]=sc.nextInt();
System.out.println("Enter the maximum requirment");
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
Max[i][j]=sc.nextInt();
System.out.println("Enter the Availaible fpr processes zero for (p[0])");
for(int j=0;j<3;j++)
Available[j]=sc.nextInt();
//to find the required need for process max[][]-Allocation[][]
for(int i=0;i<5;i++)
for(int j=0;j<3;j++)
Need_req[i][j]=Max[i][j]-Allocation[i][j];
int x=0;
while (x<5)
{
for(int i=0;i<5;i++)
{
for(int j=0;j<1;j++)
if(Need_req[i][j]<=Available[j] && Need_req[i][j+1]<=Available[j+1] &&
Need_req[i]
[j+2]<=Available[j+2])
{
for(int s=0;s<3;s++)
{
Available[s]= Available[s]-Need_req[i][s]+Max[i][s];
Need_req[i][s]=100;
}
System.out.println("Available =");
for(int s=0;s<3;s++)

2
System.out.println(" "+Available[s]+" ");
System.out.println();
System.out.println("process p["+i+"] is over ");
x=x+1;
}
}
}//while
}///main
}//class
OUTPUT:

Enter the Allocation


0
1
0
2
0
0
3
0
2
2
1
1
0
0
2
Enter the maximum requirment
7
4
3
1
2
2
6
0
0
0
1
1
4
3
1
Enter the Availaible for processes zero for (p[0])
3
3
2
Available =
5
3

3
2

process p[1] is over


Available =
8
3
4

process p[2] is over


Available =
10
4
5

process p[3] is over


Available =
10
4
7

process p[4] is over


Available =
10
5
7

process p[0] is over

4
FLOWCHART: START

Declare array Allocation, Max, Need_req, Available, p and initialise p to{0,1,2,3,4}

Print “Enter the Allocation”

for i=0 to 5 do

for j=0 to 3 do

Read the Allocation.

Print “Enter the maximum requirement”.

for i=0 to 5 do

for j=0 to 3 do

Read the maximum requirement”.

Print “Enter the Availaible fpr processes zero for (p[0])”.

for j=0 to3 do

Read the Available for process.

for i=0 to5 do

for j=0 to3 do

Need_req[i][j] Max[i][j]-Allocation[i][j]
Max[i][j]-Allocation[i][j]

A 5
A

Initialise x to 0.

repeat until x<5 B

for i=0 to5 do

for j=0 to1 do

(Need_req[i][j]<=Available[j] && Need_req[i][j+1]<=Available[j+1] && Need_req[i][j+2]<=Available[j+2]

for s=0 to3 do

Available[s]= Available[s]-Need_req[i][s]+Max[i][s]
Need_req[i][s]=100

Print “Available =”.

for s=0 to3 do

Print "Available[s]+ ="

Print "process p["+i+"] is over "

xx+1

STOP

CONCLUSION:
Hence we have studied and implemented Bankers algorithm for deadlock detection.

Das könnte Ihnen auch gefallen