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
*/
import java.lang.reflect.*;
//this section sets up the Car class
class FirstFit implements baseAlgorithm
{
@ -15,20 +17,21 @@ class FirstFit implements baseAlgorithm
startLoc,
endLoc,
blkSize,
memSize,
memSize = memoryManagement.MEMORYSIZE,
active,
noJobs=0,
s1=0,
compMemTest=0,
jobLoaded=0,
tableEntries=1;
private int[] tempVal = new int[6];
private int[][] memTable = new int[memSize+2][6];
private int[] memory = new int[memSize];
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
//this is a no argument constructor
public FirstFit(int memSize)
public FirstFit()
{
this.memSize = memSize;
memTable[0][0]=0; //job number
memTable[0][1]=0; //job size
memTable[0][2]=0; //start location in memory
@ -47,6 +50,12 @@ class FirstFit implements baseAlgorithm
noJobs++;
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
if(jobSize>memSize)
{
@ -56,6 +65,15 @@ class FirstFit implements baseAlgorithm
System.exit(0);
}
//this will loop until job is loaded into memory
do
{
//this section looks for a place to put the new job
do
{
@ -70,7 +88,10 @@ class FirstFit implements baseAlgorithm
memTable[s1][3] = jobSize-1;
memTable[s1][4] = memTable[0][3]-memTable[0][2]+1;
memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
memTable[s1+1][0] = 0;
memTable[s1+1][1] = 0;
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][5] = -1;
tableEntries++;
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2;
}
//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][4] = memTable[s1][3]-memTable[s1][2]+1;
memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
memTable[s1+1][0] = 0;
memTable[s1+1][1] = 0;
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][5] = -1;
tableEntries++;
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2;
}
}
@ -106,7 +134,12 @@ class FirstFit implements baseAlgorithm
memTable[s1][0] = jobId;
memTable[s1][1] = jobSize;
memTable[s1][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[s1][2]);
jobArray[jobId - 1] = newJob;
newJob.start();
jobLoaded=1;
System.out.println("add job "+jobId+toString());
s1=memSize*2;
}
else
@ -114,70 +147,62 @@ class FirstFit implements baseAlgorithm
s1++;
}
}while(s1<tableEntries);
//if job will not fit this section will compress memory and try placing the job again
//if job will not fit this section will compress memory
if(s1==tableEntries)
{
noJobs=noJobs-1;
compMem();
allocate(ID, size, jobTime);
}
}while(jobLoaded==0);
s1=0;
jobLoaded=0;
} 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
public void removeJob(int ID)
{
jobId = ID;
s1=0;
do
{
if(memTable[s1][0] == 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
public void deallocate(int jobSize, int beginningLocation)
//public void removeJob(int ID)
public void deallocate(int jSize, int beginningLocation)
{
jobId = 0;
jobSize = jobSize;
jobSize = jSize;
startLoc = beginningLocation;
s1=0;
do
{
if(memTable[s1][2] == startLoc)
{
System.out.println(startLoc+" removed job "+memTable[s1][0]);
memTable[s1][0] = 0;
memTable[s1][1] = 0;
memTable[s1][5] = 0;
s1=memSize*2;
jobId=-1;
System.out.println(memTable[s1][0]+" "+memTable[s1][1]+" "+memTable[s1][5]);
System.out.println(toString());
noJobs--;
s1=memSize*2;
}
else
{
s1++;
}
}while (s1<tableEntries);
}
//this method compacts the memory
public void compMem()
{
//System.out.println("Compacting Memory");
compMemTest=tableEntries;
for(int c=0; c<=compMemTest; c++)
{

View File

@ -4,6 +4,8 @@
this class sets up a First Fit memory scheme
*/
import java.lang.reflect.*;
//this section sets up the Car class
class NextFit implements baseAlgorithm
{
@ -50,6 +52,12 @@ class NextFit implements baseAlgorithm
s1=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
if(jobSize>memSize)
{
@ -74,7 +82,9 @@ class NextFit implements baseAlgorithm
memTable[currentPosition][3] = jobSize-1;
memTable[currentPosition][4] = memTable[0][3]-memTable[0][2]+1;
memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
memTable[currentPosition+1][0] = 0;
memTable[currentPosition+1][1] = 0;
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][4] = memTable[currentPosition][3]-memTable[currentPosition][2]+1;
memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
memTable[currentPosition+1][0] = 0;
memTable[currentPosition+1][1] = 0;
memTable[currentPosition+1][2] = memTable[currentPosition][3]+1;
@ -114,7 +126,9 @@ class NextFit implements baseAlgorithm
memTable[currentPosition][0] = jobId;
memTable[currentPosition][1] = jobSize;
memTable[currentPosition][5] = 1;
Job newJob = new Job(jobTime, jobId, jobSize, memTable[currentPosition][2], deallocateMethod, this);
fillMemory(jobId, jobSize, memTable[currentPosition][2]);
newJob.start();
currentPosition++;
positionToCompress=currentPosition;
s1=memSize*2;
@ -142,8 +156,17 @@ class NextFit implements baseAlgorithm
positionToCompress=0;
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
public void removeJob(int ID)
{
@ -197,7 +220,6 @@ class NextFit implements baseAlgorithm
//this method compacts the memory
public void compMem()
{
System.out.println("***********************enter compress************************");
compMemTest=tableEntries;
for(int c=0; c<=compMemTest; c++)
{

View File

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

View File

@ -9,8 +9,8 @@ import java.util.StringTokenizer;
public class memoryManagement{
static final int JOBAMOUNT = 1000;
static final int MEMORYSIZE = 10000;
static final int JOBAMOUNT = 200;
static final int MEMORYSIZE = 100;
public static void main(String args[])throws Exception{
@ -32,6 +32,8 @@ public class memoryManagement{
//******Add your algorithm class here******//
//baseAlgorithm alg = new dummyAlgorithm(MEMORYSIZE);
threadedAllocation Bradlee_Speice = new threadedAllocation(MEMORYSIZE);
//FirstFit David01 = new FirstFit();
//NextFit David02 = new NextFit();
//Gets a file name, else creates five random jobs
do{
@ -48,8 +50,8 @@ public class memoryManagement{
jobLength = JOBAMOUNT;
for(int i = 0; i < jobLength; i++){
id[i] = i+1;
size[i] = rand.nextInt(1000)+1;
time[i] = rand.nextInt(1000)+1;
size[i] = rand.nextInt(5)+1;
time[i] = rand.nextInt(1000)+2001;
}
System.out.println("complete");
}
@ -73,6 +75,8 @@ public class memoryManagement{
//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]);
//David01.allocate(id[i], size[i], time[i]);
//David02.allocate(id[i], size[i], time[i]);
}
timeEnd = System.currentTimeMillis() - timeStart;
System.out.println("complete");
@ -85,4 +89,4 @@ public class memoryManagement{
//Forcibly close down all threads
System.exit(0);
}
}
}

View File

@ -12,11 +12,12 @@ public class threadedAllocation implements baseAlgorithm{
* our actual operations */
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.start();
// Set up the array of job references
jobArray = new Job[memoryManagement.JOBAMOUNT];
}
int locateBlock(int blockSize){
@ -26,23 +27,25 @@ public class threadedAllocation implements baseAlgorithm{
int memoryLoc = 0;
while (memoryLoc < this.memoryBlock.length)
{
if (memoryBlock[memoryLoc] != 0)
if (memoryBlock[memoryLoc] != 0){
memoryLoc++;
//Block isn't free
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 (free < blockSize && memoryBlock[memoryLoc] == 0)
while (free < blockSize && memoryLoc < this.memoryBlock.length && memoryBlock[memoryLoc] == 0)
{
memoryLoc += 1;
free += 1;
}
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;
}
}
@ -53,7 +56,6 @@ 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
@ -62,27 +64,34 @@ public class threadedAllocation implements baseAlgorithm{
while (beginningLocation == -1)
beginningLocation = locateBlock( jobSize );
//We've got a location, mark it as filled, and start the job.
for (int x = 0; x < jobSize; x++)
{
memoryBlock[beginningLocation + x] = jobID;
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++)
{
memoryBlock[beginningLocation + x] = jobID;
}
}
Job newJob = new Job(jobLength, jobID, jobSize, beginningLocation, deallocateMethod, this);
jobArray[jobID - 1] = newJob;
jobArray[jobID] = newJob;
newJob.start();
} 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){
/* 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.
for (int x = 0; x < jobSize; x++)
{
memoryBlock[beginningLocation + x] = 0;
synchronized(memoryBlock){
for (int x = 0; x < jobSize; x++)
{
memoryBlock[beginningLocation + x] = 0;
}
}
}

View File

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