From db95b35afe3c8c4a9260564bc6fba77e113e8202 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Tue, 27 Nov 2012 17:36:40 -0500 Subject: [PATCH] Fixes for David Weber's code --- BestFitAlgorithm.java | 40 +++++++++++++++++++++++++--------------- WorstFitAlgorithm.java | 18 +++++++++++++----- 2 files changed, 38 insertions(+), 20 deletions(-) diff --git a/BestFitAlgorithm.java b/BestFitAlgorithm.java index 9138be3..7196e8f 100644 --- a/BestFitAlgorithm.java +++ b/BestFitAlgorithm.java @@ -64,10 +64,12 @@ public class BestFitAlgorithm implements baseAlgorithm{ //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()) + { + bSize = blocks.get(0).intValue(); + } //GET BEST INDEX for(int i = 0; i < blocks.size(); i++) @@ -100,22 +102,27 @@ public class BestFitAlgorithm implements baseAlgorithm{ bestSizeIndex = this.getBestIndex(jobSize); + if(bestSizeIndex == -1) + { + while(bestSizeIndex == -1) + { + //Compact and try again + //System.out.println("Compacting memory..."); + this.compact(); + bestSizeIndex = this.getBestIndex(jobSize); + } + } + if(jobSize > memoryBlock.length) { //System.out.println("Job is too large for current memory size"); } - if(bestSizeIndex == -1) + + if(bestSizeIndex != -1) { - //Compact and try again - //System.out.println("Compacting memory..."); - this.compact(); - bestSizeIndex = this.getBestIndex(jobSize); - } - else - { - //System.out.println("The size of the job is: " + jobSize); - //System.out.println("The best size index is: " + bestSizeIndex); + System.out.println("The size of the job is: " + jobSize); + System.out.println("The best size index is: " + bestSizeIndex); synchronized(memoryBlock) { @@ -128,7 +135,9 @@ public class BestFitAlgorithm implements baseAlgorithm{ //System.out.println("Successfully allocated! Starting job..."); - Job newJob = new Job(jobSize, jobID, jobSize, bestSizeIndex, deallocateMethod, this); + System.out.println("Job time: " + jobTime); + + Job newJob = new Job(jobTime, jobID, jobSize, bestSizeIndex, deallocateMethod, this); jobArray[jobID] = newJob; @@ -139,7 +148,8 @@ public class BestFitAlgorithm implements baseAlgorithm{ } catch (Exception e) { - //System.out.println("Could not allocate job with ID " + jobID); + e.printStackTrace(); + System.exit(-1); } } /* diff --git a/WorstFitAlgorithm.java b/WorstFitAlgorithm.java index 5e1fa40..8c21d38 100644 --- a/WorstFitAlgorithm.java +++ b/WorstFitAlgorithm.java @@ -67,7 +67,12 @@ public class WorstFitAlgorithm implements baseAlgorithm{ } int worstIndex = -1; - int wSize = blocks.get(0).intValue(); + int wSize = -1; + + if(!blocks.isEmpty()) + { + wSize = blocks.get(0).intValue(); + } //GET WORST INDEX @@ -110,10 +115,13 @@ public class WorstFitAlgorithm implements baseAlgorithm{ if(worstSizeIndex == -1) { - //Compact and try again - //System.out.println("Compacting memory..."); - this.compact(); - worstSizeIndex = this.getWorstIndex(jobSize); + while(worstSizeIndex == -1) + { + //Compact and try again + //System.out.println("Compacting memory..."); + this.compact(); + worstSizeIndex = this.getWorstIndex(jobSize); + } } else {