Completed most of best/worst fit algorithms

Modified main class to run these algorithms

Signed-off-by: David Weber <djw612@gmail.com>
This commit is contained in:
David Weber 2012-11-20 19:03:08 -08:00
parent e6e1a8b634
commit b599237482
3 changed files with 284 additions and 196 deletions

View File

@ -5,163 +5,183 @@
*/ */
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.*;
public class BestFitAlgorithm implements baseAlgorithm{ public class BestFitAlgorithm implements baseAlgorithm{
int memoryBlock[]; int memoryBlock[];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10]; private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
ArrayList<Integer> candidates; List<Integer> indices;
List<Integer> blocks;
int memoryLocation;
int bestSize; //The most suitable block size for the job
int bestSizeIndex; //The most suitable block size starting index for the job
public BestFitAlgorithm(int memorySize) public BestFitAlgorithm(int memorySize)
{ {
//Initialize memory block to whatever the size is //Initialize memory block to whatever the size is
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
System.out.println("The size of the memory is: " + memoryBlock.length); blocks = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
indices = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
} }
public int getBestSizeIndex(int jobSize) public int getBestIndex(int jobSize)
{ {
int bestSize; //The most suitable block size for the job memoryLocation = 0;
int bestSizeIndex; //The most suitable block size starting index for the job
System.out.println("The size of the job is: " + jobSize); indices.clear();
blocks.clear();
candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size) while (memoryLocation < this.memoryBlock.length)
int counter = 0; //Counter for measuring unallocated memory
//Scan through memory block and get free blocks. Add candidates for allocation to array list
for(int i = 0; i < memoryBlock.length; i++)
{
//If position in memory block here is 0, iterate from that index and count up sequential 0's (free space)
if(memoryBlock[i] == 0)
{
for(int j = i; j < memoryBlock.length - i; j++)
{
if(memoryBlock[j] == 0)
{
counter++;
}
}
if(counter == jobSize)
{
candidates.add(i); //Store index
candidates.add(counter); //Store size of free memory chunk
}
else if(counter >= jobSize)
{
candidates.add(i); //Store index
candidates.add(counter); //Store size of free memory chunk
}
//System.out.println("The size of the counter is: " + counter);
counter = 0;
}
}
for(int i = 0; i < candidates.size(); i++)
{
System.out.println("Candidate index: " + candidates.get(i));
System.out.println("Candidate size: " + candidates.get(i+1));
}
//Iterate through candidate sizes
bestSizeIndex = candidates.get(0).intValue(); //Initialize best index to first spot in array list
bestSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate
//Iterate through sizes and find the best fit
for(int i = 1; i < candidates.size(); i=i+2)
{
//Best possible case: job size = free block size (you're done)
if(candidates.get(i).intValue() == jobSize)
{
bestSizeIndex = i - 1;
System.out.println("The best size index is: " + bestSizeIndex);
return bestSizeIndex; //You're done. Return the value.
}
//If the current size is less than the previous best size, make this the new best size
else if(candidates.get(i).intValue() < bestSize)
{
bestSize = candidates.get(i+1).intValue();
bestSizeIndex = i - 1;
System.out.println("The best size index is: " + bestSizeIndex);
}
}
System.out.println("The best size is: " + bestSize);
//No candidates
if(candidates.isEmpty())
{ {
System.out.println("No best size index"); if (memoryBlock[memoryLocation] != 0){
return -1; memoryLocation++;
continue;
}
int beginningLoc = memoryLocation;
int free = 0;
while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{
memoryLocation++;
free++;
}
if (free >= jobSize){
//System.out.println("Found a block of size " + free + " at " + beginningLoc);
blocks.add(free);
indices.add(beginningLoc);
}
} }
return bestSizeIndex; //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));
}
int bestIndex = -1;
int bSize = blocks.get(0).intValue();
//GET BEST INDEX
for(int i = 0; i < blocks.size(); i++)
{
//BEST CASE
if(blocks.get(i).intValue() == jobSize)
{
//Best possible fit. You're done.
//System.out.println("Best Case");
bestIndex = indices.get(i).intValue();
}
else if((blocks.get(i).intValue() <= bSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1)
{
bestIndex = indices.get(i).intValue();
}
}
//System.out.println("bestIndex: " + bestIndex);
//System.out.println("bSize: " + bSize);
return bestIndex;
} }
@Override @Override
public void allocate(int jobID, int jobSize, int jobTime) public void allocate(int jobID, int jobSize, int jobTime)
{ {
try try
{ {
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//checks to see if the job will fit in memory bestSizeIndex = this.getBestIndex(jobSize);
if(jobSize>memoryBlock.length)
if(jobSize > memoryBlock.length)
{ {
System.out.println("This job is too large"); //System.out.println("Job is too large for current memory size");
System.exit(0);
} }
int bestSizeIndex = getBestSizeIndex(jobSize); if(bestSizeIndex == -1)
//No candidates found
if(bestSizeIndex == -1)
{ {
System.out.println("No candidates found...attempting to compact"); //Compact and try again
//Try compacting, then attempt to get an index again //System.out.println("Compacting memory...");
compact(); this.compact();
bestSizeIndex = this.getBestIndex(jobSize);
bestSizeIndex = getBestSizeIndex(jobSize);
//Compacting still didn't produce an appropriate block
if(bestSizeIndex == -1)
{
//TODO .....
}
} }
else else
{ {
//Allocate the memory //System.out.println("The size of the job is: " + jobSize);
for(int i = bestSizeIndex; i < jobSize; i++) //System.out.println("The best size index is: " + bestSizeIndex);
{
memoryBlock[i] = jobID;
}
System.out.println("Successfully allocated!"); synchronized(memoryBlock)
for(int i = 0; i < memoryBlock.length; i++)
{ {
System.out.println("Job at position " + i + "in memoryblock: " + memoryBlock[i]); for(int i = bestSizeIndex; i < jobSize + bestSizeIndex; i++)
{
//System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block!");
this.memoryBlock[i] = jobID;
}
} }
}
} //System.out.println("Successfully allocated! Starting job...");
Job newJob = new Job(jobSize, jobID, jobSize, bestSizeIndex, deallocateMethod, this);
jobArray[jobID] = newJob;
newJob.start();
//System.out.println("Job started!");
}
}
catch (Exception e) catch (Exception e)
{ {
System.out.println("Could not allocate job with ID " + jobID); //System.out.println("Could not allocate job with ID " + jobID);
} }
} }
/*
* This method gathers all occupied memory and stores it contiguously in an array list of blocks.
* After that, it rewrites the memoryBlock array by writing the memory in contiguous order, and then
* filling in the rest of the memory with 0's
*/
public void compact() public void compact()
{ {
//TODO: Compact memory if no suitable allocation candidates are found on the first pass List<Integer> takenBlocks = new ArrayList<>();
memoryLocation = 0;
//Gather allocated memory
while(memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] != 0)
{
takenBlocks.add(memoryBlock[memoryLocation]);
memoryLocation++;
}
for(int i = 0; i < takenBlocks.size(); i++)
{
this.memoryBlock[i] = takenBlocks.get(i).intValue();
}
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{
this.memoryBlock[i] = 0;
}
/*System.out.println("Successfully compacted!");
if(takenBlocks.isEmpty())
{
System.out.println("Cannot compact!");
}
*/
} }
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{ {
for(int i = beginningLocation; i < jobSize; i++) for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{ {
memoryBlock[i] = 0; memoryBlock[i] = 0;
} }

View File

@ -1,111 +1,188 @@
/* /*
* Worst Fit Algorithm by David Weber * Best Fit Algorithm by David Weber
* ITCS 3146 * ITCS 3146
* 11/9/2012 * 11/9/2012
*/ */
import java.util.ArrayList; import java.lang.reflect.Method;
import java.util.*;
public class WorstFitAlgorithm implements baseAlgorithm{ public class WorstFitAlgorithm implements baseAlgorithm{
int memoryBlock[]; int memoryBlock[];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
List<Integer> indices;
List<Integer> blocks;
int memoryLocation;
int worstSize; //The most suitable block size for the job
int worstSizeIndex; //The most suitable block size starting index for the job
public WorstFitAlgorithm(int memorySize) public WorstFitAlgorithm(int memorySize)
{ {
//Initialize memory block to whatever the size is //Initialize memory block to whatever the size is
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
blocks = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
indices = new ArrayList<>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size);
} }
public int getWorstSizeIndex(int jobSize) public int getWorstIndex(int jobSize)
{ {
int worstSize = 0; memoryLocation = 0;
int worstSizeIndex = 0;
ArrayList<Integer> candidates = new ArrayList<Integer>(); //Dynamically resizable array list for allocation candidates (interleaved with index and memory size) indices.clear();
blocks.clear();
int counter = 0; //Counter for measuring unallocated memory
//Scan through memory block and get free blocks while (memoryLocation < this.memoryBlock.length)
for(int i = 0; i < memoryBlock.length; i++)
{
//If position in memory block here is 0, iterate from that index and count up sequential 0's
if(memoryBlock[i] == 0)
{
for(int j = i; j < memoryBlock.length - i; j++)
{
if(memoryBlock[j] == 0)
{
counter++;
}
}
if(counter >= jobSize)
{
candidates.add(i); //Store index
candidates.add(counter); //Store size of free memory chunk
}
counter = 0;
}
}
//Iterate through candidate sizes
worstSizeIndex = 0; //Initialize best index to first spot in array list
worstSize = candidates.get(1).intValue(); //Initialize bestSize to first space size in candidate
//Iterate through sizes and find the best fit
for(int i = 1; i < candidates.size(); i=i+2)
{
//If the current size is greater than the previous best size, make this the new best size
if(candidates.get(i).intValue() > worstSize)
{
worstSize = candidates.get(i+1).intValue();
worstSizeIndex = i - 1;
}
}
//If the best size is less than the job size, run this again
if(candidates.isEmpty())
{ {
return -1; if (memoryBlock[memoryLocation] != 0){
memoryLocation++;
continue;
}
int beginningLoc = memoryLocation;
int free = 0;
while (memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] == 0)
{
memoryLocation++;
free++;
}
if (free >= jobSize){
//System.out.println("Found a block of size " + free + " at " + beginningLoc);
blocks.add(free);
indices.add(beginningLoc);
}
} }
return worstSizeIndex; //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));
}
int worstIndex = -1;
int wSize = blocks.get(0).intValue();
//GET WORST INDEX
for(int i = 0; i < blocks.size(); i++)
{
//Get largest possible free block to allocate to
if((blocks.get(i).intValue() >= wSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1)
{
worstIndex = indices.get(i).intValue();
}
//"Worst fit"....same size as job size
else if(blocks.get(i).intValue() == jobSize)
{
//"Worst" possible fit. You're done.
//System.out.println("Worst Case");
worstIndex = indices.get(i).intValue();
}
}
//System.out.println("bestIndex: " + bestIndex);
//System.out.println("bSize: " + bSize);
return worstIndex;
} }
@Override @Override
public void allocate(int jobID, int jobSize, int jobTime) public void allocate(int jobID, int jobSize, int jobTime)
{ {
int worstSizeIndex = getWorstSizeIndex(jobSize); try
if(worstSizeIndex == -1) //No candidates found
{ {
//Try compacting, then attempt to get an index again Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
compact();
worstSizeIndex = getWorstSizeIndex(jobSize); worstSizeIndex = this.getWorstIndex(jobSize);
if(jobSize > memoryBlock.length)
{
//System.out.println("Job is too large for current memory size");
}
//Compacting still didn't produce an appropriate block
if(worstSizeIndex == -1) if(worstSizeIndex == -1)
{ {
//TODO ..... //Compact and try again
//System.out.println("Compacting memory...");
this.compact();
worstSizeIndex = this.getWorstIndex(jobSize);
} }
} else
{
//System.out.println("The size of the job is: " + jobSize);
//System.out.println("The best size index is: " + bestSizeIndex);
synchronized(memoryBlock)
{
for(int i = worstSizeIndex; i < jobSize + worstSizeIndex; i++)
{
//System.out.println("Writing jobID: " + jobID + " to position " + i + " in memory block!");
this.memoryBlock[i] = jobID;
}
}
//System.out.println("Successfully allocated! Starting job...");
Job newJob = new Job(jobSize, jobID, jobSize, worstSizeIndex, deallocateMethod, this);
//Allocate the memory jobArray[jobID] = newJob;
for(int i = worstSizeIndex; i < jobSize; i++)
newJob.start();
//System.out.println("Job started!");
}
}
catch (Exception e)
{ {
memoryBlock[i] = jobID; //System.out.println("Could not allocate job with ID " + jobID);
} }
} }
/*
* This method gathers all occupied memory and stores it contiguously in an array list of blocks.
* After that, it rewrites the memoryBlock array by writing the memory in contiguous order, and then
* filling in the rest of the memory with 0's
*/
public void compact() public void compact()
{ {
//TODO: Compact memory if no suitable allocation candidates are found on the first pass List<Integer> takenBlocks = new ArrayList<>();
memoryLocation = 0;
//Gather allocated memory
while(memoryLocation < this.memoryBlock.length && memoryBlock[memoryLocation] != 0)
{
takenBlocks.add(memoryBlock[memoryLocation]);
memoryLocation++;
}
for(int i = 0; i < takenBlocks.size(); i++)
{
this.memoryBlock[i] = takenBlocks.get(i).intValue();
}
for(int i = takenBlocks.size(); i < this.memoryBlock.length; i++)
{
this.memoryBlock[i] = 0;
}
/*
System.out.println("Successfully compacted!");
if(takenBlocks.isEmpty())
{
System.out.println("Cannot compact!");
}
*/
} }
@Override @Override
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jobSize, int beginningLocation)
{ {
for(int i = beginningLocation; i < jobSize; i++) for(int i = beginningLocation; i < jobSize + beginningLocation; i++)
{ {
memoryBlock[i] = 0; memoryBlock[i] = 0;
} }

View File

@ -88,35 +88,26 @@ public class memoryManagement{
System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength + System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength +
" jobs is " + timeEnd + " milliseconds"); " jobs is " + timeEnd + " milliseconds");
//***Best Fit (David Weber)*** //***Best Fit (David Weber)***
timeStart = System.currentTimeMillis(); timeStart = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++){ for(int i = 0; i < jobLength - 1; i++){
David_Weber_BestFit.allocate(id[i], size[i], time[i]); David_Weber_BestFit.allocate(id[i], size[i], time[i]);
} }
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete");
System.out.println("Elapsed time for threaded best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("Elapsed time for best fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
/*
//***Worst Fit (David Weber)*** //***Worst Fit (David Weber)***
timeStart = System.currentTimeMillis(); timeStart = System.currentTimeMillis();
for(int i = 0; i < jobLength - 1; i++){ for(int i = 0; i < jobLength - 1; i++){
//David_Weber_WorstFit.allocate(id[i], size[i], time[i]); David_Weber_WorstFit.allocate(id[i], size[i], time[i]);
} }
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete");
System.out.println("Elapsed time for threaded worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
*/
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("Elapsed time for worst fit allocation algorithm to complete " + jobLength + " jobs is " + timeEnd + " milliseconds");
//Put other algorithms here. //Put other algorithms here.
System.out.println("Completed Successfully"); System.out.println("Completed Successfully");
//Forcibly close down all threads //Forcibly close down all threads
System.exit(0); System.exit(0);