Question

Implement the Banker’s algorithm in Python which includes the Safety algorithm.Your program should read the system...

Implement the Banker’s algorithm in Python which includes the Safety algorithm.Your program should read the system configuration information from a data file (i.e., sys_config.txt). More specifically, this data file contains the ‘Available’vector, ‘Max’matrix, ‘Allocation’matrix in the following format.

Allocation

Process 0: 0 1 0

Process 1: 2 0 0

Process 2: 3 0 2

Pro

cess 3: 2 1 1

Process 4: 0 0 2

Max

Process 0: 7 5 3

Process 1: 3 2 2

Process 2: 9 0 2

Process 3: 2 2 2

Process 4: 4 3 3

Available

3 3 2

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Banker's Algorithm is named so because of the mechanism used by the banks in granting loans to its customers. Basically when a customer applies for a loan then the bank calculates the total money it has and then the amount of loan applied is deducted from that amount and then it is made sure that the amount left is greater than the amount deposited in the bank by all customers.

In computer applications, Banker's algorithm is used to calculate the resource allocation in computer systems and avoiding deadlock condition in resource request and allocation.

In Banker's algorithm, there are certain terminologies that we need to consider before proceeding further such as :

1. Available vector - Array of length m. It represents the number of resources available. Available[ j ] = k means there are ‘k’ instances of resource type Rj.

2. Allocation matrix - an n x m matrix which represents the number of resources of each type currently allocated to each process. If Allocation[i][j] = k, then process P(i) is currently allocated k instances of resource type R(j)

3. Max matrix - an array of size n*m that defines the maximum demand of each process in a system. Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.

Now we need to calculate Need matrix which is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.

Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj

Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]

Implementation:

import json

# Number of processes

P = 5

# Number of resources

R = 3

# Function to find the need of each process

def calculateNeed(need, maximum, allocation):

# Calculating Need of each P

for i in range(P):

for j in range(R):

need[i][j] = maximum[i][j] - allocation[i][j]

# Function to find the system is in

# safe state or not

def isSafe(processes, available, maximum, allocation):

need = []

for i in range(P):

l = []

for j in range(R):

l.append(0)

need.append(l)

calculateNeed(need, maximum, allocation)

# Mark all processes as infinish

finish = [0] * P

# To store safe sequence

safeSeq = [0] * P

# Make a copy of available resources

work = [0] * R

for i in range(R):

work[i] = available[i]

# While all processes are not finished

# or system is not in safe state.

count = 0

while (count < P):

found = False

for p in range(P):

# First check if a process is finished,

# if no, go for next condition

if (finish[p] == 0):

# Check if for all resources

# of current P need is less

# than work

for j in range(R):

if (need[p][j] > work[j]):

break

# If all needs of p were satisfied.

if (j == R - 1):

for k in range(R):

work[k] += allocation[p][k]

# Add this process to safe sequence.

safeSeq[count] = p

count += 1

# Mark this p as finished

finish[p] = 1

found = True

# If we could not find a next process

# in safe sequence.

if (found == False):

print("System is not in safe state")

return False

return True

# Driver code

if __name__ =="__main__":

with open('sys_config.txt') as conf_file:

config = conf_file.read()

config = json.loads(config)

processes = config['processes']

available = config['available']

maximum = config['available']

allocation = config['allocation']

# Check system is in safe state or not

isSafe(processes, available, maximum, allocation)

Add a comment
Know the answer?
Add Answer to:
Implement the Banker’s algorithm in Python which includes the Safety algorithm.Your program should read the system...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT