itcs3146-project/FirstFit.java

288 lines
7.3 KiB
Java
Raw Normal View History

2012-11-06 18:43:43 -05:00
/* David P. Turnbull
ITCS3146
group project
this class sets up a First Fit memory scheme
*/
//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-09 20:16:46 -05:00
memSize = DemoMemory.memory,
2012-11-06 18:43:43 -05:00
active,
noJobs=0,
s1=0,
chkCompress=0,
compMemTest=0,
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-06 18:43:43 -05:00
//this is a no argument constructor
public FirstFit()
{
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-06 18:43:43 -05:00
//this method sets the job up need to modify the job class to return something
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;
//checks to see if the job will fit in memory
if(jobSize>memSize)
{
System.out.println("\n\n*********************************************************"+
" THIS JOB IS TO LARGE TO FIT INTO MEMORY"+
"*********************************************************");
System.exit(0);
}
//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-09 20:16:46 -05:00
fillMemory(jobId, jobSize, memTable[s1][2]);
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;
chkCompress=0;
tableEntries++;
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-09 20:16:46 -05:00
System.out.println(jobId+" "+jobSize+" "+memTable[s1][2]);
fillMemory(jobId, jobSize, memTable[s1][2]);
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++;
chkCompress=0;
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-09 20:16:46 -05:00
fillMemory(jobId, jobSize, memTable[s1][2]);
2012-11-06 18:43:43 -05:00
chkCompress=0;
s1=memSize*2;
}
else
{
s1++;
}
}while(s1<tableEntries);
//this section runs if the job will not fit after memory compaction
if(chkCompress==1)
{
chkCompress=0;
System.out.println("put job back in queue");
//need to return that this job needs to be returned to the queue
System.exit(0);
}
//if job will not fit this section will compress memory and try placing the job again
if(s1==tableEntries && chkCompress==0)
{
noJobs=noJobs-1;
compMem();
chkCompress++;
2012-11-09 20:16:46 -05:00
allocate(ID, size, jobTime);
2012-11-06 18:43:43 -05:00
}
}
2012-11-09 20:16:46 -05:00
//this method is used if you want to deallocate a job by jobId
2012-11-06 18:43:43 -05:00
public void removeJob(int ID)
{
jobId = ID;
s1=0;
do
{
if(memTable[s1][0] == jobId)
2012-11-09 20:16:46 -05:00
{
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)
{
jobId = 0;
jobSize = jobSize;
startLoc = beginningLocation;
s1=0;
do
{
if(memTable[s1][2] == startLoc)
2012-11-06 18:43:43 -05:00
{
memTable[s1][0] = 0;
memTable[s1][1] = 0;
memTable[s1][5] = 0;
2012-11-09 20:16:46 -05:00
s1=memSize*2;
2012-11-06 18:43:43 -05:00
jobId=-1;
noJobs--;
}
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()
{
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;
for(int fillCount=startLoc; fillCount<jobSize+startLoc; fillCount++)
{
memory[fillCount]=jobId;
System.out.println("\n"+fillCount+" "+jobId);
}
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;
}
}