Question

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 argumen...

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 output the following in a standardized format.

  1. Echo the number of processes.
  2. Echo the number of resource types.
  3. Echo the allocation matrix. Label the processes and resource types (see sample output).
  4. Echo the max matrix. Label the processes and resource types (see sample output).
  5. Compute and print the need matrix. Label the processes and resource types (see sample output).
  6. Echo the available vector. Label the resource types.
  7. Compute if the system is in a safe state.
  8. Echo the request vector. Label the process making the request and resource types (see sample output).
  9. Compute if the request can be granted.
  10. Compute the new available vector (see sample output).

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Input

5

4

0 0 1 2
1 0 0 0
1 3 5 4
0 6 3 2
0 0 1 4

0 0 1 2
1 7 5 0
2 3 5 6
0 6 5 2
0 6 5 6

1 5 2 0

1:0 4 2 0

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Sample Output

There are 5 processes in the system.

There are 4 resource types.

The Allocation Matrix is...
   A B C D
0: 0 0 1 2
1: 1 0 0 0
2: 1 3 5 4
3: 0 6 3 2
4: 0 0 1 4

The Max Matrix is...
   A B C D
0: 0 0 1 2
1: 1 7 5 0
2: 2 3 5 6
3: 0 6 5 2
4: 0 6 5 6

The Need Matrix is...
   A B C D
0: 0 0 0 0
1: 0 7 5 0
2: 1 0 0 2
3: 0 0 2 0
4: 0 6 4 2

The Available Vector is...
A B C D
1 5 2 0

THE SYSTEM IS IN A SAFE STATE!

The Request Vector is...
  A B C D
1:0 4 2 0

THE REQUEST CAN BE GRANTED!

The Available Vector is...
A B C D
1 1 0 0
0 0
Add a comment Improve this question Transcribed image text
Answer #1

I am going to write this program in Python.

Following is the code for the same:

import sys

def print_output(file_name):

        f = open(file_name, 'r')

        lines = [line for line in f.readlines() if line.strip()]

        i = 0

        np = int(lines[i].strip())

       print "There are " + str(np) + " processes in the system.\n"

       i = i+1

      nr = int(lines[i].strip())

      print "There are " + str(nr) + " resource types.\n"

       i = i+1

      al_mat = [[0 for x in range(nr)] for y in range(np)]

      print "The Allocation Matrix is..."

      print " ",

      for char in range(65, 65+nr):

         print " " + chr(char),

     print ""

     for j in range(0, np):

        print str(j) + ":",

        for k in range(0, 2*nr-1, 2):

            al_mat[j][k/2] = int(lines[j+i][k])

            print " " + lines[j+2][k],

       print ""

    i = i+np

    max_mat = [[0 for x in range(nr)] for y in range(np)]

    print "\nThe Max Matrix is..."

    print " ",

    for char in range(65, 65+nr):

        print " " + chr(char),

   print ""

for j in range(0, np):

print str(j) + ":",

for k in range(0, 2*nr-1, 2):

max_mat[j][k/2] = int(lines[j+i][k])

print " " + lines[j+i][k],

print ""

need_mat = [[0 for x in range(nr)] for y in range(np)]

print "\nThe Need Matrix is..."

print " ",

for char in range(65, 65+nr):

print " " + chr(char),

print ""

for j in range(0, np):

print str(j) + ":",

for k in range(0, nr):

need_mat[j][k] = max_mat[j][k] - al_mat[j][k]

print " " + str(need_mat[j][k]),

print ""

i = i+np

avail_vec = [0 for x in range(nr)]

safe = 0

print "\nThe Available Vector is..."

for char in range(65, 65+nr):

print chr(char) + " ",

print ""

for k in range(0, 2*nr-1, 2):

avail_vec[k/2] = int(lines[i][k])

print lines[i][k] + " ",

if (avail_vec[k/2] != 0):

safe = 1

if (safe):

print "\n\nTHE SYSTEM IS IN A SAFE STATE!"

else:

print "\n\nTHE SYSTEM IS NOT IN A SAFE STATE!"

i = i+1

req_vec = [0 for x in range(nr)]

grant = 1

print "\nThe Request Vector is..."

print " ",

for char in range(65, 65+nr):

print " " + chr(char),

print ""

print lines[i][:2],

for k in range(2, 2*nr+1, 2):

req_vec[k/2-1] = int(lines[i][k])

print lines[i][k] + " ",

avail_vec[k/2-1] = avail_vec[k/2-1] - req_vec[k/2-1]

if (avail_vec[k/2-1] < 0):

grant = 0

if (grant):

print "\n\nTHE REQUEST CAN BE GRANTED!"

else:

print "\n\nTHE REQUEST CAN NOT BE GRANTED!"

print "\nThe Available Vector is..."

for char in range(65, 65+nr):

print chr(char) + " ",

print ""

for k in range(0, nr):

print str(avail_vec[k]) + " ",

def main():

if len(sys.argv) < 2:

print('Please provide the input file path name while running the program.')

sys.exit(1)

file_name = sys.argv[1]

print_output(file_name)

if __name__ == '__main__':

main()

max mat [I0 for x in range (nr)] for y in range (np)l print nThe Max Matrix is. print , for char in range (65, 65+nr): pri

avail_vec [0 for x in range(nr)] safe print nThe Available Vector is... for char in range (65, 65+nr): print chr(char) + ,

Add a comment
Know the answer?
Add Answer to:
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 argumen...
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
  • Part B (10) Deadlock Avoidance Consider the following maximum-claim reusable resource system with four processes and...

    Part B (10) Deadlock Avoidance Consider the following maximum-claim reusable resource system with four processes and three resource types. The maximum claim matrix is given by C [4 3 5 11 1 41 1 4 6 13 1 6] where Cij denote maximum claim of process i for resourcej. The total units of each resource type are given by the vector (5, 8, 15). The current allocation of resources is given by the matrix To 1 4] 2 0 1...

  • Consider the following snapshot of a system: Allocation P R1 R2 R3 R4 P1 0 0...

    Consider the following snapshot of a system: Allocation P R1 R2 R3 R4 P1 0 0 1 2 P2 1 0 0 0 P3 1 3 5 4 P4 0 6 3 2 P5 0 0 1 4 P represents processes R represents resources Need P R1 R2 R3 R4 P1 0 0 1 2 P2 1 7 5 0 P3 2 3 5 6 P4 0 6 5 2 P5 0 6 5 6 P represents processes R represents...

  • a. A system has two processes and three identical resources. Each process needs a maximum of...

    a. A system has two processes and three identical resources. Each process needs a maximum of two resources. Is deadlock possible? Explain your answer. b. A system has 4 processes, P1 through P4, and 5 types of resources, R1 through R5. Existing resource vector E = (3, 2, 1, 2, 2) Current allocation matrix C = R1 R2 R3 R4 R5 P1 1 1 0 0 0 P2 0 0 1 0 0 P2 1 0 0 20 P4 0...

  • Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt...

    Hello everyone. I have a bankers algorithm written in java that gets inputs from a .txt file. The file has 7 processes and 5 resources however, when the program runs, it doesn't sum the resource columns correctly. The program runs correctly for smaller matricies (4 processes, 3 resources), not sure what the issue is and have been looking over the code for awhile so maybe another set of eyes would help...thanks BankersAlgorithm.java /** * This program implements Bankers algorithm which...

  • Consider the following snapshot of a system: Process РО P1 P2 P3 P4 Allocation A B...

    Consider the following snapshot of a system: Process РО P1 P2 P3 P4 Allocation A B C D 2013 2 2 1 0 3 1 2 1 0 4 1 0 4 2 1 2 Max A B C D 5 1 1 6 3 2 1 1 3 2 2 1 4 6 1 2 5 3 2 5 Using the banker's algorithm, determine whether or not each of the following states is unsafe. If the state is safe,...

  • Write a C program, named sortit, that reads three integers from the command line argument and...

    Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • 6. Create a text file entitled "your last name" using the cat command, it must contain...

    6. Create a text file entitled "your last name" using the cat command, it must contain 2 complete sentences.      2. Execute a cat command to verify its contents.    3. Execute ls -l to see protection levels.     4. Modify your textfile’s protection so that the group domain^users has read/write capabilities in your file.     5. Execute ls -l to see new protection levels.

  • Write a Python program named aIP.py which will read data from a file named wireShark.txt and...

    Write a Python program named aIP.py which will read data from a file named wireShark.txt and extract all the pairs of source and destination ip addresses and output them in pairs to another file called IPAddresses.txt , one per line, listing source and destination. Example of output: Source                      Destination 192.168.1.180         239.255.255.250 Detailed Requirements: You will read from a file called wireShark.txt which, to avoid problems with finding paths, will be located in the same directory as your code You will...

  • 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...

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