Skip to content

Instantly share code, notes, and snippets.

@thelittlemango
Created December 11, 2011 23:42
Show Gist options
  • Save thelittlemango/1463544 to your computer and use it in GitHub Desktop.
Save thelittlemango/1463544 to your computer and use it in GitHub Desktop.
DeadlockSim [Final Product]
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
// of resources in the system. Used 2d arrays to create simulation
// using 1d arrays was more complicated (even though this simulation
// is actually really complicated).
//
// Before allocating resources the following must be known:
// (1) How much of each resource each process must allowed to request.
// (2) How much of each resource each process is currently holding.
// (3) How much of each resource the system has available.
//
// *Notes*
// -Regular scanner was giving us problems reading input so we used
// InputStreamReader and BufferReader as per oracle docs suggestion along
// with outside advice.
//
// References:
// http://en.wikipedia.org/wiki/Banker's_algorithm
// http://www.sci.csueastbay.edu/~billard/cs4560/node11.html
// http://docs.oracle.com/javase/1.4.2/docs/api/java/io/InputStreamReader.html
// http://docs.oracle.com/javase/1.4.2/docs/api/java/io/BufferedReader.html
//-------------------------------------------------------------------
public class DeadlockSim {
static int safe[]=new int[30];
//---------------------------------------------------------------
// Checks to see if resource
//
//---------------------------------------------------------------
static boolean check(int numOfProcess, int numOfResources,
int resourcesNeeded[][], int availableResources[],
int allocation[][]){
int p = numOfProcess;
int r = numOfResources;
int need[][]=new int[p][r];
int work[]=new int[r]; //Stores allocated resources
int all[][]=new int[p][r];
//--------------------------------
// New array with values from
// availableResources.
//---------------------------------
for(int x = 0; x < r; x++){
work[x] = availableResources[x];
}//end for
//--------------------------------
// New array with values from
// allocation.
//---------------------------------
for (int x = 0; x < p; x++){
for (int y = 0; y < numOfResources; y++){
all[x][y] = allocation[x][y];
}//end for
}//end for
//--------------------------------
// New array with values from
// resourcesNeeded.
//---------------------------------
for (int x = 0; x < p; x++){
for (int y = 0; y < r; y++){
need[x][y] = resourcesNeeded[x][y];
}//end for
}//end for
//--------------------------------
// Creates array for future check.
// Initialize all spots as false.
//--------------------------------
boolean finsafe[] = new boolean[p];
for (int x = 0; x < p; x++){
finsafe[x] = false;
}//end for
//--------------------------------
// While check and check2 is less
// than the number of processes
// execute the following code.
//--------------------------------
int check = 0;
int check2 = 0;
do{
for (int x = 0; x < p; x++){
boolean flag = true;
//---------------------------------
// If value at that spot in the
// fin array is false execute
// for loop (should always execute)
//----------------------------------
if(finsafe[x] == false){
for(int y = 0; y < r; y++){
//---------------------------------
// Flag boolean becomes false if
// available resources is less than
// resource that process needs.
//---------------------------------
if (work[y] < need[x][y]){
flag = false;
}//end if
}//end for
//------------------------------
// Adds allocated resources to
// work array.
//------------------------------
if(flag){
for(int y = 0; x < r; y++)
work[y]+=all[x][y];
safe[check] = x;
finsafe[x] = true;
}//end if
}//end if
}//end for
check2++;
}//end do
while (check < numOfProcess && check2 < numOfProcess);
//-----------------------------
// If check is greater than
// the number of processes
// then the check method returns
// false.
//-----------------------------
if(check > numOfProcess){
return false;
}//end if
else {
return true;
}//end else
}//end check
static boolean request(int numOfProcess, int numOfResources,
int process, int allocation[][],
int resourcesNeeded[][], int request[],
int availableResources[]){
int p = numOfProcess;
int r = numOfResources;
int pid = process;
int alloc[][] = new int[p][r];
int needR[][] = new int[p][r];
int req[] = new int[r];
int avail[] = new int[r];
//--------------------------------
// New array with values from
// request.
//---------------------------------
for(int x = 0; x < r; x++){
req[x] = request[x];
}//end for
//--------------------------------
// New array with values from
// available resources.
//---------------------------------
for(int x = 0; x < r; x++){
avail[x] = availableResources[x];
}//end for
//--------------------------------
// New array with values from
// allocation.
//---------------------------------
for(int x = 0;x < p; x++){
for(int y = 0; y < r; y++){
alloc[p][r] = allocation[p][r];
}//end for
}//end for
//--------------------------------
// New array with values from
// resourcesNeeded.
//---------------------------------
for(int x = 0;x < p; x++){
for(int y = 0; y < r; y++){
needR[p][r] = resourcesNeeded[p][r];
}//end for
}//end for
boolean flag = true;
if (flag){
//-----------------------------
// If available resources less
// than requested resources
// flag is false.
//----------------------------
for (int x = 0; x < r; x++){
if (avail[x] < req[x]){
flag = false;
}//end if
}//end for
if(flag){
for (int x = 0; x < r; x++){
alloc[pid][x] += req[x]; //Add requested resources to allocated resources.
needR[pid][x] -= req[x]; //Subtract requested resources from potential needed resources.
avail[x] -= req[x]; //Subtract requested resources from available resources.
}//end for
if (check(p, r, needR, avail, alloc)){
return true;
}//end if
else
System.out.println("Deadlock will occur. Try your request again.");
}//end if
else
System.out.println("Process " + pid + " needs to wait.");
}//end if
else
System.out.println("Process is going beyond it's limits. Try your request again.");
return false;
}//end request
public static void main(String[] args)throws IOException{
InputStreamReader scan = new InputStreamReader(System.in);
BufferedReader obj = new BufferedReader(scan);
int numOfProcess, numOfResources;
System.out.print("Enter number of processes: ");
numOfProcess = Integer.parseInt(obj.readLine());
System.out.print("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.print("Enter number of instances for Resource " + resourceNum + ": ");
availableResources[resourceNum] = Integer.parseInt(obj.readLine()); // Current # of unused resources.
}//end for
System.out.println("");
System.out.println("Allocation Requests: ");
//----------------------------------------
// Asks user in advance for resources that
// their processes will require.
//----------------------------------------
int allocation[][] = new int[numOfProcess][numOfResources]; // Processes current allocation of resources.
for(int x = 0; x < numOfProcess; x++){
for(int y = 0; y < numOfResources; y++){
System.out.print("How many instances of Resource " + y + " does Process " + x + " want to allocate? ");
allocation[x][y] = Integer.parseInt(obj.readLine());
}//end for
}//end for
System.out.println("");
System.out.println("Process restrictions (max): ");
//----------------------------------------
// Asks user in advance for max # of
// resource instances for each process.
//----------------------------------------
int maxResources[][] = new int[numOfProcess][numOfResources]; // Max demand of specified resource for specified process.
for(int x = 0; x < numOfProcess; x++){
for(int y = 0; y < numOfResources; y++){
System.out.print("Max instances for Resource "+ y +" for Process "+ x + " ? ");
maxResources[x][y] = Integer.parseInt(obj.readLine());
}//end for
}//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]; // Processes potential for more resources.
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 for
//------------------------------------------------
// Executes check method. May allocate resources
// if first condition holds.
//------------------------------------------------
if(check(numOfProcess, numOfResources,resourcesNeeded, availableResources, allocation)){
System.out.print("In safe state! ");
//------------------------------
// Prints out check array
// -----------------------------
for(int x = 0; x < numOfProcess; x++){
System.out.println(safe[x]+" ");
}//end for
} //end if
//------------------------------------------------
// If processes don't meet allocation requirements.
// (Executes request method).
//------------------------------------------------
else{
System.out.println("Check returned usafe state. ");
System.out.println("Begin requesting resources!");
System.out.print("What process wants to request? ");
int process = Integer.parseInt(obj.readLine());
//--------------------------------------------
// Reads in which process is trying to request
//---------------------------------------------
int request[]=new int[numOfResources];
System.out.print("Enter resources that Process " + process + " would like to request:");
for (int x = 0; x < numOfResources; x++){
request[x]=Integer.parseInt(obj.readLine());
//--------------------------------------
// If requirements are met execute this
//-------------------------------------
if(request(numOfProcess, numOfResources, process, allocation, resourcesNeeded, request, availableResources)){
System.out.println("Request meets requirements! May allocate. ");
for(int b = 0; b < numOfResources; b++){
allocation[process][b] += request[b]; //adds request to resources allocate to process
resourcesNeeded[process][b] -= request[b]; //subtracts request from resourcesNeeded
availableResources[b] -= request[b]; //subtracts request from availableResources
}//end for
if(check(numOfProcess, numOfResources, resourcesNeeded, availableResources, allocation)){
System.out.println("Check returned safe state! ");
for(int b = 0; b < numOfProcess; b++){
System.out.println(safe[b]+" ");
}//end for
}//end if
else
System.out.println("Check returned unsafe state. ");
}//end if
}//end for
}//end else
}//end main
}//end DeadlockSim
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment