From 25fbca314c476a3d290bf26a3c49abb573733cd5 Mon Sep 17 00:00:00 2001 From: dpturnbull Date: Fri, 16 Nov 2012 13:01:41 -0500 Subject: [PATCH 1/3] Added code to run jobs --- FirstFit.java | 32 +++++++++++++++++++++++++++++--- NextFit.java | 24 +++++++++++++++++++++++- memoryManagement.java | 12 ++++++++---- 3 files changed, 60 insertions(+), 8 deletions(-) diff --git a/FirstFit.java b/FirstFit.java index b36c1e7..e0a3c5b 100644 --- a/FirstFit.java +++ b/FirstFit.java @@ -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,7 +17,7 @@ class FirstFit implements baseAlgorithm startLoc, endLoc, blkSize, - memSize, + memSize = memoryManagement.MEMORYSIZE, active, noJobs=0, s1=0, @@ -26,9 +28,8 @@ class FirstFit implements baseAlgorithm private int[] memory = new int[memSize]; //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 +48,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) { @@ -70,7 +77,9 @@ 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]); + newJob.start(); memTable[s1+1][0] = 0; memTable[s1+1][1] = 0; memTable[s1+1][2] = memTable[s1][3]+1; @@ -78,6 +87,7 @@ class FirstFit implements baseAlgorithm memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][5] = -1; tableEntries++; + System.out.println(toString()); s1=memSize*2; } //runs after the first job and if the only available slot is at the end of memory @@ -89,7 +99,9 @@ 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]); + newJob.start(); memTable[s1+1][0] = 0; memTable[s1+1][1] = 0; memTable[s1+1][2] = memTable[s1][3]+1; @@ -97,6 +109,7 @@ class FirstFit implements baseAlgorithm memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][5] = -1; tableEntries++; + System.out.println(toString()); s1=memSize*2; } } @@ -106,7 +119,10 @@ 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]); + newJob.start(); + System.out.println(toString()); s1=memSize*2; } else @@ -123,8 +139,14 @@ class FirstFit implements baseAlgorithm compMem(); 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) { @@ -147,6 +169,8 @@ class FirstFit implements baseAlgorithm 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) @@ -165,6 +189,7 @@ class FirstFit implements baseAlgorithm s1=memSize*2; jobId=-1; noJobs--; + System.out.println("removed job "+memTable[s1][0]); } else { @@ -178,6 +203,7 @@ class FirstFit implements baseAlgorithm //this method compacts the memory public void compMem() { + //System.out.println("Compacting Memory"); compMemTest=tableEntries; for(int c=0; c<=compMemTest; c++) { diff --git a/NextFit.java b/NextFit.java index 43d2a9b..8bfbad8 100644 --- a/NextFit.java +++ b/NextFit.java @@ -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++) { diff --git a/memoryManagement.java b/memoryManagement.java index f2bc53f..3a52461 100644 --- a/memoryManagement.java +++ b/memoryManagement.java @@ -9,7 +9,7 @@ import java.util.StringTokenizer; public class memoryManagement{ - static final int JOBAMOUNT = 1000; + static final int JOBAMOUNT = 30; static final int MEMORYSIZE = 10000; public static void main(String args[])throws Exception{ @@ -31,7 +31,9 @@ public class memoryManagement{ //******Add your algorithm class here******// //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 do{ @@ -72,7 +74,9 @@ public class memoryManagement{ timeStart = System.currentTimeMillis(); //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]); + //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); } -} +} From 51aad5ee74db8df3be2c2543d7d5a97c9308bf8e Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Fri, 16 Nov 2012 22:13:29 -0500 Subject: [PATCH 2/3] Finish bugfixing for threaded allocation algorithm. Threaded allocation is now complete. --- jobThread.java | 1 - memoryManagement.java | 16 +++++++------- threadedAllocation.java | 39 +++++++++++++++++++++------------- threadedAllocationGarbage.java | 36 ++++++++++++++++++------------- 4 files changed, 53 insertions(+), 39 deletions(-) diff --git a/jobThread.java b/jobThread.java index 64d540d..1a103a6 100644 --- a/jobThread.java +++ b/jobThread.java @@ -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; } diff --git a/memoryManagement.java b/memoryManagement.java index 3a52461..e7f28f8 100644 --- a/memoryManagement.java +++ b/memoryManagement.java @@ -9,8 +9,8 @@ import java.util.StringTokenizer; public class memoryManagement{ - static final int JOBAMOUNT = 30; - static final int MEMORYSIZE = 10000; + static final int JOBAMOUNT = 200; + static final int MEMORYSIZE = 100; public static void main(String args[])throws Exception{ @@ -31,9 +31,9 @@ public class memoryManagement{ //******Add your algorithm class here******// //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(); + //NextFit David02 = new NextFit(); //Gets a file name, else creates five random jobs do{ @@ -50,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"); } @@ -74,9 +74,9 @@ public class memoryManagement{ timeStart = System.currentTimeMillis(); //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]); + 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]); + //David02.allocate(id[i], size[i], time[i]); } timeEnd = System.currentTimeMillis() - timeStart; System.out.println("complete"); diff --git a/threadedAllocation.java b/threadedAllocation.java index e7795ba..ecebeee 100644 --- a/threadedAllocation.java +++ b/threadedAllocation.java @@ -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; + } } } diff --git a/threadedAllocationGarbage.java b/threadedAllocationGarbage.java index a5ca9c8..990bf43 100644 --- a/threadedAllocationGarbage.java +++ b/threadedAllocationGarbage.java @@ -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++; } } From a468512e16e53a09e8d483a2594d55521012d89a Mon Sep 17 00:00:00 2001 From: dpturnbull Date: Sat, 17 Nov 2012 00:33:09 -0500 Subject: [PATCH 3/3] Debugging code --- FirstFit.java | 71 +++++++++++++++++++++++++-------------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/FirstFit.java b/FirstFit.java index e0a3c5b..ecff746 100644 --- a/FirstFit.java +++ b/FirstFit.java @@ -22,10 +22,12 @@ class FirstFit implements baseAlgorithm 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() @@ -63,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 { @@ -79,6 +90,7 @@ class FirstFit implements baseAlgorithm 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; @@ -87,7 +99,8 @@ class FirstFit implements baseAlgorithm memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][5] = -1; tableEntries++; - System.out.println(toString()); + 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 @@ -101,6 +114,7 @@ class FirstFit implements baseAlgorithm 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; @@ -109,7 +123,8 @@ class FirstFit implements baseAlgorithm memTable[s1+1][4] = memSize-memTable[s1+1][2]; memTable[s1+1][5] = -1; tableEntries++; - System.out.println(toString()); + jobLoaded=1; + System.out.println("add job "+jobId+toString()); s1=memSize*2; } } @@ -121,8 +136,10 @@ class FirstFit implements baseAlgorithm 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(); - System.out.println(toString()); + jobLoaded=1; + System.out.println("add job "+jobId+toString()); s1=memSize*2; } else @@ -130,15 +147,20 @@ class FirstFit implements baseAlgorithm s1++; } }while(s1