Aet'S Atharva College of Engineering: Mini-Project Synopsis On
Aet'S Atharva College of Engineering: Mini-Project Synopsis On
MINI-PROJECT SYNOPSIS ON
“Banker’s Algorithm”
Guide
Prof. Divya Kumawat
Aim :- Banker’s Algorithm for deadlock handling.
Objective:- Implementation of Banker algorithm that will use to prevent deadlock which
occour when the process are not properly synchronized.
Requirements:- Python IDE version greater than 3 to run the implemented code.
Block Diagram :-
Project Description :-
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all
resources, then makes an “s-state” check to test for possible activities, before deciding whether
allocation should be allowed to continue
Steps of Algorithm:
1. Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work= Available
Finish [i]=false; for i=1,2,……,n 2.
Find an i such that both
a) Finish [i]=false
b) Need_i<=work if no such i exists goto step (4)
3. Work=Work + Allocation_i Finish[i]= true goto step(2)
4. If Finish[i]=true for all i,
then the system is in safe state
Code:-
import numpy as np
def banker(maxn,alloc,avail,N,M):
f = [0,0,0,0,0]
need = np.zeros((N,M))
ans = np.zeros((5))
for i in range(0,N):
for j in range(0,M):
need[i,j]= need[i,j]+(maxn[i,j]-alloc[i,j])
count=0
while(count<N):
for i in range(0,N):
if(f[i]==0):
flag = 0
sd = need[i,:]
if(np.any(sd > avail)):
flag = 1
if(flag == 0):
avail = avail + alloc[i]
f[i] = f[i]+1
ans[count] = ans[count] + i
count = count + 1
flag = 0
print('\n')
print('The Need Matrix is:\n')
print(' R0 R1 R2 \n')
print(need)
for i in range(0,N):
if(f[i]==0):
flag = 1
break;
if(flag==0):
print('\n')
print('Following is the safe sequence:',end = ' ')
for i in range(0,N):
print('p->'+str(ans[i]),end = ' ')
return flag
N = int(input('ENTER THE NO OF PROCESS:'))
M = int(input('Enter the no of resources:'))
r_array = list()
max_need0 = list()
max_need = list()
alloc0 = list()
alloc = list()
need = list()
s=np.zeros((1,3))
s1=np.zeros((1,3))
avail0 = list()
for i in range(0,M):
print('Enter The No. Of Instances Of Resource R'+str(i)+':')
n = int(input())
r_array.append(n)
for i in range(0,N):
print('\n\nEnter the Max for P'+str(i)+':')
for j in range(0,M):
print('Enter the Max need for R'+str(j)+' Resource:')
num = int(input())
max_need0.append(num)
l = np.array(max_need0)
max_need = l.reshape(N,M)
for i in range(0,N):
print('\n\nEnter the Alocation for P'+str(i)+':')
for j in range(0,M):
print('Enter the Alocation for R'+str(j)+' Resource:')
num1 = int(input())
alloc0.append(num1)
q = np.array(alloc0)
alloc = q.reshape(N,M)
for i in range(0,N):
s= s+alloc[i,0:M]
avail0 = r_array - s
avail = np.array(avail0)
print('\nThe avail Matrix is:\n')
print(' R0 R1 R2 \n')
print(avail)
flag = banker(max_need,alloc,avail,N,M)
if(flag == 1):
print('Not in the safe state!!')
else:
re = list()
num = int(input('\nEnter the prosses no. that request for resource:\n'))
for j in range(0,M):
print('\nEnter the request for resource R'+str(j)+':')
new = int(input())
re.append(new)
request = np.array(re)
need = max_need[num,:] - alloc[num,:]
if(np.any(request>need)):
print('\nDeny Request'+''+str(num))
else:
if(np.any(request>avail)):
print('\nDeny Request'+''+str(num))
else:
print('\nGrant Request'+''+str(num))
alloc[num,:] = alloc[num,:] + request
availM = avail - request
print('\nThe avail Matrix is:\n')
print(' R0 R1 R2 \n')
print(availM)
banker(max_need,alloc,availM,N,M)
Result:-
Conclusions & future enhancements:- Thus in this project we implemented the
Banker’s algorithm that deal with the deadlock handling.
In future we will look it to brought this in practice while designing an operating system which
help the process in the operating system to work efficiently and to prevent the deadlock of the
process.