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
  • a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should...

    a) Implement the bottom-up dynamic programming algorithm for the knapsack problem in python. The program should read inputs from a file called “data.txt”, and the output will be written to screen, indicating the optimal subset(s). b) For the bottom-up dynamic programming algorithm, prove that its time efficiency is in Θ(nW), its space efficiency is in Θ(nW) and the time needed to find the composition of an optimal subset from a filled dynamic programming table is in O(n). Consider the following...

  • Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from...

    Part B (BI). Implement a Red-Black tree with only operation Insert(). Your program should read from a file that contain positive integers and should insert those numbers into the RB tree in that order. Note that the input file will only contain distinct integers. Print your tree by level using positive values for Black color and negative values for Red color Do not print out null nodes. Format for a node: <Node_value>, <Parent_value>). For example, the following tree is represented...

  • This is a C program question you will implement a program to show the performance of...

    This is a C program question you will implement a program to show the performance of FCFS scheduling algorithm with I/O burst. Your program should get a file (e.g., “jobs.txt”) as the command-line input, and read the contents of the file. This file contains a set of processes. For example, consider the file with the following content: 1:(45,15);(16,20);(80,10);(40,-1) 2:(15,10);(60,15);(90,10);(85,20);(20,-1) 3:(30,15);(40,20);(5,15);(10,15);(15,-1) In this example we have 3 processes, each process is represented in a separate line. The general format of a...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

  • Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should...

    Using Java programming language Your assignment is to implement a recursive reverse sorting algorithm. It should meet the following requirements: 1. The program shall graphically prompt the user for a file. 2. The program shall read the selected file which will contain 1 integer per line. 3. The program shall sort the values it reads from the file from largest to smallest. 4. The program shall write the values to an output file from largest to smallest in the same...

  • Please help with this python assignment. Thank you. question 1 Write a Python program to read...

    Please help with this python assignment. Thank you. question 1 Write a Python program to read a file line by line store it into a variable. question 2 Write a Python program to read a file line by line store it into an array. question 3 Write a python program to find the longest words. question 4 Write a Python program to count the number of lines in a text file. question 5 Write a Python program to count the...

  • This should be a program to implement the Boyer-Moore algorithm for string search in C. The...

    This should be a program to implement the Boyer-Moore algorithm for string search in C. The text is in file data 5.txt, which has 44049 lines of strings. A search patterns includes the 52 upper and lower case letters only. Search is case-sensitive. When a program is executed, it reads in the text, prompts for a pattern, finds all the occurrences of the pattern in the text, and reports the total number of occurrences found. Don’t remove any symbols (characters)...

  • Write an efficient java program to implement BST. Your java program should read n words and...

    Write an efficient java program to implement BST. Your java program should read n words and its corresponding French and store it in a BST. Then read every English word and print its corresponding French by searching in BST. Assume your java program is in engtofren.java file To compile: javac engtofren.java To execute: java engtofren< any data file name Your main method should be as follow: public static void main(String args[]) { engtofren bst = new engtofren (); // try{...

  • PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node...

    PYTHON ONLY Implement the Dijkstra’s Shortest path algorithm in Python. A graph with 10 nodes (Node 0 to node 9) must be implemented. You are supposed to denote the distance of the edges via an adjacency matrix (You can assume the edge weights are either 0 or a positive value). The adjacency matrix is supposed to be a 2-D array and it is to be inputted to the graph. Remember that the adjacency list denotes the edge values for the...

  • In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a seri...

    In either Java or Python 3, write a program that simulates a deterministic FSM. It will read from two input files. The first is a file describing an FSM The first line contains the alphabet as a series of characters separated by a single space - The second line contains the number of states as an integer k 2 1; states will be numbered 0,1,..., k -1. The start state is always state O The third line contains a series...

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