Question

In: Computer Science

Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes...

Implement the Banker's algorithm for deadlock avoidance, that works on a given set of N processes and M resource types (N<10,M<10). Use C/C++/C# or Java for the implementation, with a simple text interface, where the user enters only the name of the input file (text only). The program reads all the necessary input data from that file.

The input data and result is then displayed on the screen.

You may use your program to validate the example you gave in the Week 4 discussion.

Deliverables: the source code + a screenshot of the program showing an execution example + the list of ALL available solutions for the Example posted in the Week 4 Discussions area.

IMPORTANT: The grading scale for this assignment is all follows:

- max score is 70% if you use a GREEDY approach (will find one solution, but not always).

- max score is 100% if you use BACKTRACKING and find all solutions

Solutions

Expert Solution

please please thumbs up!!!

hope it will help uh out!!

Code::
(IN JAVA PROGRAMMING LANGUAGE)

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

import java.io.*;
import java.util.StringTokenizer;
import java.util.Scanner;
class BankerAlgorithm
{
public static void main(String[]args)throws IOException,FileNotFoundException
{
int num_process = 0,num_resources = 0,lineCount = 0;
int [] sumColumn, sumRow,resourceVector,availableVector;
int [] current_available,process_order;
int index = 0;
boolean finish[];
int [][] max_resource_matrix,allocationMatrix, needMatrix;
boolean isSafe = false;
String line,filename;
StringTokenizer lines;
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
System.out.print("Enter the name of the input file : ");
filename = input.nextLine();
FileReader fr = new FileReader(filename); // build input stream
BufferedReader br = new BufferedReader(fr);
line = br.readLine(); // Read first line
num_process = Integer.parseInt(line);
line = br.readLine();
num_resources = Integer.parseInt(line);
max_resource_matrix = new int[num_process][num_resources]; //Create 2D array for each of the m process and n resources
allocationMatrix = new int[num_process][num_resources];
needMatrix = new int [num_process][num_resources];   
resourceVector = new int[num_resources];
//Create availableVector
availableVector = new int[num_resources];
current_available = new int[num_resources];
finish = new boolean[num_process];
process_order = new int[num_process];
sumColumn = new int[num_resources];
sumRow = new int[num_process];
line = br.readLine();
// Read the file and get the claimMatrix from the file
while(line != null && lineCount < num_process)
{
lines = new StringTokenizer(line);
if(lines.hasMoreTokens())
{
for(int j = 0; j < num_resources; j++)
{
max_resource_matrix[lineCount][j]=Integer.parseInt(lines.nextToken());
}
}
line = br.readLine();
lineCount++;
}
lineCount = 0;
// Read the file and get the Allocation Matrix   
while(line != null && lineCount < num_process)
{
lines = new StringTokenizer(line);
if(lines.hasMoreTokens())
{
for(int j = 0; j < num_resources; j++)
{
allocationMatrix[lineCount][j] =
Integer.parseInt(lines.nextToken());
}
}
line = br.readLine();
lineCount++;
}
// Read the last line and set Resource Vector
lines = new StringTokenizer(line);
while(lines.hasMoreTokens())
{
for(int i = 0; i < num_resources; i++)
{
resourceVector[i] = Integer.parseInt(lines.nextToken());
}
}
br.close();
fr.close();
for(int i = 0; i < num_process; i++)
{
for(int j = 0; j < num_resources; j++)
{
needMatrix[i][j] = max_resource_matrix[i][j]-allocationMatrix[i][j];
}
}
for(int i = 0; i < num_process; i++)
{
for(int j = 0; j < num_resources; j++)

{
sumColumn[j] += allocationMatrix[i][j];
sumRow[i] += needMatrix[i][j];
}
}
for(int j = 0; j < num_resources; j++)
{
availableVector[j] = resourceVector[j] - sumColumn[j];
}
// Initialize Work and Finish
for(int j = 0; j < num_resources; j++)
{
current_available[j]=availableVector[j];
}
for(int i = 0; i < num_process; i++)
{
finish[i] = false;
}
boolean unsafe = false;
do
{
unsafe = false;
int i = 0;
for(; i < num_process; i++)
{
if ((!finish[i]))
{
boolean good = true;
for (int j = 0; j < num_resources; j++)
{
if(needMatrix[i][j] > current_available[j])
{
good = false;
break;
}
}
if (!good)
continue;
unsafe = true;
break;
}
}
if(unsafe)
{
finish[i] = true;
for (int j = 0; j < num_resources; j++)
{
current_available[j] += allocationMatrix[i][j];

}

process_order[index++] = i;

}

}while (unsafe);
//check whether all process are finished or not
for(int i = 0; i < num_process; i++)
{
if(!finish[i])
{
isSafe = false;
}
else
{
isSafe = true;
}
}
// Display output
System.out.println("Number of Processes : " + num_process);
System.out.println("Number of Resources : " + num_resources + "\n"); //Display the max_resource_matrix
System.out.println("Max_resource_Matrix : ");
for(int i = 0; i < num_process; i++)
{
for(int j = 0; j < num_resources; j++)
{
System.out.print(max_resource_matrix[i][j] + " ");
}
System.out.println();
}
//Display allocation matrix
System.out.println("\nAllocation Matrix : ");
for(int i = 0; i < num_process; i++)
{
for(int j = 0; j < num_resources; j++)
{
System.out.print(allocationMatrix[i][j] + " ");
}

System.out.println();
}
System.out.println("\nResource Vector : "); //Display resource matrix
for(int i = 0; i < num_resources; i++)
{
System.out.print(resourceVector[i] + " ");
}
System.out.println();
System.out.println("\nNeed Matrix : "); //Display the Need matrix
for(int i = 0; i < num_process; i++)
{
for(int j = 0; j < num_resources; j++)
{
System.out.print(needMatrix[i][j] + " ");
}
System.out.println();
}
System.out.println();
System.out.println("Initial Available Vector : "); //Display the Available Vector
for(int j = 0; j < num_resources; j++)
{
System.out.print(availableVector[j] +" ");
}
System.out.println("\n");
if(isSafe)
{
System.out.print("Process order of sequence : "); //Print the output
for(int i = 0; i < process_order.length; i++)
{
System.out.print((process_order[i]+1) + " ");
}
System.out.println();
System.out.println("This system is in a safe state!!!");
}
else
{
System.out.println("This system is not in a safe state!!!");
}
}
}

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

output::


Related Solutions

Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes...
Consider the bankers algorithm for deadlock avoidance. Give an example of this algorithm for 7 processes and 5 resource types.
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5,...
Implement a C++ program to implement the Banker’s algorithm for deadlock avoidance. Number of process 5, number of resources 3 and the number of instances of each given resource is in available. You should complete the functionalities for safe state check and resource request processing. To Do 1. Complete the definition of isSafe function. The function take, the process array, 1D array of available resources, 2D array storing current allocation, and 2D array of current need. The function does not...
Deadlocks. The Banker's algorithm is used for deadlock avoidance. Consider the state of resource availability and allocation defined by the following matrices.
Deadlocks. The Banker's algorithm is used for deadlock avoidance. Consider the state of resource availability and allocation defined by the following matrices.(1) Assuming that the total amounts for resources R1, R2, and R3 are 10, 2, and 10, should a new request to the Banker's algorithm by process P3 to acquire one additional resource from R1 and one additional resource from R3 be approved or denied? Explain why or why not(2) Assuming that the total amounts for resources R1, R2,...
Given the following state for the Banker's Algorithm
Given the following state for the Banker's Algorithm 5 processes PO through P4 3 resource types before assigning to any process. A has 5 instances: B has 6 Instances. C has 8 instances and D has 4.Allocation: the resources currently assigned to each process Max Claim: The maximum number of resource instances a process will need in order to complete. Determine the available vector
Deadlock Avoidance using Banker’s Algorithm Q2: Use the following information and complete the Table, also write...
Deadlock Avoidance using Banker’s Algorithm Q2: Use the following information and complete the Table, also write down the safe sequence if exist? Three Resources (R1=4, R2=9, R3=11) Processes Allocated Resources R1   R2    R3 Maximum Required Resources R1   R2    R3 Currently Available Resources R1   R2    R3 Remaining Need R1   R2    R3 Safe Sequence P1 1      4      2 2      4      6 P2 2      1      1 3      2      8 P3 0      0      1 1      2      3 P4 0      0      0 4      4      2...
Deadlock –Banker’s Algorithm A system has three resource types (A, B, C) and four processes {P1,...
Deadlock –Banker’s Algorithm A system has three resource types (A, B, C) and four processes {P1, P2, P3, P4 }. The total units of system resources are: (8, 5, 4) units of A, B and C, respectively. The maximum demands for each process is P1(1,2,3), P2(3,2,1), P3(6,5,4) and P4(4,4,2). The current allocation is: P1(0,1,1), P2(2,2,0) and P3(3,0,1) and P4(1,0,1). (a) Allocation table is given for the 3 processes with the following four columns: PROCESS, ALLOCATION, MAX and NEED. And fill...
1. How numerically ordered resources method prevent deadlock using example. 1. How banker’s algorithm works on...
1. How numerically ordered resources method prevent deadlock using example. 1. How banker’s algorithm works on deadlock prevention using example
A set of processes along with their burst time in milliseconds is given below. The processes...
A set of processes along with their burst time in milliseconds is given below. The processes should execute in First Come First Served order. Assume that the quantum (q) is 3: Process Burst Time P1 22 P2 4 P3 12 P4 15 P5 2 Find the average waiting time using the Round Robin algorithm. Round your answer to 2 decimal places
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes...
using Java, Implement the following algorithm: - Accept 5 different memory partitions and 4 different processes from user and show how best fit algorithm allocates them.
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an...
Write a program in C++ to implement Binary Search Algorithm. Assume the data given in an array. Use number of data N at least more than 10. The function Binary Search should return the index of the search key V.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT