diff --git a/BestFitAlgorithm.java b/BestFitAlgorithm.java index d40d8b4..9138be3 100644 --- a/BestFitAlgorithm.java +++ b/BestFitAlgorithm.java @@ -56,21 +56,18 @@ 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; - if(!blocks.isEmpty()) - { - bestIndex = indices.get(0).intValue(); - bSize = blocks.get(0).intValue(); - } + int bSize = blocks.get(0).intValue(); + //GET BEST INDEX for(int i = 0; i < blocks.size(); i++) @@ -81,17 +78,16 @@ 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; } @@ -112,7 +108,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); } @@ -125,12 +121,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 that has a pre-existing value of: " + this.memoryBlock[i]); + //System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block!"); 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); @@ -138,14 +134,12 @@ public class BestFitAlgorithm implements baseAlgorithm{ newJob.start(); - System.out.println("Job started!"); + //System.out.println("Job started!"); } } catch (Exception e) { - System.out.println("Oops"); - e.printStackTrace(); - System.exit(-1); + //System.out.println("Could not allocate job with ID " + jobID); } } /* @@ -197,10 +191,9 @@ public class BestFitAlgorithm implements baseAlgorithm{ { synchronized(memoryBlock) { - for (int x = 0; x < jobSize; x++) + for(int i = beginningLocation; i < jobSize + beginningLocation; i++) { - System.out.println("Deallocating job at: " + "Location: " + beginningLocation + " Position: " + x); - memoryBlock[beginningLocation + x] = 0; + memoryBlock[i] = 0; } } } diff --git a/WorstFitAlgorithm.java b/WorstFitAlgorithm.java index 14ee83c..5e1fa40 100644 --- a/WorstFitAlgorithm.java +++ b/WorstFitAlgorithm.java @@ -138,10 +138,9 @@ public class WorstFitAlgorithm implements baseAlgorithm{ //System.out.println("Job started!"); } } - catch (Exception e) + catch (Exception e) { - e.printStackTrace(); - System.exit(-1); + //System.out.println("Could not allocate job with ID " + jobID); } } /* @@ -189,11 +188,11 @@ public class WorstFitAlgorithm implements baseAlgorithm{ @Override public void deallocate(int jobSize, int beginningLocation) { - synchronized(memoryBlock) + synchronized(memoryBlock) { - for (int x = 0; x < jobSize; x++) + for(int i = beginningLocation; i < jobSize + beginningLocation; i++) { - memoryBlock[beginningLocation + x] = 0; + memoryBlock[i] = 0; } } } diff --git a/memoryManagement.java b/memoryManagement.java index e76d410..ff48d86 100644 --- a/memoryManagement.java +++ b/memoryManagement.java @@ -43,10 +43,8 @@ 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 @@ -77,23 +75,19 @@ 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...\n"); + System.out.print("Sending jobs to best fit allocation algorithm..."); timeStart[1] = System.currentTimeMillis(); - for(int i = 0; i < jobLength - 1; i++) { - System.out.println("**********NEXT ITERATION***********"); - bestFit.allocate(id[i], size[i], time[i]); - } + for(int i = 0; i < jobLength - 1; i++) + 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"); @@ -101,9 +95,8 @@ 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"); @@ -111,9 +104,8 @@ 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"); @@ -121,9 +113,8 @@ 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"); @@ -131,12 +122,10 @@ 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("---------------------------");