Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Banker Deadlock detection algorith going haywire and likely a stack overflow error

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Banker Deadlock detection algorith going haywire and likely a stack overflow error

    It seems I might be getting a stack overflow error. My try/catch blocks aren't outputting anything.

    It's going wrong at the getInteger() return call in getInteger(). There are two base cases that should stop an infinite loop.

    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<Vector<Boolean>> finished;
    private static Scanner existence;
    private static Scanner allocation;
    private static Scanner request;
    private static int processes;
    private static int resource_types;
    public static void main(String[] args)
    {
     
    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<Vector<Boolean>>();
     
    for (int i=0; i < allocated.size(); i++)
    {
    finished.add(new Vector<Boolean>());
     
    for (int j =0; j < allocated.get(i).size(); j++)
    {
    if (allocated.get(i).get(j) != 0)
    {
    finished.get(i).add(false);
    }
    else
    {
    finished.get(i).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)
    System.out.println("Done without a deadlock.");
    else if (getInteger() == -1)
    {
    System.out.println("Deadlock occurred!");
     
    for (int i =0; i < finished.size(); i++)
    {
    for (int j =0; j < finished.get(i).size(); j++)
    {
    if (finished.get(i).get(j) == false)
    System.out.println("Process P" + j + " 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;
     
    for (int i =0; i < finished.size(); i++)
    {
     
    for (int j =0; j < finished.get(i).size(); j++)
    {
    if (finished.get(i).get(j) == false)
    done = false;
     
    }
     
    }
     
     
    if (done == true)
    return -2;
     
    for (int i =0; i < requested.size(); i++)
    {
    for (int j =0; j < requested.get(i).size(); j++)
    {
    if (finished.get(i).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(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(j).get(index));
    }
    catch(NullPointerException npe)
    {
    System.out.println("It's null at " + i + "," + j + "," + index + " Captain!");
    }
     
    finished.get(j).remove(index);
    try
    {
    finished.get(j).add(index, true); 
    }
    catch(NullPointerException npe)
    {
    System.out.println("It's null at " + i + "," + j + "," + index + " Captain!");
    }
     
    }
     
     
    }

    Cant' say what's going wrong. It's making so much of an error dialog that I can't see the spot where it started. The console apparently only holds a limited amount of space for UNIX Exceed on Demand 8.

    It says at 316
    return getInteger();

    However, it was returning saying the error was at 300 originally before some try/catches were put in. Anyway, it's not catching the error so I'm thinking it might be an Error rather than an Exception that's causing it?

    The algorithm is, and the vagueness since some parts are 2D and others are 1D, yet the algorithm in the book seems to not make that distinction, despite saying that some are 2D and others 1D.

    Anyway:

    1.) Let Work vector be initialized to available vector. For i = 0, 1,2, ..., n-1 if Allocation i != 0 then Finished i = false. Else Finished i = true
    2.) Find an index i such that both
    a.) Finish[i] == false
    b.) Request i <= Work
    if no such i exists, then go to 4.
    3.) Work = Work + Allocation i;
    Finish[i] = true;
    Go to 2;
    4.) If Finish i is false for some i. 0 <= i <= n, then the system is in a deadlocked state. Also, if Finished[i] == false, when this deadlocked state has occurred, then Process Pi is deadlocked.

    As for the error, this is all I could get from it.

     

    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)
    at banker_detect.getInteger(banker_detect.java:316)


    Attached Files Attached Files
    Last edited by javapenguin; April 22nd, 2012 at 08:30 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Banker Deadlock detection algorith going haywire and likely a stack overflow erro

    It always seems to never have a deadlock. Also, when I apply the same text file for both the request and allocation matrices, it throws an exception, an array index out of bounds exception, which it shouldn't. I'm thinking it's how I'm using my i's and j's.

    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)
    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(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(j).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!");
    }
     
    }
     
    System.out.println("Work: " + work.toString());
    System.out.println("Finished: " + finished.toString());
    System.out.println("Allocated: " + allocated.toString());
    }
     
    }

    rmatrix.txt is:
    0 1 0 0 1

    0 0 1 0 1

    0 0 0 0 1

    1 0 1 0 1


    cmatrix.txt is:

    1 0 1 1 0

    1 1 0 0 0

    0 0 0 1 0

    0 0 0 0 0


    It got run together in Notepad I guess.

    rmatrix2.txt:

    0 1 0 0 1
    0 0 1 0 1
    0 0 0 0 1
    1 0 1 0 100

    cmatrix2.txt:
    1 0 1 1 100
    1 1 0 0 0
    0 0 0 1 0
    0 0 0 0 0

    While cmatrix.txt and rmatrix.txt, when called as

    banker_detect.java 4 5 evector.txt cmatrix.txt rmatrix.txt may indeed be deadlock free. However,

    banker_detect.java 4 5 evector.txt cmatrix2.txt rmatrix2.txt should most definitely have a deadlock, but it doesn't!

    Also

    banker_detect.java 4 5 evector.txt cmatrix.txt cmatrix.txt should be valid, albeit goofy, but it's throwing an ArrayIndexOutOfBoundsException at index 4 I think. (Not sure which index it was for that combination. I think it's 4.)
    Last edited by javapenguin; April 23rd, 2012 at 12:25 PM.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Banker Deadlock detection algorith going haywire and likely a stack overflow erro

    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.
    Last edited by javapenguin; April 23rd, 2012 at 07:23 PM.

Similar Threads

  1. [SOLVED] stack overflow error
    By Goldfinch in forum What's Wrong With My Code?
    Replies: 14
    Last Post: February 3rd, 2012, 12:07 AM
  2. stack project-runtime error
    By makonikor in forum What's Wrong With My Code?
    Replies: 16
    Last Post: July 31st, 2011, 02:16 PM
  3. I think I need an algorith
    By javapenguin in forum What's Wrong With My Code?
    Replies: 25
    Last Post: September 3rd, 2010, 02:10 AM
  4. Error of "cannot access InToPost" in 3 and 5 code
    By jaysoncutie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 25th, 2009, 09:12 AM
  5. Replies: 0
    Last Post: October 14th, 2008, 06:40 PM