Skip to content

Instantly share code, notes, and snippets.

@thelittlemango
Created December 8, 2011 01:24
Show Gist options
  • Save thelittlemango/1445663 to your computer and use it in GitHub Desktop.
Save thelittlemango/1445663 to your computer and use it in GitHub Desktop.
Deadlock Simulation
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];
//------------------------------------
// Reads in desired number of
// instances.
//
//Note: (x+1) because arrays begin at 0
//--------------------------------------
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
//------------------------------
// Processes allocating resources
//-------------------------------
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
//------------------------------
if (Resources[resourceInst] == 0){
System.out.println("No more instances of this resource available.");
}//end if
else{
//--------------------------
//
//
//--------------------------
Processes[process+1] = "Resource" + resourceInst; //stores instance to appropriate process
for(int x = 0; x < processNum; x++){
System.out.println("Process Processes[x]);
}//end for
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment