Skip to content

Instantly share code, notes, and snippets.

@thelittlemango
Created December 8, 2011 16:59
Show Gist options
  • Save thelittlemango/1447597 to your computer and use it in GitHub Desktop.
Save thelittlemango/1447597 to your computer and use it in GitHub Desktop.
Deadlock Hopelessness
import java.util.*;
//------------------------------------------------------------------
// Program by: Sam Fader and Bell Lopez
// The purpose of this program is to determine if processes are
// deadlocked or not.
// We pledge that this work is ours.
//-------------------------------------------------------------------
public class Deadlock {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//-----------------------------------
// Creates an array of Processes.
//-----------------------------------
System.out.print("Number of processes: ");
int processNum = scan.nextInt();
String[] Processes = new String[processNum];
//-----------------------------------
// Creates an array of Resources.
//-----------------------------------
System.out.print("Number of resources: ");
int resourceNum = scan.nextInt();
int[] Resources = new int[resourceNum];
//-------------------------
// Creates Process objects
//-------------------------
Process[] procInstances = new Process[resourceNum];
for (int x = 0; x < procInstances.length; x++) {
procInstances[x] = new Process(resourceNum);
}//end for
//------------------------------------
// Stores resource instances in
// resource array.
//--------------------------------------
for(int x = 0; x < resourceNum; x++){
System.out.print("Number of instances for Resource " + (x+1) + ": ");
int instance = scan.nextInt();
Resources[x] = instance;
}//end for
//-------------------------------------
// Prints out all user data so far.
//-------------------------------------
System.out.println("");
System.out.println("Summary: ");
System.out.println("Number of Processes: " + processNum);
System.out.println("Number of Resources: " + resourceNum);
for(int x = 0; x < resourceNum; x++){
System.out.println("Resource " + (x+1) + " has " + Resources[x] + " instances." );
}//end for
while(true){
System.out.println("Which process wants to allocate resources? (-1 to exit) ");
int process = scan.nextInt() - 1;
if (process == -1){
break;
}//end if
else{
System.out.println("Which resource does Process " + (process+1) + " want to allocate? ");
int resourceInst = scan.nextInt() - 1;
//------------------------------
// Code block below handles:
// No instances.
// Should: set status to blocked
// for specified resource.
//------------------------------
if (Resources[resourceInst] == 0){
System.out.println("No more instances of this resource available.");
procInstances[resourceInst].getStatus();
}//end if
else{
//------------------------------------
// Should add resource instance to
// process object and decrement
// resources from resource array.
// -----------------------------------
procInstances[resourceInst].getResource(process, resourceInst); //allocates resource instance
Resources[process]--; //decrements resource instance
}//end else
}//end else
}//end while
System.out.println("Summary after allocation: ");
for(int x = 0; x < resourceNum; x++){
System.out.println("Resource " + (x+1) + " has " + Resources[x] + " instances." );
}//end for
}//end main
}//end Deadlock
import java.util.*;
public class Process {
private ArrayList myArrayList2;
private int[] processArray;
private int size;
private String status ="running";
public Process (int size){
processArray = new int[size];
}//end constructor
public String getStatus(){
String status = "blocked";
}//end getStatus
public int getResource(int process, int resource){
processArray[process] = resource;
}//end getResource
}//end process
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment