diff --git a/BestFitAlgorithm.java b/BestFitAlgorithm.java index 9138be3..d40d8b4 100644 --- a/BestFitAlgorithm.java +++ b/BestFitAlgorithm.java @@ -56,18 +56,21 @@ public class BestFitAlgorithm implements baseAlgorithm{ } } } - //System.out.println("Size of indices array: " + indices.size()); - //System.out.println("Size of sizes array: " + blocks.size()); + System.out.println("Size of indices array: " + indices.size()); + System.out.println("Size of sizes array: " + blocks.size()); for(int i = 0; i < blocks.size(); i++) { - //System.out.println("Index: " + indices.get(i)); - //System.out.println("Size: " + blocks.get(i)); + System.out.println("Index: " + indices.get(i)); + System.out.println("Size: " + blocks.get(i)); } - + int bSize = -1; int bestIndex = -1; - int bSize = blocks.get(0).intValue(); - + if(!blocks.isEmpty()) + { + bestIndex = indices.get(0).intValue(); + bSize = blocks.get(0).intValue(); + } //GET BEST INDEX for(int i = 0; i < blocks.size(); i++) @@ -78,16 +81,17 @@ public class BestFitAlgorithm implements baseAlgorithm{ //Best possible fit. You're done. //System.out.println("Best Case"); bestIndex = indices.get(i).intValue(); + return bestIndex; } else if((blocks.get(i).intValue() <= bSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1) { bestIndex = indices.get(i).intValue(); + return bestIndex; } } //System.out.println("bestIndex: " + bestIndex); //System.out.println("bSize: " + bSize); - return bestIndex; } @@ -108,7 +112,7 @@ public class BestFitAlgorithm implements baseAlgorithm{ if(bestSizeIndex == -1) { //Compact and try again - //System.out.println("Compacting memory..."); + System.out.println("Compacting memory..."); this.compact(); bestSizeIndex = this.getBestIndex(jobSize); } @@ -121,12 +125,12 @@ public class BestFitAlgorithm implements baseAlgorithm{ { for(int i = bestSizeIndex; i < jobSize + bestSizeIndex; i++) { - //System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block!"); + System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block that has a pre-existing value of: " + this.memoryBlock[i]); this.memoryBlock[i] = jobID; } } - //System.out.println("Successfully allocated! Starting job..."); + System.out.println("Successfully allocated! Starting job..."); Job newJob = new Job(jobSize, jobID, jobSize, bestSizeIndex, deallocateMethod, this); @@ -134,12 +138,14 @@ public class BestFitAlgorithm implements baseAlgorithm{ newJob.start(); - //System.out.println("Job started!"); + System.out.println("Job started!"); } } catch (Exception e) { - //System.out.println("Could not allocate job with ID " + jobID); + System.out.println("Oops"); + e.printStackTrace(); + System.exit(-1); } } /* @@ -191,9 +197,10 @@ public class BestFitAlgorithm implements baseAlgorithm{ { synchronized(memoryBlock) { - for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + for (int x = 0; x < jobSize; x++) { - memoryBlock[i] = 0; + System.out.println("Deallocating job at: " + "Location: " + beginningLocation + " Position: " + x); + memoryBlock[beginningLocation + x] = 0; } } } diff --git a/WorstFitAlgorithm.java b/WorstFitAlgorithm.java index 5e1fa40..14ee83c 100644 --- a/WorstFitAlgorithm.java +++ b/WorstFitAlgorithm.java @@ -138,9 +138,10 @@ public class WorstFitAlgorithm implements baseAlgorithm{ //System.out.println("Job started!"); } } - catch (Exception e) + catch (Exception e) { - //System.out.println("Could not allocate job with ID " + jobID); + e.printStackTrace(); + System.exit(-1); } } /* @@ -188,11 +189,11 @@ public class WorstFitAlgorithm implements baseAlgorithm{ @Override public void deallocate(int jobSize, int beginningLocation) { - synchronized(memoryBlock) + synchronized(memoryBlock) { - for(int i = beginningLocation; i < jobSize + beginningLocation; i++) + for (int x = 0; x < jobSize; x++) { - memoryBlock[i] = 0; + memoryBlock[beginningLocation + x] = 0; } } } diff --git a/memoryManagement.java b/memoryManagement.java index ff48d86..e76d410 100644 --- a/memoryManagement.java +++ b/memoryManagement.java @@ -43,8 +43,10 @@ public class memoryManagement{ System.out.println("Type filename to load jobs from a file or just press enter for random jobs"); read = keyboard.nextLine(); file = new File(read + ".txt"); - if(!read.equals("") && !file.exists()) - System.out.println("File not found, try again"); + if(!read.equals("") && !file.exists()) + { + System.out.println("File not found, try again"); + } }while(!read.equals("") && !file.exists()); //Create random jobs or read from the file and create jobs @@ -75,19 +77,23 @@ public class memoryManagement{ //Send jobs to algorithm, time is calculated and printed out after completion //Note that we use `jobLength - 1` to compensate for the id above //Threaded Fit + System.out.print("Sending jobs to threaded allocation algorithm..."); timeStart[0] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) - threadedFit.allocate(id[i], size[i], time[i]); + for(int i = 0; i < jobLength - 1; i++) { + threadedFit.allocate(id[i], size[i], time[i]); + } timeEnd[0] = System.currentTimeMillis() - timeStart[0]; System.out.println("complete"); System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[0] + " milliseconds"); //Best Fit - System.out.print("Sending jobs to best fit allocation algorithm..."); + System.out.print("Sending jobs to best fit allocation algorithm...\n"); timeStart[1] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) - bestFit.allocate(id[i], size[i], time[i]); + for(int i = 0; i < jobLength - 1; i++) { + System.out.println("**********NEXT ITERATION***********"); + bestFit.allocate(id[i], size[i], time[i]); + } timeEnd[1] = System.currentTimeMillis() - timeStart[1]; System.out.println("complete"); System.out.println("Elapsed time for best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[1] + " milliseconds"); @@ -95,8 +101,9 @@ public class memoryManagement{ //Worst Fit System.out.print("Sending jobs to worst fit allocation algorithm..."); timeStart[2] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) - worstFit.allocate(id[i], size[i], time[i]); + for(int i = 0; i < jobLength - 1; i++) { + worstFit.allocate(id[i], size[i], time[i]); + } timeEnd[2] = System.currentTimeMillis() - timeStart[2]; System.out.println("complete"); System.out.println("Elapsed time for worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[2] + " milliseconds"); @@ -104,8 +111,9 @@ public class memoryManagement{ //First Fit System.out.print("Sending jobs to first fit allocation algorithm..."); timeStart[3] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) - firstFit.allocate(id[i], size[i], time[i]); + for(int i = 0; i < jobLength - 1; i++) { + firstFit.allocate(id[i], size[i], time[i]); + } timeEnd[3] = System.currentTimeMillis() - timeStart[3]; System.out.println("complete"); System.out.println("Elapsed time for first fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[3] + " milliseconds"); @@ -113,8 +121,9 @@ public class memoryManagement{ //Next Fit System.out.print("Sending jobs to next fit allocation algorithm..."); timeStart[4] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) - nextFit.allocate(id[i], size[i], time[i]); + for(int i = 0; i < jobLength - 1; i++) { + nextFit.allocate(id[i], size[i], time[i]); + } timeEnd[4] = System.currentTimeMillis() - timeStart[4]; System.out.println("complete"); System.out.println("Elapsed time for next fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[4] + " milliseconds"); @@ -122,10 +131,12 @@ public class memoryManagement{ System.out.print("Printing to log..."); out.println("Memory Management Log"); out.println("---------------------------"); - if(read.equals("")) - out.println("Job Assignment: Random"); - else - out.println("Job Assignment: " + read + ".txt"); + if(read.equals("")) { + out.println("Job Assignment: Random"); + } + else { + out.println("Job Assignment: " + read + ".txt"); + } out.println("Job Amount: " + jobLength); out.println("Memory Size: " + MEMORYSIZE); out.println("---------------------------");