This commit is contained in:
David Weber 2012-11-17 02:00:35 -05:00
commit 0928f69c77
6 changed files with 137 additions and 72 deletions

View File

@ -4,6 +4,8 @@
this class sets up a First Fit memory scheme this class sets up a First Fit memory scheme
*/ */
import java.lang.reflect.*;
//this section sets up the Car class //this section sets up the Car class
class FirstFit implements baseAlgorithm class FirstFit implements baseAlgorithm
{ {
@ -15,20 +17,21 @@ class FirstFit implements baseAlgorithm
startLoc, startLoc,
endLoc, endLoc,
blkSize, blkSize,
memSize, memSize = memoryManagement.MEMORYSIZE,
active, active,
noJobs=0, noJobs=0,
s1=0, s1=0,
compMemTest=0, compMemTest=0,
jobLoaded=0,
tableEntries=1; tableEntries=1;
private int[] tempVal = new int[6]; private int[] tempVal = new int[6];
private int[][] memTable = new int[memSize+2][6]; private int[][] memTable = new int[memSize+2][6];
private int[] memory = new int[memSize]; private int[] memory = new int[memSize];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
//this is a no argument constructor //this is a no argument constructor
public FirstFit(int memSize) public FirstFit()
{ {
this.memSize = memSize;
memTable[0][0]=0; //job number memTable[0][0]=0; //job number
memTable[0][1]=0; //job size memTable[0][1]=0; //job size
memTable[0][2]=0; //start location in memory memTable[0][2]=0; //start location in memory
@ -47,6 +50,12 @@ class FirstFit implements baseAlgorithm
noJobs++; noJobs++;
s1=0; s1=0;
//Bradlee's code ***********************************************************************************
try
{
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//checks to see if the job will fit in memory //checks to see if the job will fit in memory
if(jobSize>memSize) if(jobSize>memSize)
{ {
@ -56,6 +65,15 @@ class FirstFit implements baseAlgorithm
System.exit(0); System.exit(0);
} }
//this will loop until job is loaded into memory
do
{
//this section looks for a place to put the new job //this section looks for a place to put the new job
do do
{ {
@ -70,7 +88,10 @@ class FirstFit implements baseAlgorithm
memTable[s1][3] = jobSize-1; memTable[s1][3] = jobSize-1;
memTable[s1][4] = memTable[0][3]-memTable[0][2]+1; memTable[s1][4] = memTable[0][3]-memTable[0][2]+1;
memTable[s1][5] = 1; memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]); fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
memTable[s1+1][0] = 0; memTable[s1+1][0] = 0;
memTable[s1+1][1] = 0; memTable[s1+1][1] = 0;
memTable[s1+1][2] = memTable[s1][3]+1; memTable[s1+1][2] = memTable[s1][3]+1;
@ -78,6 +99,8 @@ class FirstFit implements baseAlgorithm
memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][4] = memSize-memTable[s1+1][2];
memTable[s1+1][5] = -1; memTable[s1+1][5] = -1;
tableEntries++; tableEntries++;
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2; s1=memSize*2;
} }
//runs after the first job and if the only available slot is at the end of memory //runs after the first job and if the only available slot is at the end of memory
@ -89,7 +112,10 @@ class FirstFit implements baseAlgorithm
memTable[s1][3] = jobSize+memTable[s1][2]-1; memTable[s1][3] = jobSize+memTable[s1][2]-1;
memTable[s1][4] = memTable[s1][3]-memTable[s1][2]+1; memTable[s1][4] = memTable[s1][3]-memTable[s1][2]+1;
memTable[s1][5] = 1; memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]); fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
memTable[s1+1][0] = 0; memTable[s1+1][0] = 0;
memTable[s1+1][1] = 0; memTable[s1+1][1] = 0;
memTable[s1+1][2] = memTable[s1][3]+1; memTable[s1+1][2] = memTable[s1][3]+1;
@ -97,6 +123,8 @@ class FirstFit implements baseAlgorithm
memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][4] = memSize-memTable[s1+1][2];
memTable[s1+1][5] = -1; memTable[s1+1][5] = -1;
tableEntries++; tableEntries++;
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2; s1=memSize*2;
} }
} }
@ -106,7 +134,12 @@ class FirstFit implements baseAlgorithm
memTable[s1][0] = jobId; memTable[s1][0] = jobId;
memTable[s1][1] = jobSize; memTable[s1][1] = jobSize;
memTable[s1][5] = 1; memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]); fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2; s1=memSize*2;
} }
else else
@ -115,69 +148,61 @@ class FirstFit implements baseAlgorithm
} }
}while(s1<tableEntries); }while(s1<tableEntries);
//if job will not fit this section will compress memory
//if job will not fit this section will compress memory and try placing the job again
if(s1==tableEntries) if(s1==tableEntries)
{ {
noJobs=noJobs-1; noJobs=noJobs-1;
compMem(); compMem();
allocate(ID, size, jobTime);
}
} }
//this method is used if you want to deallocate a job by jobId }while(jobLoaded==0);
public void removeJob(int ID)
{
jobId = ID;
s1=0; s1=0;
do jobLoaded=0;
} catch (Exception e)
{ {
if(memTable[s1][0] == jobId) System.out.println("Could not allocate job with ID " + jobId);
{
jobSize = memTable[s1][1];
startLoc = memTable[s1][2];
s1=memSize*2;
} }
else
{
s1++;
} }
}while (s1<tableEntries);
deallocate(jobSize, startLoc);
}
//this method removes a job it does not check to see if the job exisits //this method removes a job it does not check to see if the job exisits
public void deallocate(int jobSize, int beginningLocation) public void deallocate(int jSize, int beginningLocation)
//public void removeJob(int ID)
{ {
jobId = 0; jobSize = jSize;
jobSize = jobSize;
startLoc = beginningLocation; startLoc = beginningLocation;
s1=0; s1=0;
do do
{ {
if(memTable[s1][2] == startLoc) if(memTable[s1][2] == startLoc)
{ {
System.out.println(startLoc+" removed job "+memTable[s1][0]);
memTable[s1][0] = 0; memTable[s1][0] = 0;
memTable[s1][1] = 0; memTable[s1][1] = 0;
memTable[s1][5] = 0; memTable[s1][5] = 0;
s1=memSize*2; System.out.println(memTable[s1][0]+" "+memTable[s1][1]+" "+memTable[s1][5]);
jobId=-1; System.out.println(toString());
noJobs--; noJobs--;
s1=memSize*2;
} }
else else
{ {
s1++; s1++;
} }
}while (s1<tableEntries); }while (s1<tableEntries);
} }
//this method compacts the memory //this method compacts the memory
public void compMem() public void compMem()
{ {
//System.out.println("Compacting Memory");
compMemTest=tableEntries; compMemTest=tableEntries;
for(int c=0; c<=compMemTest; c++) for(int c=0; c<=compMemTest; c++)
{ {

View File

@ -4,6 +4,8 @@
this class sets up a First Fit memory scheme this class sets up a First Fit memory scheme
*/ */
import java.lang.reflect.*;
//this section sets up the Car class //this section sets up the Car class
class NextFit implements baseAlgorithm class NextFit implements baseAlgorithm
{ {
@ -50,6 +52,12 @@ class NextFit implements baseAlgorithm
s1=0; s1=0;
loopCount=0; loopCount=0;
//Bradlee's code ***********************************************************************************
try
{
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//checks to see if the job will fit in memory //checks to see if the job will fit in memory
if(jobSize>memSize) if(jobSize>memSize)
{ {
@ -74,7 +82,9 @@ class NextFit implements baseAlgorithm
memTable[currentPosition][3] = jobSize-1; memTable[currentPosition][3] = jobSize-1;
memTable[currentPosition][4] = memTable[0][3]-memTable[0][2]+1; memTable[currentPosition][4] = memTable[0][3]-memTable[0][2]+1;
memTable[currentPosition][5] = 1; memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]); fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
memTable[currentPosition+1][0] = 0; memTable[currentPosition+1][0] = 0;
memTable[currentPosition+1][1] = 0; memTable[currentPosition+1][1] = 0;
memTable[currentPosition+1][2] = memTable[currentPosition][3]+1; memTable[currentPosition+1][2] = memTable[currentPosition][3]+1;
@ -95,7 +105,9 @@ class NextFit implements baseAlgorithm
memTable[currentPosition][3] = jobSize+memTable[currentPosition][2]-1; memTable[currentPosition][3] = jobSize+memTable[currentPosition][2]-1;
memTable[currentPosition][4] = memTable[currentPosition][3]-memTable[currentPosition][2]+1; memTable[currentPosition][4] = memTable[currentPosition][3]-memTable[currentPosition][2]+1;
memTable[currentPosition][5] = 1; memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]); fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
memTable[currentPosition+1][0] = 0; memTable[currentPosition+1][0] = 0;
memTable[currentPosition+1][1] = 0; memTable[currentPosition+1][1] = 0;
memTable[currentPosition+1][2] = memTable[currentPosition][3]+1; memTable[currentPosition+1][2] = memTable[currentPosition][3]+1;
@ -114,7 +126,9 @@ class NextFit implements baseAlgorithm
memTable[currentPosition][0] = jobId; memTable[currentPosition][0] = jobId;
memTable[currentPosition][1] = jobSize; memTable[currentPosition][1] = jobSize;
memTable[currentPosition][5] = 1; memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]); fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
currentPosition++; currentPosition++;
positionToCompress=currentPosition; positionToCompress=currentPosition;
s1=memSize*2; s1=memSize*2;
@ -142,8 +156,17 @@ class NextFit implements baseAlgorithm
positionToCompress=0; positionToCompress=0;
allocate(ID, size, jobTime); allocate(ID, size, jobTime);
} }
} catch (Exception e)
{
System.out.println("Could not allocate job with ID " + jobId);
} }
}
//this method is used if you want to deallocate a job by jobId //this method is used if you want to deallocate a job by jobId
public void removeJob(int ID) public void removeJob(int ID)
{ {
@ -197,7 +220,6 @@ class NextFit implements baseAlgorithm
//this method compacts the memory //this method compacts the memory
public void compMem() public void compMem()
{ {
System.out.println("***********************enter compress************************");
compMemTest=tableEntries; compMemTest=tableEntries;
for(int c=0; c<=compMemTest; c++) for(int c=0; c<=compMemTest; c++)
{ {

View File

@ -87,7 +87,6 @@ public class jobThread extends Thread {
Object[] deallocateArgs = {this.jobSize, this.beginningLocation}; Object[] deallocateArgs = {this.jobSize, this.beginningLocation};
//We're done, go ahead and notify our algorithm to clean us up //We're done, go ahead and notify our algorithm to clean us up
parentAlgorithmDeallocate.invoke(parentAlgorithm, deallocateArgs); parentAlgorithmDeallocate.invoke(parentAlgorithm, deallocateArgs);
} catch (Exception e) { } catch (Exception e) {
return; return;
} }

View File

@ -9,8 +9,8 @@ import java.util.StringTokenizer;
public class memoryManagement{ public class memoryManagement{
static final int JOBAMOUNT = 1000; static final int JOBAMOUNT = 200;
static final int MEMORYSIZE = 10000; static final int MEMORYSIZE = 100;
public static void main(String args[])throws Exception{ public static void main(String args[])throws Exception{
@ -32,6 +32,8 @@ public class memoryManagement{
//******Add your algorithm class here******// //******Add your algorithm class here******//
//baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE); //baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE); threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE);
//FirstFit David01 = new FirstFit();
//NextFit David02 = new NextFit();
//Gets a file name, else creates five random jobs //Gets a file name, else creates five random jobs
do{ do{
@ -48,8 +50,8 @@ public class memoryManagement{
jobLength = JOBAMOUNT; jobLength = JOBAMOUNT;
for(int i = 0; i < jobLength; i++){ for(int i = 0; i < jobLength; i++){
id[i] = i+1; id[i] = i+1;
size[i] = rand.nextInt(1000)+1; size[i] = rand.nextInt(5)+1;
time[i] = rand.nextInt(1000)+1; time[i] = rand.nextInt(1000)+2001;
} }
System.out.println("complete"); System.out.println("complete");
} }
@ -73,6 +75,8 @@ public class memoryManagement{
//Note that we use `jobLength - 1` to compensate for the id above //Note that we use `jobLength - 1` to compensate for the id above
for(int i = 0; i < jobLength - 1; i++){ for(int i = 0; i < jobLength - 1; i++){
Bradlee_Speice.allocate(id[i], size[i], time[i]); Bradlee_Speice.allocate(id[i], size[i], time[i]);
//David01.allocate(id[i], size[i], time[i]);
//David02.allocate(id[i], size[i], time[i]);
} }
timeEnd = System.currentTimeMillis() - timeStart; timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete"); System.out.println("complete");

View File

@ -12,11 +12,12 @@ public class threadedAllocation implements baseAlgorithm{
* our actual operations */ * our actual operations */
memoryBlock = new int[memorySize]; memoryBlock = new int[memorySize];
// Set up the array of job references
this.jobArray = new Job[memoryManagement.JOBAMOUNT + 1];
this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray); this.garbageThread = new threadedAllocationGarbage(this.memoryBlock, 20, this.jobArray);
this.garbageThread.start(); this.garbageThread.start();
// Set up the array of job references
jobArray = new Job[memoryManagement.JOBAMOUNT];
} }
int locateBlock(int blockSize){ int locateBlock(int blockSize){
@ -26,23 +27,25 @@ public class threadedAllocation implements baseAlgorithm{
int memoryLoc = 0; int memoryLoc = 0;
while (memoryLoc < this.memoryBlock.length) while (memoryLoc < this.memoryBlock.length)
{ {
if (memoryBlock[memoryLoc] != 0) if (memoryBlock[memoryLoc] != 0){
memoryLoc++;
//Block isn't free //Block isn't free
continue; continue;
}
//One location is free, find out total size free //One location is free, find out total size free
//This loop breaks either when we've found the size we need, or //This loop breaks either when we've found the size we need, or
//we found the beginning of the next block. //we found the beginning of the next block.
int beginningLoc = memoryLoc; int beginningLoc = memoryLoc;
int free = 0; int free = 0;
while (free < blockSize && memoryBlock[memoryLoc] == 0) while (free < blockSize && memoryLoc < this.memoryBlock.length && memoryBlock[memoryLoc] == 0)
{ {
memoryLoc += 1; memoryLoc += 1;
free += 1; free += 1;
} }
if (free >= blockSize){ if (free >= blockSize){
System.out.println("Found a block of size " + blockSize + " at " + beginningLoc); //System.out.println("Found a block of size " + blockSize + " at " + beginningLoc);
return beginningLoc; return beginningLoc;
} }
} }
@ -53,7 +56,6 @@ public class threadedAllocation implements baseAlgorithm{
public void allocate(int jobID, int jobSize, int jobLength ){ public void allocate(int jobID, int jobSize, int jobLength ){
/* Over-rides allocate() of baseAlgorithm */ /* Over-rides allocate() of baseAlgorithm */
try{ try{
System.err.println("Allocating job with ID " + jobID);
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class}); Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
//Loop until we get a block big enough for our job //Loop until we get a block big enough for our job
@ -62,29 +64,36 @@ public class threadedAllocation implements baseAlgorithm{
while (beginningLocation == -1) while (beginningLocation == -1)
beginningLocation = locateBlock( jobSize ); beginningLocation = locateBlock( jobSize );
//We've got a location, mark it as filled, and start the job. //We've got a location, mark it as filled, and start the job.
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++) for (int x = 0; x < jobSize; x++)
{ {
memoryBlock[beginningLocation + x] = jobID; memoryBlock[beginningLocation + x] = jobID;
} }
}
Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this); Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this);
jobArray[jobID - 1] = newJob; jobArray[jobID] = newJob;
newJob.start(); newJob.start();
} catch (Exception e){ } catch (Exception e){
System.out.println("Could not allocate job with ID " + jobID); e.printStackTrace();
System.exit(-1);
} }
} }
public void deallocate(int jobSize, int beginningLocation){ public void deallocate(int jobSize, int beginningLocation){
/* Over-rides deallocate() of baseAlgorithm */ /* Over-rides deallocate() of baseAlgorithm */
//System.err.println("Deallocation job with ID " + memoryBlock[beginningLocation] + " at time " + System.currentTimeMillis());
//Simple algorithm, basically just mark the memory as cleared. //Simple algorithm, basically just mark the memory as cleared.
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++) for (int x = 0; x < jobSize; x++)
{ {
memoryBlock[beginningLocation + x] = 0; memoryBlock[beginningLocation + x] = 0;
} }
} }
}
} }

View File

@ -70,6 +70,7 @@ class threadedAllocationGarbage extends Thread
*/ */
int[] largestBlockInfo = largestBlock(); int[] largestBlockInfo = largestBlock();
int maxFreeBeginning = largestBlockInfo[0]; int maxFreeBeginning = largestBlockInfo[0];
int maxFreeSize = largestBlockInfo[1]; int maxFreeSize = largestBlockInfo[1];
@ -97,13 +98,14 @@ class threadedAllocationGarbage extends Thread
//Pause the job, and then relocate it //Pause the job, and then relocate it
//Note that we need to lock out the allocation to prevent a race //Note that we need to lock out the allocation to prevent a race
int memoryLoc = maxFreeBeginning;
synchronized (this.memoryBlock) { synchronized (this.memoryBlock) {
//Pause the job operation try{
System.out.println("Job ID about to pause: " + jobID ); //Pause the job operation - note that we use a try-catch, as the job may disappear on us,
jobArray[jobID - 1].pause(); //but is not a fatal error.
jobArray[jobID].pause();
//Write the job into the free space //Write the job into the free space
int memoryLoc = maxFreeBeginning;
counter = 0; counter = 0;
while (counter < jobSize){ while (counter < jobSize){
memoryBlock[memoryLoc] = jobID; memoryBlock[memoryLoc] = jobID;
@ -111,15 +113,19 @@ class threadedAllocationGarbage extends Thread
} }
//Inform the job of its new beginning location //Inform the job of its new beginning location
jobArray[jobID - 1].setBeginningLocation(maxFreeBeginning); jobArray[jobID].setBeginningLocation(maxFreeBeginning);
//Restart the job //Restart the job
jobArray[jobID - 1].resume(); jobArray[jobID].resume();
} catch (Exception e){
//Job ended before we could clean it up, proceed as if nothing happened.
}
//Write the remaining memory as free //Write the remaining memory as free
counter = 0; counter = 0;
while (counter < maxFreeSize){ while (counter < maxFreeSize){
memoryBlock[memoryLoc] = 0; memoryBlock[memoryLoc] = 0;
counter++;
} }
} }