From aa872f238ad4629fa1efce56f8cc6b6d1b365aa1 Mon Sep 17 00:00:00 2001 From: dpturnbull Date: Thu, 22 Nov 2012 23:11:56 -0500 Subject: [PATCH] testing for thread code --- FirstFit.java | 36 +++++++++----- NextFit.java | 134 ++++++++++++++++++++++++++++---------------------- log.txt | 0 3 files changed, 100 insertions(+), 70 deletions(-) create mode 100644 log.txt diff --git a/FirstFit.java b/FirstFit.java index 1b367a0..59ac5c8 100644 --- a/FirstFit.java +++ b/FirstFit.java @@ -44,6 +44,7 @@ class FirstFit implements baseAlgorithm //this method sets the job up public void allocate(int ID, int size, int jTime) { + //synchronized(memTable){ jobId = ID; jobSize = size; jobTime = jTime; @@ -82,6 +83,7 @@ class FirstFit implements baseAlgorithm //runs only for the first job if(noJobs==1) { + synchronized(memTable){ memTable[s1][0] = jobId; memTable[s1][1] = jobSize; memTable[s1][2] = 0; @@ -102,10 +104,12 @@ class FirstFit implements baseAlgorithm 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 else { + synchronized(memTable){ memTable[s1][0] = jobId; memTable[s1][1] = jobSize; memTable[s1][2] = memTable[s1-1][3]+1; @@ -126,11 +130,13 @@ class FirstFit implements baseAlgorithm jobLoaded=1; System.out.println("add job "+jobId+toString()); s1=memSize*2; + } } } //checks for first available free block that has been deallocated else if(memTable[s1][4]>=jobSize && memTable[s1][5]==0) { + synchronized(memTable){ memTable[s1][0] = jobId; memTable[s1][1] = jobSize; memTable[s1][5] = 1; @@ -141,6 +147,7 @@ class FirstFit implements baseAlgorithm jobLoaded=1; System.out.println("add job "+jobId+toString()); s1=memSize*2; + } } else { @@ -165,6 +172,7 @@ class FirstFit implements baseAlgorithm { System.out.println("Could not allocate job with ID " + jobId); } + //} } @@ -175,28 +183,32 @@ class FirstFit implements baseAlgorithm //this method removes a job it does not check to see if the job exisits public void deallocate(int jSize, int beginningLocation) { + synchronized(memTable){ + int deallocates1=0; jobSize = jSize; startLoc = beginningLocation; - s1=0; + System.out.println("jSize= "+jobSize+" startLoc= "+startLoc); + //s1=0; do { - if(memTable[s1][2] == startLoc) + if(memTable[deallocates1][2] == startLoc) { - - System.out.println(startLoc+" removed job "+memTable[s1][0]); - memTable[s1][0] = 0; - memTable[s1][1] = 0; - memTable[s1][5] = 0; - System.out.println(memTable[s1][0]+" "+memTable[s1][1]+" "+memTable[s1][5]); + System.out.println(memTable[deallocates1][0]+" "+memTable[deallocates1][1]+" "+memTable[deallocates1][5]); + System.out.println(startLoc+" removed job "+memTable[deallocates1][0]); + memTable[deallocates1][0] = 0; + memTable[deallocates1][1] = 0; + memTable[deallocates1][5] = 0; + System.out.println(memTable[deallocates1][0]+" "+memTable[deallocates1][1]+" "+memTable[deallocates1][5]); System.out.println(toString()); noJobs--; - s1=memSize*2; + deallocates1=memSize*2; } else { - s1++; + deallocates1++; } - }while (s1memSize) { @@ -66,24 +70,34 @@ class NextFit 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 { - if(memTable[currentPosition][5]==-1 && memTable[currentPosition][4]>=jobSize && - memTable[currentPosition][3]==memSize-1) + if(memTable[currentPosition][5]==-1 && memTable[currentPosition][4]>=jobSize && memTable[currentPosition][3]==memSize-1) { //runs only for the first job if(noJobs==1) { + synchronized(memTable){ memTable[currentPosition][0] = jobId; memTable[currentPosition][1] = jobSize; memTable[currentPosition][2] = 0; 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]); + 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[currentPosition+1][0] = 0; memTable[currentPosition+1][1] = 0; @@ -94,19 +108,24 @@ class NextFit implements baseAlgorithm currentPosition++; positionToCompress=currentPosition; tableEntries++; + 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 else { + synchronized(memTable){ memTable[currentPosition][0] = jobId; memTable[currentPosition][1] = jobSize; memTable[currentPosition][2] = memTable[currentPosition-1][3]+1; 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]); + 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[currentPosition+1][0] = 0; memTable[currentPosition+1][1] = 0; @@ -116,24 +135,32 @@ class NextFit implements baseAlgorithm memTable[currentPosition+1][5] = -1; tableEntries++; currentPosition++; + jobLoaded=1; positionToCompress=currentPosition; + System.out.println("add job "+jobId+toString()); s1=memSize*2; + } } } //checks for first available free block that has been deallocated else if(memTable[currentPosition][4]>=jobSize && memTable[currentPosition][5]==0) { + synchronized(memTable){ 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]); + Job newJob = new Job(jobTime, jobId, jobSize, memTable[s1][2], deallocateMethod, this); + fillMemory(jobId, jobSize, memTable[s1][2]); + jobArray[jobId - 1] = newJob; newJob.start(); currentPosition++; + jobLoaded=1; positionToCompress=currentPosition; + System.out.println("add job "+jobId+toString()); s1=memSize*2; + } } - else if(currentPosition==tableEntries-1) + else if (currentPosition==tableEntries-1) { currentPosition=0; s1++; @@ -143,83 +170,71 @@ class NextFit implements baseAlgorithm s1++; currentPosition++; } - }while(s1