Initial round of bugfixes to make program actually work

This commit is contained in:
Bradlee Speice 2012-11-13 17:22:47 -05:00
parent 7e80fda0c0
commit c062724071
4 changed files with 42 additions and 19 deletions

View File

@ -21,10 +21,12 @@ public class jobThread extends Thread {
this.parentAlgorithm = parentClass;
this.elapsedTime = 0;
this.isPaused = false;
this.startTime = 0;
this.startTime = System.currentTimeMillis();
this.jobID = jobID;
this.jobSize = jobSize;
this.beginningLocation = beginningLocation;
this.jobDone = false;
}
public void setBeginning(int newBeginning){

View File

@ -30,7 +30,8 @@ public class memoryManagement{
int[] time = new int[JOBAMOUNT];
//******Add your algorithm class here******//
baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
//baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE);
//Gets a file name, else creates five random jobs
do{
@ -43,7 +44,7 @@ public class memoryManagement{
//Create random jobs or read from the file and create jobs
if(read.equals("")){
System.out.print("Creating "+JOBAMOUNT+" random jobs...");
System.out.print("Creating " + JOBAMOUNT + " random jobs...");
jobLength = JOBAMOUNT;
for(int i = 0; i < jobLength; i++){
id[i] = i+1;
@ -67,15 +68,21 @@ public class memoryManagement{
}
//Send jobs to algorithm, time is calculated and printed out after completion
System.out.print("Sending jobs to algorithm...");
System.out.print("Sending jobs to threaded allocation algorithm...");
timeStart = System.currentTimeMillis();
for(int i = 0; i < jobLength; i++){
alg.allocate(id[i], size[i], time[i]);
//Note that we use `jobLength - 1` to compensate for the id above
for(int i = 0; i < jobLength - 1; i++){
Bradlee_Speice.allocate(id[i], size[i], time[i]);
}
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete");
System.out.println("Elapsed time for algorithm to complete "+ jobLength+" jobs is "+timeEnd+" milliseconds");
System.out.println("Elapsed time for threaded allocation algorithm to complete " + jobLength +
" jobs is " + timeEnd + " milliseconds");
//Put other algorithms here.
System.out.println("Completed Successfully");
//Forcibly close down all threads
System.exit(0);
}
}

View File

@ -13,7 +13,7 @@ public class threadedAllocation implements baseAlgorithm{
memoryBlock = new int[memorySize];
this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray);
this.garbageThread.run();
this.garbageThread.start();
// Set up the array of job references
jobArray = new Job[memoryManagement.JOBAMOUNT];
@ -41,8 +41,10 @@ public class threadedAllocation implements baseAlgorithm{
free += 1;
}
if (free >= blockSize)
if (free >= blockSize){
System.out.println("Found a block of size " + blockSize + " at " + beginningLoc);
return beginningLoc;
}
}
//Once the above loop has exited, we've run out of locations to check
return -1;
@ -51,6 +53,7 @@ public class threadedAllocation implements baseAlgorithm{
public void allocate(int jobID, int jobSize, int jobLength ){
/* Over-rides allocate() of baseAlgorithm */
try{
System.err.println("Allocating job with ID " + jobID);
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//Loop until we get a block big enough for our job
@ -65,9 +68,8 @@ public class threadedAllocation implements baseAlgorithm{
memoryBlock[beginningLocation + x] = jobID;
}
//TODO: Code to start the job
Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this);
jobArray[jobID] = newJob;
jobArray[jobID - 1] = newJob;
newJob.start();
} catch (Exception e){
System.out.println("Could not allocate job with ID " + jobID);
@ -76,7 +78,7 @@ public class threadedAllocation implements baseAlgorithm{
public void deallocate(int jobSize, int beginningLocation){
/* Over-rides deallocate() of baseAlgorithm */
//Simple algorithm, basically just mark the memory as cleared.
for (int x = 0; x < jobSize; x++)
{

View File

@ -26,20 +26,23 @@ class threadedAllocationGarbage extends Thread
int maxFreeSize = 0, maxFreeIndex = 0;
while (memoryLoc < this.memoryBlock.length)
{
if (this.memoryBlock[memoryLoc] != 0)
if (this.memoryBlock[memoryLoc] != 0){
//Block isn't free
memoryLoc++;
continue;
}
//One location is free, find out total size free
//This loop breaks either when we've found the size we need, or
//we found the beginning of the next block.
int beginningLoc = memoryLoc;
int free = 0;
while (this.memoryBlock[memoryLoc] == 0)
while (memoryLoc < this.memoryBlock.length && this.memoryBlock[memoryLoc] == 0)
{
memoryLoc += 1;
free += 1;
}
//We've found the end of that chunk, see if it's bigger than what we have on file
if (free > maxFreeSize){
maxFreeSize = free;
@ -75,11 +78,19 @@ class threadedAllocationGarbage extends Thread
continue;
//Find out what ID the job is, and how big it is
int jobID = this.memoryBlock[maxFreeBeginning + maxFreeSize + 1];
//Make it safe though - if we reach the end of the array, we don't
//want to cause an out-of-bounds exception
int jobIndex = maxFreeBeginning + maxFreeSize + 1;
int jobID;
if (jobIndex < this.memoryBlock.length)
jobID = this.memoryBlock[maxFreeBeginning + maxFreeSize + 1];
else
continue;
int jobSize = 0;
int counter = maxFreeBeginning + maxFreeSize;
while (this.memoryBlock[counter] == jobID){
//Note that the logic must be in this order to short-circuit if it would cause an array-out-of-bounds
while (counter < this.memoryBlock.length && this.memoryBlock[counter] == jobID){
counter++;
jobSize++;
}
@ -88,7 +99,8 @@ class threadedAllocationGarbage extends Thread
//Note that we need to lock out the allocation to prevent a race
synchronized (this.memoryBlock) {
//Pause the job operation
jobArray[jobID].pause();
System.out.println("Job ID about to pause: " + jobID );
jobArray[jobID - 1].pause();
//Write the job into the free space
int memoryLoc = maxFreeBeginning;
@ -99,10 +111,10 @@ class threadedAllocationGarbage extends Thread
}
//Inform the job of its new beginning location
jobArray[jobID].setBeginningLocation(maxFreeBeginning);
jobArray[jobID - 1].setBeginningLocation(maxFreeBeginning);
//Restart the job
jobArray[jobID].resume();
jobArray[jobID - 1].resume();
//Write the remaining memory as free
counter = 0;