2012-11-06 18:43:43 -05:00
|
|
|
/* David P. Turnbull
|
|
|
|
ITCS3146
|
|
|
|
group project
|
|
|
|
this class sets up a First Fit memory scheme
|
|
|
|
*/
|
|
|
|
|
2012-11-16 13:01:41 -05:00
|
|
|
import java.lang.reflect.*;
|
|
|
|
|
2012-11-06 18:43:43 -05:00
|
|
|
//this section sets up the Car class
|
2012-11-09 20:16:46 -05:00
|
|
|
class FirstFit implements baseAlgorithm
|
2012-11-06 18:43:43 -05:00
|
|
|
{
|
|
|
|
|
|
|
|
//this section sets up the private elements of the class
|
|
|
|
private int jobId,
|
|
|
|
jobSize,
|
2012-11-09 20:16:46 -05:00
|
|
|
jobTime,
|
2012-11-06 18:43:43 -05:00
|
|
|
startLoc,
|
|
|
|
endLoc,
|
|
|
|
blkSize,
|
2012-11-16 13:01:41 -05:00
|
|
|
memSize = memoryManagement.MEMORYSIZE,
|
2012-11-06 18:43:43 -05:00
|
|
|
active,
|
|
|
|
noJobs=0,
|
|
|
|
s1=0,
|
|
|
|
compMemTest=0,
|
2012-11-17 00:33:09 -05:00
|
|
|
jobLoaded=0,
|
2012-11-06 18:43:43 -05:00
|
|
|
tableEntries=1;
|
|
|
|
private int[] tempVal = new int[6];
|
|
|
|
private int[][] memTable = new int[memSize+2][6];
|
2012-11-09 20:16:46 -05:00
|
|
|
private int[] memory = new int[memSize];
|
2012-11-17 00:33:09 -05:00
|
|
|
private Job[] jobArray = new Job[memoryManagement.JOBAMOUNT+10];
|
2012-11-06 19:14:53 -05:00
|
|
|
|
2012-11-06 18:43:43 -05:00
|
|
|
//this is a no argument constructor
|
2012-11-16 13:01:41 -05:00
|
|
|
public FirstFit()
|
2012-11-06 18:43:43 -05:00
|
|
|
{
|
|
|
|
memTable[0][0]=0; //job number
|
|
|
|
memTable[0][1]=0; //job size
|
|
|
|
memTable[0][2]=0; //start location in memory
|
|
|
|
memTable[0][3]=memSize-1; //end location in memory
|
|
|
|
memTable[0][4]=memSize; //mem blk size size
|
|
|
|
memTable[0][5]=-1; //status, 0=not active, 1=active, -1=special
|
|
|
|
}
|
|
|
|
|
2012-11-09 20:16:46 -05:00
|
|
|
|
2012-11-09 20:30:52 -05:00
|
|
|
//this method sets the job up
|
2012-11-09 20:16:46 -05:00
|
|
|
public void allocate(int ID, int size, int jTime)
|
2012-11-06 18:43:43 -05:00
|
|
|
{
|
|
|
|
jobId = ID;
|
|
|
|
jobSize = size;
|
2012-11-09 20:16:46 -05:00
|
|
|
jobTime = jTime;
|
2012-11-06 18:43:43 -05:00
|
|
|
noJobs++;
|
|
|
|
s1=0;
|
|
|
|
|
2012-11-16 13:01:41 -05:00
|
|
|
//Bradlee's code ***********************************************************************************
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Method deallocateMethod = this.getClass().getMethod("deallocate", new Class[]{int.class, int.class});
|
|
|
|
|
|
|
|
|
2012-11-06 18:43:43 -05:00
|
|
|
//checks to see if the job will fit in memory
|
|
|
|
if(jobSize>memSize)
|
|
|
|
{
|
|
|
|
System.out.println("\n\n*********************************************************"+
|
2012-11-17 05:07:03 -05:00
|
|
|
" THIS JOB IS TOO LARGE TO FIT INTO MEMORY"+
|
2012-11-06 18:43:43 -05:00
|
|
|
"*********************************************************");
|
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2012-11-17 00:33:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//this will loop until job is loaded into memory
|
|
|
|
do
|
|
|
|
{
|
|
|
|
|
2012-11-06 18:43:43 -05:00
|
|
|
//this section looks for a place to put the new job
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(memTable[s1][5]==-1 && memTable[s1][4]>=jobSize && memTable[s1][3]==memSize-1)
|
|
|
|
{
|
|
|
|
//runs only for the first job
|
|
|
|
if(noJobs==1)
|
|
|
|
{
|
|
|
|
memTable[s1][0] = jobId;
|
|
|
|
memTable[s1][1] = jobSize;
|
|
|
|
memTable[s1][2] = 0;
|
|
|
|
memTable[s1][3] = jobSize-1;
|
|
|
|
memTable[s1][4] = memTable[0][3]-memTable[0][2]+1;
|
|
|
|
memTable[s1][5] = 1;
|
2012-11-16 13:01:41 -05:00
|
|
|
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
|
2012-11-09 20:16:46 -05:00
|
|
|
fillMemory(jobId, jobSize, memTable[s1][2]);
|
2012-11-17 00:33:09 -05:00
|
|
|
jobArray[jobId - 1] = newJob;
|
2012-11-16 13:01:41 -05:00
|
|
|
newJob.start();
|
2012-11-06 18:43:43 -05:00
|
|
|
memTable[s1+1][0] = 0;
|
|
|
|
memTable[s1+1][1] = 0;
|
|
|
|
memTable[s1+1][2] = memTable[s1][3]+1;
|
|
|
|
memTable[s1+1][3] = memSize-1;
|
|
|
|
memTable[s1+1][4] = memSize-memTable[s1+1][2];
|
|
|
|
memTable[s1+1][5] = -1;
|
|
|
|
tableEntries++;
|
2012-11-17 00:33:09 -05:00
|
|
|
jobLoaded=1;
|
|
|
|
System.out.println("add job "+jobId+toString());
|
2012-11-06 18:43:43 -05:00
|
|
|
s1=memSize*2;
|
|
|
|
}
|
|
|
|
//runs after the first job and if the only available slot is at the end of memory
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memTable[s1][0] = jobId;
|
|
|
|
memTable[s1][1] = jobSize;
|
|
|
|
memTable[s1][2] = memTable[s1-1][3]+1;
|
|
|
|
memTable[s1][3] = jobSize+memTable[s1][2]-1;
|
|
|
|
memTable[s1][4] = memTable[s1][3]-memTable[s1][2]+1;
|
|
|
|
memTable[s1][5] = 1;
|
2012-11-16 13:01:41 -05:00
|
|
|
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
|
2012-11-09 20:16:46 -05:00
|
|
|
fillMemory(jobId, jobSize, memTable[s1][2]);
|
2012-11-17 00:33:09 -05:00
|
|
|
jobArray[jobId - 1] = newJob;
|
2012-11-16 13:01:41 -05:00
|
|
|
newJob.start();
|
2012-11-06 18:43:43 -05:00
|
|
|
memTable[s1+1][0] = 0;
|
|
|
|
memTable[s1+1][1] = 0;
|
|
|
|
memTable[s1+1][2] = memTable[s1][3]+1;
|
|
|
|
memTable[s1+1][3] = memSize-1;
|
|
|
|
memTable[s1+1][4] = memSize-memTable[s1+1][2];
|
|
|
|
memTable[s1+1][5] = -1;
|
|
|
|
tableEntries++;
|
2012-11-17 00:33:09 -05:00
|
|
|
jobLoaded=1;
|
|
|
|
System.out.println("add job "+jobId+toString());
|
2012-11-06 18:43:43 -05:00
|
|
|
s1=memSize*2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//checks for first available free block that has been deallocated
|
|
|
|
else if(memTable[s1][4]>=jobSize && memTable[s1][5]==0)
|
|
|
|
{
|
|
|
|
memTable[s1][0] = jobId;
|
|
|
|
memTable[s1][1] = jobSize;
|
|
|
|
memTable[s1][5] = 1;
|
2012-11-16 13:01:41 -05:00
|
|
|
Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this);
|
2012-11-09 20:16:46 -05:00
|
|
|
fillMemory(jobId, jobSize, memTable[s1][2]);
|
2012-11-17 00:33:09 -05:00
|
|
|
jobArray[jobId - 1] = newJob;
|
2012-11-16 13:01:41 -05:00
|
|
|
newJob.start();
|
2012-11-17 00:33:09 -05:00
|
|
|
jobLoaded=1;
|
|
|
|
System.out.println("add job "+jobId+toString());
|
2012-11-06 18:43:43 -05:00
|
|
|
s1=memSize*2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
}while(s1<tableEntries);
|
2012-11-09 20:30:52 -05:00
|
|
|
|
2012-11-17 00:33:09 -05:00
|
|
|
//if job will not fit this section will compress memory
|
2012-11-09 20:30:52 -05:00
|
|
|
if(s1==tableEntries)
|
2012-11-06 18:43:43 -05:00
|
|
|
{
|
|
|
|
noJobs=noJobs-1;
|
|
|
|
compMem();
|
|
|
|
}
|
2012-11-17 00:33:09 -05:00
|
|
|
|
|
|
|
}while(jobLoaded==0);
|
|
|
|
s1=0;
|
|
|
|
jobLoaded=0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-16 13:01:41 -05:00
|
|
|
} catch (Exception e)
|
|
|
|
{
|
|
|
|
System.out.println("Could not allocate job with ID " + jobId);
|
|
|
|
}
|
2012-11-06 18:43:43 -05:00
|
|
|
}
|
|
|
|
|
2012-11-16 13:01:41 -05:00
|
|
|
|
|
|
|
|
2012-11-09 20:16:46 -05:00
|
|
|
|
2012-11-16 13:01:41 -05:00
|
|
|
|
|
|
|
|
2012-11-09 20:16:46 -05:00
|
|
|
//this method removes a job it does not check to see if the job exisits
|
2012-11-17 00:33:09 -05:00
|
|
|
public void deallocate(int jSize, int beginningLocation)
|
2012-11-09 20:16:46 -05:00
|
|
|
{
|
2012-11-17 00:33:09 -05:00
|
|
|
jobSize = jSize;
|
2012-11-09 20:16:46 -05:00
|
|
|
startLoc = beginningLocation;
|
|
|
|
s1=0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if(memTable[s1][2] == startLoc)
|
2012-11-06 18:43:43 -05:00
|
|
|
{
|
2012-11-17 00:33:09 -05:00
|
|
|
|
|
|
|
System.out.println(startLoc+" removed job "+memTable[s1][0]);
|
2012-11-06 18:43:43 -05:00
|
|
|
memTable[s1][0] = 0;
|
|
|
|
memTable[s1][1] = 0;
|
|
|
|
memTable[s1][5] = 0;
|
2012-11-17 00:33:09 -05:00
|
|
|
System.out.println(memTable[s1][0]+" "+memTable[s1][1]+" "+memTable[s1][5]);
|
|
|
|
System.out.println(toString());
|
2012-11-06 18:43:43 -05:00
|
|
|
noJobs--;
|
2012-11-17 00:33:09 -05:00
|
|
|
s1=memSize*2;
|
2012-11-06 18:43:43 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
s1++;
|
|
|
|
}
|
2012-11-09 20:16:46 -05:00
|
|
|
}while (s1<tableEntries);
|
2012-11-06 18:43:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//this method compacts the memory
|
|
|
|
public void compMem()
|
|
|
|
{
|
2012-11-16 13:01:41 -05:00
|
|
|
//System.out.println("Compacting Memory");
|
2012-11-06 18:43:43 -05:00
|
|
|
compMemTest=tableEntries;
|
|
|
|
for(int c=0; c<=compMemTest; c++)
|
|
|
|
{
|
|
|
|
//this section checks to see if two unused blks are next to each other and then
|
|
|
|
//comdines them
|
|
|
|
if(memTable[c][5]==0 && memTable[c+1][5]==0)
|
|
|
|
{
|
|
|
|
tempVal[0] = memTable[c+1][0];
|
|
|
|
tempVal[1] = memTable[c+1][1];
|
|
|
|
tempVal[2] = memTable[c+1][2];
|
|
|
|
tempVal[3] = memTable[c+1][3];
|
|
|
|
tempVal[4] = memTable[c+1][4];
|
|
|
|
tempVal[5] = memTable[c+1][5];
|
|
|
|
memTable[c+1][0]=-1;
|
|
|
|
memTable[c+1][1]=-1;
|
|
|
|
memTable[c+1][2]=-1;
|
|
|
|
memTable[c+1][3]=-1;
|
|
|
|
memTable[c+1][4]=-1;
|
|
|
|
memTable[c+1][5]=-1;
|
|
|
|
memTable[c][0]=0;
|
|
|
|
memTable[c][1]=0;
|
|
|
|
memTable[c][3]=tempVal[3];
|
|
|
|
memTable[c][4]=memTable[c][4]+tempVal[4];
|
|
|
|
memTable[c][5]=0;
|
|
|
|
//this loop shifts the remaining jobs up
|
|
|
|
for(int srt=c+1; srt<tableEntries; srt++)
|
|
|
|
{
|
|
|
|
memTable[srt][0]=memTable[srt+1][0];
|
|
|
|
memTable[srt][1]=memTable[srt+1][1];
|
|
|
|
memTable[srt][2]=memTable[srt+1][2];
|
|
|
|
memTable[srt][3]=memTable[srt+1][3];
|
|
|
|
memTable[srt][4]=memTable[srt+1][4];
|
|
|
|
memTable[srt][5]=memTable[srt+1][5];
|
|
|
|
}
|
|
|
|
memTable[tableEntries-1][0]=-1;
|
|
|
|
memTable[tableEntries-1][1]=-1;
|
|
|
|
memTable[tableEntries-1][2]=-1;
|
|
|
|
memTable[tableEntries-1][3]=-1;
|
|
|
|
memTable[tableEntries-1][4]=-1;
|
|
|
|
memTable[tableEntries-1][5]=-1;
|
|
|
|
c--;
|
|
|
|
}
|
|
|
|
}
|
2012-11-09 20:16:46 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
s1=0;
|
|
|
|
for(int c1=0; c1<tableEntries; c1++)
|
|
|
|
{
|
|
|
|
if(memTable[c1][0]==-1)
|
|
|
|
{
|
|
|
|
s1++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tableEntries=tableEntries-s1;
|
|
|
|
|
|
|
|
if(memTable[tableEntries-2][5]==0 && memTable[tableEntries-1][5]==-1)
|
|
|
|
{
|
|
|
|
memTable[tableEntries-2][3]=memTable[tableEntries-1][3];
|
|
|
|
memTable[tableEntries-2][4]=memTable[tableEntries-1][4]+memTable[tableEntries-2][4];
|
|
|
|
memTable[tableEntries-2][5]=-1;
|
|
|
|
tableEntries--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//this method fills the memory location with the data
|
|
|
|
private void fillMemory(int job, int size, int start)
|
|
|
|
{
|
|
|
|
jobId=job;
|
|
|
|
jobSize=size;
|
|
|
|
startLoc=start;
|
|
|
|
|
2012-11-17 11:37:33 -05:00
|
|
|
synchronized(memory){
|
|
|
|
for(int fillCount=startLoc; fillCount<jobSize+startLoc; fillCount++)
|
|
|
|
{
|
|
|
|
memory[fillCount]=jobId;
|
|
|
|
}
|
2012-11-09 20:16:46 -05:00
|
|
|
}
|
2012-11-06 18:43:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
//this method returns a String of all the elements stored in the object
|
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
String str;
|
|
|
|
str = ("\n\nJob ID\tJob Size\tStart Loc\tEnd Loc\tMem Blk Size\tStatus");
|
|
|
|
|
|
|
|
for(int cnt=0; cnt<tableEntries; cnt++)
|
|
|
|
{
|
|
|
|
str = (str+"\n"+memTable[cnt][0]+"\t"+memTable[cnt][1]+"\t\t"+memTable[cnt][2]+"\t\t"+
|
|
|
|
memTable[cnt][3]+"\t\t"+memTable[cnt][4]+"\t"+memTable[cnt][5]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|