Added braces to loops in main class

Added debugging code to best fit and worst fit
This commit is contained in:
David Weber 2012-11-27 04:39:37 -05:00
parent 964248fda7
commit 74a3b51404
3 changed files with 56 additions and 37 deletions

View File

@ -56,18 +56,21 @@ public class BestFitAlgorithm implements baseAlgorithm{
} }
} }
} }
//System.out.println("Size of indices array: " + indices.size()); System.out.println("Size of indices array: " + indices.size());
//System.out.println("Size of sizes array: " + blocks.size()); System.out.println("Size of sizes array: " + blocks.size());
for(int i = 0; i < blocks.size(); i++) for(int i = 0; i < blocks.size(); i++)
{ {
//System.out.println("Index: " + indices.get(i)); System.out.println("Index: " + indices.get(i));
//System.out.println("Size: " + blocks.get(i)); System.out.println("Size: " + blocks.get(i));
} }
int bSize = -1;
int bestIndex = -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 //GET BEST INDEX
for(int i = 0; i < blocks.size(); i++) for(int i = 0; i < blocks.size(); i++)
@ -78,16 +81,17 @@ public class BestFitAlgorithm implements baseAlgorithm{
//Best possible fit. You're done. //Best possible fit. You're done.
//System.out.println("Best Case"); //System.out.println("Best Case");
bestIndex = indices.get(i).intValue(); bestIndex = indices.get(i).intValue();
return bestIndex;
} }
else if((blocks.get(i).intValue() <= bSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1) else if((blocks.get(i).intValue() <= bSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1)
{ {
bestIndex = indices.get(i).intValue(); bestIndex = indices.get(i).intValue();
return bestIndex;
} }
} }
//System.out.println("bestIndex: " + bestIndex); //System.out.println("bestIndex: " + bestIndex);
//System.out.println("bSize: " + bSize); //System.out.println("bSize: " + bSize);
return bestIndex; return bestIndex;
} }
@ -108,7 +112,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
if(bestSizeIndex == -1) if(bestSizeIndex == -1)
{ {
//Compact and try again //Compact and try again
//System.out.println("Compacting memory..."); System.out.println("Compacting memory...");
this.compact(); this.compact();
bestSizeIndex = this.getBestIndex(jobSize); bestSizeIndex = this.getBestIndex(jobSize);
} }
@ -121,12 +125,12 @@ public class BestFitAlgorithm implements baseAlgorithm{
{ {
for(int i = bestSizeIndex; i < jobSize + bestSizeIndex; i++) 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; 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); Job newJob = new Job(jobSize, jobID, jobSize, bestSizeIndex, deallocateMethod, this);
@ -134,12 +138,14 @@ public class BestFitAlgorithm implements baseAlgorithm{
newJob.start(); newJob.start();
//System.out.println("Job started!"); System.out.println("Job started!");
} }
} }
catch (Exception e) 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) 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;
} }
} }
} }

View File

@ -138,9 +138,10 @@ public class WorstFitAlgorithm implements baseAlgorithm{
//System.out.println("Job started!"); //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 @Override
public void deallocate(int jobSize, int beginningLocation) 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;
} }
} }
} }

View File

@ -44,7 +44,9 @@ public class memoryManagement{
read = keyboard.nextLine(); read = keyboard.nextLine();
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");
}
}while(!read.equals("") && !file.exists()); }while(!read.equals("") && !file.exists());
//Create random jobs or read from the file and create jobs //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 //Send jobs to algorithm, time is calculated and printed out after completion
//Note that we use `jobLength - 1` to compensate for the id above //Note that we use `jobLength - 1` to compensate for the id above
//Threaded Fit //Threaded Fit
System.out.print("Sending jobs to threaded allocation algorithm..."); System.out.print("Sending jobs to threaded allocation algorithm...");
timeStart[0] = System.currentTimeMillis(); timeStart[0] = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++) for(int i = 0; i < jobLength - 1; i++) {
threadedFit.allocate(id[i], size[i], time[i]); threadedFit.allocate(id[i], size[i], time[i]);
}
timeEnd[0] = System.currentTimeMillis() - timeStart[0]; timeEnd[0] = System.currentTimeMillis() - timeStart[0];
System.out.println("complete"); System.out.println("complete");
System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[0] + " milliseconds"); System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[0] + " milliseconds");
//Best Fit //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(); timeStart[1] = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++) for(int i = 0; i < jobLength - 1; i++) {
bestFit.allocate(id[i], size[i], time[i]); System.out.println("**********NEXT ITERATION***********");
bestFit.allocate(id[i], size[i], time[i]);
}
timeEnd[1] = System.currentTimeMillis() - timeStart[1]; timeEnd[1] = System.currentTimeMillis() - timeStart[1];
System.out.println("complete"); System.out.println("complete");
System.out.println("Elapsed time for best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[1] + " milliseconds"); 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 //Worst Fit
System.out.print("Sending jobs to worst fit allocation algorithm..."); System.out.print("Sending jobs to worst fit allocation algorithm...");
timeStart[2] = System.currentTimeMillis(); timeStart[2] = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++) for(int i = 0; i < jobLength - 1; i++) {
worstFit.allocate(id[i], size[i], time[i]); worstFit.allocate(id[i], size[i], time[i]);
}
timeEnd[2] = System.currentTimeMillis() - timeStart[2]; timeEnd[2] = System.currentTimeMillis() - timeStart[2];
System.out.println("complete"); System.out.println("complete");
System.out.println("Elapsed time for worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[2] + " milliseconds"); 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 //First Fit
System.out.print("Sending jobs to first fit allocation algorithm..."); System.out.print("Sending jobs to first fit allocation algorithm...");
timeStart[3] = System.currentTimeMillis(); timeStart[3] = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++) for(int i = 0; i < jobLength - 1; i++) {
firstFit.allocate(id[i], size[i], time[i]); firstFit.allocate(id[i], size[i], time[i]);
}
timeEnd[3] = System.currentTimeMillis() - timeStart[3]; timeEnd[3] = System.currentTimeMillis() - timeStart[3];
System.out.println("complete"); System.out.println("complete");
System.out.println("Elapsed time for first fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[3] + " milliseconds"); 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 //Next Fit
System.out.print("Sending jobs to next fit allocation algorithm..."); System.out.print("Sending jobs to next fit allocation algorithm...");
timeStart[4] = System.currentTimeMillis(); timeStart[4] = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++) for(int i = 0; i < jobLength - 1; i++) {
nextFit.allocate(id[i], size[i], time[i]); nextFit.allocate(id[i], size[i], time[i]);
}
timeEnd[4] = System.currentTimeMillis() - timeStart[4]; timeEnd[4] = System.currentTimeMillis() - timeStart[4];
System.out.println("complete"); System.out.println("complete");
System.out.println("Elapsed time for next fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd[4] + " milliseconds"); 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..."); System.out.print("Printing to log...");
out.println("Memory Management Log"); out.println("Memory Management Log");
out.println("---------------------------"); out.println("---------------------------");
if(read.equals("")) if(read.equals("")) {
out.println("Job Assignment: Random"); out.println("Job Assignment: Random");
else }
out.println("Job Assignment: " + read + ".txt"); else {
out.println("Job Assignment: " + read + ".txt");
}
out.println("Job Amount: " + jobLength); out.println("Job Amount: " + jobLength);
out.println("Memory Size: " + MEMORYSIZE); out.println("Memory Size: " + MEMORYSIZE);
out.println("---------------------------"); out.println("---------------------------");