java - Implementing a deadlock detection algorithm; issue with the loops -
i have been looking past 3 hours, , can't find problem code. user inputs number of processes , number of types of resources , algorithm determines if there deadlock or not. problem in steptwo() method have checked in first block of code, , fine.
here pseudocode: https://www.dropbox.com/s/dt9cvk5h3ij7wqz/deadlockdetection.jpg?dl=0
here main, variables, etc.:
public class assignmentiii { public static int numprocesses; // represents number of processes public static int numresources; // represents number of different types of resources public static int[] available; // create matrix available processes public static int[][] allocation; // create allocation matrix public static int[][] request; // create request matrix public static int[] work; // create work matrix public static boolean[] finish; // create finish matrix public static void main(string[] args) throws filenotfoundexception { try { scanner scan = new scanner(system.in); scanner filescan = new scanner(new file("input3.txt")); // create file scanner system.out.println("please enter total number of processes: "); numprocesses = scan.nextint(); system.out.println("please enter number of different types of resources: "); numresources = scan.nextint(); // initalize matrices sizes available = new int[numresources]; allocation = new int[numprocesses][numresources]; request = new int[numprocesses][numresources]; work = new int[numresources]; finish = new boolean[numprocesses]; // initialize available matrix contents for(int = 0; < numresources; i++) available[i]=filescan.nextint(); // initialize allocation matrix contents for(int j = 0; j < numprocesses; j++) for(int k = 0; k < numresources; k++) allocation[j][k]=filescan.nextint(); // initialize request matrix contents for(int m = 0; m < numprocesses; m++) for(int n = 0; n < numresources; n++) request[m][n]=filescan.nextint(); // begin deadlock detection algorithm for(int = 0; < numresources; i++) // intialize work := available work[i]=available[i]; for(int j = 0; j < numprocesses; j++) // check allocation != 0 , initialize finish accordingly { // allocation matrix = 0 if sum of elements = 0 // same if every element = 0 int sumallocation = 0; for(int = 0; < numresources; i++) { sumallocation += allocation[j][i]; } if (sumallocation != 0) finish[j] = false; else finish[j] = true; } steptwo(); } catch(filenotfoundexception ex) { system.out.println("an error has occured. file cannot found."); } }
the error occurring somewhere in here, can't figure out:
public static void steptwo() { // step 2: find index j finish[j] = false & request[j] <= work for(int j = 0; j < numprocesses; j++) { if (finish[j] == true && j == numprocesses) { system.out.println("no deadlocks."); } for(int = 0; < numresources; i++) { if (request[j][i] <= work[i] && finish[j] == false) { if (i == (numresources-1)) { finish[j] = true; for(int m = 0; m < numresources; m++) { work[m] += allocation[j][m]; } j = -1; // reset counter break; } } else if (request[j][i] > work[i] && finish[j] == false) { system.out.println("p" + j + " deadlocked."); } } } } }
my input is:
0 0 0
0 1 0
2 0 0
3 0 3
2 1 1
0 0 2
0 0 0
2 0 2
0 0 0
1 0 0
0 0 2
the output is:
p1 deadlocked. p1 deadlocked.
the correct output should "no deadlocks."
any appreciated.
Comments
Post a Comment