Actuall fix David's code

This commit is contained in:
Bradlee Speice 2012-11-27 18:04:07 -05:00
parent db95b35afe
commit 365e071558
3 changed files with 24 additions and 13 deletions

View File

@ -66,6 +66,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
} }
int bSize = 1; int bSize = 1;
int bestIndex = -1; int bestIndex = -1;
if(!blocks.isEmpty()) if(!blocks.isEmpty())
{ {
bSize = blocks.get(0).intValue(); bSize = blocks.get(0).intValue();
@ -121,8 +122,8 @@ public class BestFitAlgorithm implements baseAlgorithm{
if(bestSizeIndex != -1) if(bestSizeIndex != -1)
{ {
System.out.println("The size of the job is: " + jobSize); //System.out.println("The size of the job is: " + jobSize);
System.out.println("The best size index is: " + bestSizeIndex); //System.out.println("The best size index is: " + bestSizeIndex);
synchronized(memoryBlock) synchronized(memoryBlock)
{ {
@ -135,7 +136,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
//System.out.println("Successfully allocated! Starting job..."); //System.out.println("Successfully allocated! Starting job...");
System.out.println("Job time: " + jobTime); //System.out.println("Job time: " + jobTime);
Job newJob = new Job(jobTime, jobID, jobSize, bestSizeIndex, deallocateMethod, this); Job newJob = new Job(jobTime, jobID, jobSize, bestSizeIndex, deallocateMethod, this);

View File

@ -74,16 +74,16 @@ public class WorstFitAlgorithm implements baseAlgorithm{
wSize = blocks.get(0).intValue(); wSize = blocks.get(0).intValue();
} }
//GET WORST INDEX //GET WORST INDEX
for(int i = 0; i < blocks.size(); i++) for(int i = 0; i < blocks.size(); i++)
{ {
//Get largest possible free block to allocate to //Get largest possible free block to allocate to
if((blocks.get(i).intValue() >= wSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1) if(blocks.get(i).intValue() >= wSize || blocks.get(i).intValue() > -1)
{ {
worstIndex = indices.get(i).intValue(); worstIndex = indices.get(i).intValue();
} }
//"Worst fit"....same size as job size //"Worst fit"....same size as job size
else if(blocks.get(i).intValue() == jobSize) else if(blocks.get(i).intValue() == jobSize)
{ {
@ -91,6 +91,13 @@ public class WorstFitAlgorithm implements baseAlgorithm{
//System.out.println("Worst Case"); //System.out.println("Worst Case");
worstIndex = indices.get(i).intValue(); worstIndex = indices.get(i).intValue();
} }
else
{
if(!blocks.isEmpty())
{
worstIndex = indices.get(i).intValue();
}
}
} }
//System.out.println("bestIndex: " + bestIndex); //System.out.println("bestIndex: " + bestIndex);
@ -118,15 +125,16 @@ public class WorstFitAlgorithm implements baseAlgorithm{
while(worstSizeIndex == -1) while(worstSizeIndex == -1)
{ {
//Compact and try again //Compact and try again
//System.out.println("Compacting memory..."); System.out.println("Compacting memory...");
this.compact(); this.compact();
worstSizeIndex = this.getWorstIndex(jobSize); worstSizeIndex = this.getWorstIndex(jobSize);
} }
} }
else if(worstSizeIndex != -1)
{ {
System.out.println("Phew");
//System.out.println("The size of the job is: " + jobSize); //System.out.println("The size of the job is: " + jobSize);
//System.out.println("The best size index is: " + bestSizeIndex); //System.out.println("The worst size index is: " + worstSizeIndex);
synchronized(memoryBlock) synchronized(memoryBlock)
{ {
for(int i = worstSizeIndex; i < jobSize + worstSizeIndex; i++) for(int i = worstSizeIndex; i < jobSize + worstSizeIndex; i++)
@ -137,10 +145,10 @@ public class WorstFitAlgorithm implements baseAlgorithm{
} }
//System.out.println("Successfully allocated! Starting job..."); //System.out.println("Successfully allocated! Starting job...");
Job newJob = new Job(jobSize, jobID, jobSize, worstSizeIndex, deallocateMethod, this); Job newJob = new Job(jobTime, jobID, jobSize, worstSizeIndex, deallocateMethod, this);
System.out.println("Job started!");
jobArray[jobID] = newJob; jobArray[jobID] = newJob;
System.out.println("The size of the job array is: " + jobArray.length);
newJob.start(); newJob.start();
//System.out.println("Job started!"); //System.out.println("Job started!");
@ -148,7 +156,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{
} }
catch (Exception e) catch (Exception e)
{ {
//System.out.println("Could not allocate job with ID " + jobID); e.printStackTrace();
System.exit(-1);
} }
} }
/* /*

View File

@ -42,6 +42,7 @@ public class memoryManagement{
do{ do{
System.out.println("Type filename to load jobs from a file or just press enter for random jobs"); System.out.println("Type filename to load jobs from a file or just press enter for random jobs");
read = keyboard.nextLine(); read = keyboard.nextLine();
read = read.trim();
file = new File(read + ".txt"); file = new File(read + ".txt");
if(!read.equals("") && !file.exists()) if(!read.equals("") && !file.exists())
System.out.println("File not found, try again"); System.out.println("File not found, try again");