ANYBODY THERE?
It should be a simple mistake. Though I can't figure out how to resolve it.
If you're going to demand an SSCCE, in spite of the fact that my code's not too long, then I'm going to say that you're pushing it too hard and that they should get rid of those stupid things.
import java.util.Vector;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class banker_detect
{
private static Vector<Boolean> deadlocked;
private static Vector<Vector<Integer>> requested;
private static Vector<Integer> available;
private static Vector<Vector<Integer>> allocated;
private static Vector<Integer> work;
private static Vector<Boolean> finished;
private static Scanner existence;
private static Scanner allocation;
private static Scanner request;
private static int processes;
private static int resource_types;
private static int timesThrough;
public static void setTimesThrough(int timesThrough2)
{
timesThrough = timesThrough2;
}
public static int getTimesThrough()
{
return timesThrough;
}
public static void main(String[] args)
{
setTimesThrough(0);
processes = 0;
resource_types = 0;
try
{
processes = Integer.parseInt(args[0]);
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Error!");
System.exit(1);
}
catch(NullPointerException npe)
{
System.out.println("Error!");
System.exit(1);
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid format");
System.exit(1);
}
try
{
resource_types = Integer.parseInt(args[1]);
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Error!");
System.exit(1);
}
catch(NullPointerException npe)
{
System.out.println("Error!");
System.exit(1);
}
catch(NumberFormatException nfe)
{
System.out.println("Invalid format");
System.exit(1);
}
System.out.println(processes);
System.out.println(resource_types);
existence = null;
try
{
existence = new Scanner(new File(args[2]));
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Error!");
System.exit(1);
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found!");
System.exit(1);
}
available = new Vector<Integer>();
while (existence.hasNext())
{
available.add(existence.nextInt());
}
allocation = null;
try
{
allocation = new Scanner(new File(args[3]));
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Error!");
System.exit(1);
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found!");
System.exit(1);
}
allocated = new Vector<Vector<Integer>>();
while (allocation.hasNext())
{
String line = allocation.nextLine();
Scanner temp = new Scanner(line);
allocated.add(new Vector<Integer>());
while (temp.hasNext())
{
try
{
allocated.get(allocated.size()-1).add(Integer.parseInt(temp.next()));
}
catch (NumberFormatException nfe)
{
System.out.println("Invalid format.");
System.exit(0);
}
}
}
request = null;
try
{
request = new Scanner(new File(args[4]));
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Error!");
System.exit(1);
}
catch(FileNotFoundException fnfe)
{
System.out.println("File not found!");
System.exit(1);
}
requested= new Vector<Vector<Integer>>();
while (request.hasNext())
{
String line = request.nextLine();
Scanner temp = new Scanner(line);
requested.add(new Vector<Integer>());
while (temp.hasNext())
{
try
{
requested.get(requested.size()-1).add(Integer.parseInt(temp.next()));
}
catch (NumberFormatException nfe)
{
System.out.println("Invalid format.");
System.exit(0);
}
}
}
work = new Vector<Integer>();
for (int i =0; i < available.size(); i++)
{
work.add(available.get(i));
}
finished = new Vector<Boolean>();
for (int i=0; i < allocated.size(); i++)
{
boolean fin = true;
for (int j =0; j < allocated.get(i).size(); j++)
{
if (allocated.get(i).get(j) != 0)
{
fin = false;
}
}
if (fin == false)
{
finished.add(false);
}
else
{
finished.add(true);
}
}
System.out.println(available.toString());
System.out.println(allocated.toString());
System.out.println(requested.toString());
System.out.println(work.toString());
System.out.println(finished.toString());
deadlocked = new Vector<Boolean>();
if (getInteger() == -2)
{
boolean died = false;
for (int i =0; i < finished.size(); i++)
{
if(finished.get(i) == false)
{
died = true;
System.out.println("Process P" + i + " is deadlocked!");
}
}
if (died == true)
System.out.println("Deadlock occurred!");
else
{
System.out.println(finished.toString());
System.out.println("Done without a deadlock.");
}
}
else if (getInteger() == -1)
{
System.out.println("Deadlock occurred!");
for (int i =0; i < finished.size(); i++)
{
if (finished.get(i) == false)
System.out.println("Process P" + i + " is deadlocked!");
}
}
}
public static int getInteger()
{
/** This part below will ensure that it doesn't signal a deadlock if everything is finished.
* It is the exception to the normal part of this method, which will return -1 if it can't find a work(i) >= requested(i)(j) AND a finished(i)(j) == false.
* If done is true at the end of this loop, it will return -2.
* If it finds an index that meets both the the conditions, then it returns getInteger().
**/
boolean done = true;
setTimesThrough(getTimesThrough() + 1);
for (int i =0; i < finished.size(); i++)
{
if (finished.get(i) == false)
done = false;
}
if (done == true)
{
System.out.println(getTimesThrough());
return -2;
}
for (int i =0; i < requested.size(); i++)
{
for (int j =0; j < requested.get(i).size(); j++)
{
if (finished.get(j) == false && requested.get(i).get(j) <= work.get(j))
{
try
{
update(j);
}
catch(NullPointerException npe)
{
System.out.println("Null pointer at " + j + "!!!");
}
catch(ArrayIndexOutOfBoundsException aioobe)
{
System.out.println("Index " +i + ","+ j + " is out of bounds!");
}
catch(Exception e)
{
System.out.println("Some other type of Exception at " + j + "!!!");
}
try
{
return getInteger();
}
catch(NullPointerException npe)
{
System.out.println("Returning a null pointer at " + j + "!!!");
}
catch(Exception e)
{
System.out.println("Some other type of Exception being thrown when return with " + j + "!!!");
}
}
}
}
return -1;
}
/**
* This should update the work vector and finished vectors. I hope I coded this right.
**/
public static void update(int index)
{
int j =0;
for (int i =0; i < allocated.get(0).size(); i++)
{
int temp = 0;
try
{
temp = work.get(i);
}
catch(NullPointerException npe)
{
System.out.println("It's null at " + i + "," + j + "," + index + " Captain!");
}
work.remove(i);
try
{
work.add(i , temp + allocated.get(i).get(index));
}
catch(NullPointerException npe)
{
System.out.println("It's null at " + i + "," + j + "," + index + " Captain!");
}
finished.remove(index);
try
{
finished.add(index, true);
}
catch(NullPointerException npe)
{
System.out.println("It's null at " + i + "," + j + "," + index + " Captain!");
}
j++;
}
System.out.println("Work: " + work.toString());
System.out.println("Finished: " + finished.toString());
System.out.println("Allocated: " + allocated.toString());
}
}
java banker_detect 4 5 evector.txt cmatrix.txt rmatrix.txt
4
5
[0, 0, 0, 0, 1]
[[1, 0, 1, 1, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]]
[[0, 1, 0, 0, 1], [0, 0, 1, 0, 1], [0, 0, 0, 0, 1], [1, 0, 1, 0, 1]]
[0, 0, 0, 0, 1]
[false, false, false, true]
Index 0,0 is out of bounds!
Index 0,1 is out of bounds!
Index 0,2 is out of bounds!
4
[true, true, true, true]
Done without a deadlock.
pradcoc@tranquility:~/IT383/1> java banker_detect 4 5 evector.txt cmatrix.txt cmatrix2.txt
4
5
[0, 0, 0, 0, 1]
[[1, 0, 1, 1, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]]
[[1, 0, 1, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]]
[0, 0, 0, 0, 1]
[false, false, false, true]
Index 0,1 is out of bounds!
Some other type of Exception being thrown when return with 1!!!
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 4
at java.util.Vector.get(Vector.java:694)
at banker_detect.getInteger(banker_detect.java:338)
at banker_detect.main(banker_detect.java:264)
Sorry about the rudeness, I was in a bad mood. It seems they are asking for SSCCEs a lot.
I'm going to make the work vector and finish vector into an array. In fact, I might make all of them arrays at some point. Then I don't have to worry about saving with that temp variable.