From 68bc6b8ba42c815d1c2a23979bc2376126f15e40 Mon Sep 17 00:00:00 2001 From: dpturnbull Date: Tue, 6 Nov 2012 18:43:43 -0500 Subject: [PATCH] Added First Fit --- FirstFit.java | 222 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 FirstFit.java diff --git a/FirstFit.java b/FirstFit.java new file mode 100644 index 0000000..deb173d --- /dev/null +++ b/FirstFit.java @@ -0,0 +1,222 @@ +/* David P. Turnbull + ITCS3146 + group project + this class sets up a First Fit memory scheme +*/ + +//this section sets up the Car class +class FirstFit +{ + + //this section sets up the private elements of the class + private int jobId, + jobSize, + startLoc, + endLoc, + blkSize, + memSize=1024, + 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]; + + + //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 + } + + //this method sets the job up need to modify the job class to return something + public void addJob(int ID, int size) + { + jobId = ID; + jobSize = size; + 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; + 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; + 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; + chkCompress=0; + s1=memSize*2; + } + else + { + s1++; + } + }while(s1