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++; } }