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
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)
Implement the Banker’s algorithm in Python which includes the Safety algorithm.Your program should read the system...
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 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...
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...
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...
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...
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 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...
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...
Your program must be named "banker" and will read the allocation, max, available, and request vectors from a file. The name of the file will be passed to your program as a command line argument. The input file format is the following: number of processes: n number of resource types: m An n x m allocation matrix An n x m max matrix A 1 x m available vector A i : 1 x m request vector Your program will...
In a sensible language ( any of C#/Java/Python) write a program which correctly calculates add, subtract, multiply and divide using our ‘minifloat’ binary format using an algorithm you code yourself. Some details: Your program only needs to work on two ‘numbers’ at a time, read those in from a text file – failure to read those values from a text file will result in a grade of 0. For each ‘number’ store the sign, exponent and mantissa separately. You can hard code your...