In: Computer Science
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
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::