Created
December 10, 2011 03:08
-
-
Save thelittlemango/1454436 to your computer and use it in GitHub Desktop.
DeadlockSim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.InputStreamReader; | |
import java.io.IOException; | |
//----------------------------------------------------------------- | |
// Program by: Sam Fader and Annabella Lopez | |
// | |
// This program was created to simulate deadlock prevention using the | |
// Banker's Algorithm. Processes must declare the maximum number of | |
// instances of each resource type and may not exceed the total number | |
// or resources in the system. | |
// | |
// Before allocating resources the following must be known: | |
// (1) How much of each resource each process may request. | |
// (2) How much of each resource each process is currently holding. | |
// (3) How much of each resource the system has available. | |
// | |
// *Notes* | |
// -Due to the difficulty of working with 2d arrays we used | |
// InputStreamReader along with BufferReader for efficiency. | |
//------------------------------------------------------------------- | |
public class DeadlockSim { | |
public static void main(String[] args)throws IOException{ | |
InputStreamReader scan = new InputStreamReader(System.in); | |
BufferedReader obj = new BufferedReader(scan); | |
int numOfProcess, numOfResources; | |
System.out.println("Enter number of processes: "); | |
numOfProcess = Integer.parseInt(obj.readLine()); | |
System.out.println("Enter number or resources: "); | |
numOfResources = Integer.parseInt(obj.readLine()); | |
//---------------------------------------- | |
// Reads in number of resource instances | |
// for storage into an array | |
//----------------------------------------- | |
int availableResources[] = new int[numOfResources]; | |
for(int resourceNum = 0; resourceNum < numOfResources; resourceNum++){ | |
System.out.println("Enter number of instances for Resource " + resourceNum + ": "); | |
availableResources[resourceNum] = Integer.parseInt(obj.readLine()); | |
}//end for | |
System.out.println("Allocation Requirements: "); | |
//---------------------------------------- | |
// Asks user in advance for resources that | |
// their processes will require. | |
//---------------------------------------- | |
int allocation[][] = new int[numOfProcess][numOfResources]; | |
for(int x = 0; x < numOfProcess; x++) | |
for(int y = 0; y < numOfResources; y++){ | |
System.out.println("How many instances of Resource " + y + " does Process " + x + " want to allocate? "); | |
allocation[x][y] = Integer.parseInt(obj.readLine()); | |
}//end for | |
System.out.println("Max Requirements: "); | |
//---------------------------------------- | |
// Asks user in advance for max # of | |
// resource instances for each process. | |
//---------------------------------------- | |
int maxResources[][] = new int[numOfProcess][numOfResources]; | |
for(int x = 0; x < numOfProcess; x++) | |
for(int y = 0; y < numOfResources; y++){ | |
System.out.println("Max instances for Resource "+ y +"for Process "+ x + " ?"); | |
maxResources[x][y]=Integer.parseInt(obj.readLine()); | |
}//end for | |
//-------------------------------------------- | |
// Created a 2d array for future check method | |
// to see if the process meets all the | |
// requirements for resource allocation. | |
//-------------------------------------------- | |
int resourcesNeeded[][] = new int[numOfProcess][numOfResources]; | |
for(int x = 0; x < numOfProcess; x++) | |
for(int y = 0; y < numOfResources; y++){ | |
resourcesNeeded[x][y] = (maxResources[x][y] - allocation[x][y]); | |
}//end for | |
}//end main | |
}//end DeadlockSim |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment