Browse Source

Actuall fix David's code

Bradlee Speice 6 years ago
parent
commit
365e071558
3 changed files with 24 additions and 13 deletions
  1. 4
    3
      BestFitAlgorithm.java
  2. 19
    10
      WorstFitAlgorithm.java
  3. 1
    0
      memoryManagement.java

+ 4
- 3
BestFitAlgorithm.java View File

@@ -66,6 +66,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
66 66
         }
67 67
         int bSize = 1;
68 68
         int bestIndex = -1;
69
+        
69 70
         if(!blocks.isEmpty())
70 71
         {
71 72
             bSize = blocks.get(0).intValue();
@@ -121,8 +122,8 @@ public class BestFitAlgorithm implements baseAlgorithm{
121 122
            
122 123
             if(bestSizeIndex != -1)
123 124
             {
124
-                System.out.println("The size of the job is: " + jobSize);
125
-                System.out.println("The best size index is: " + bestSizeIndex);
125
+                //System.out.println("The size of the job is: " + jobSize);
126
+                //System.out.println("The best size index is: " + bestSizeIndex);
126 127
                 
127 128
                 synchronized(memoryBlock)
128 129
                 {
@@ -135,7 +136,7 @@ public class BestFitAlgorithm implements baseAlgorithm{
135 136
 
136 137
                 //System.out.println("Successfully allocated! Starting job...");
137 138
                 
138
-                System.out.println("Job time: " + jobTime);
139
+                //System.out.println("Job time: " + jobTime);
139 140
                 
140 141
                 Job newJob = new Job(jobTime, jobID, jobSize, bestSizeIndex, deallocateMethod, this);
141 142
         

+ 19
- 10
WorstFitAlgorithm.java View File

@@ -74,16 +74,16 @@ public class WorstFitAlgorithm implements baseAlgorithm{
74 74
             wSize = blocks.get(0).intValue();
75 75
         }
76 76
         
77
-
77
+       
78 78
         //GET WORST INDEX
79 79
         for(int i = 0; i < blocks.size(); i++)
80 80
         {
81 81
             //Get largest possible free block to allocate to
82
-            if((blocks.get(i).intValue() >= wSize && blocks.get(i).intValue() >= jobSize) || blocks.get(i).intValue() > -1)
82
+            if(blocks.get(i).intValue() >= wSize || blocks.get(i).intValue() > -1)
83 83
             {
84 84
                 worstIndex = indices.get(i).intValue();
85 85
             }
86
-            
86
+
87 87
             //"Worst fit"....same size as job size
88 88
             else if(blocks.get(i).intValue() == jobSize)
89 89
             {
@@ -91,6 +91,13 @@ public class WorstFitAlgorithm implements baseAlgorithm{
91 91
                 //System.out.println("Worst Case");
92 92
                 worstIndex = indices.get(i).intValue();
93 93
             }
94
+            else
95
+            {
96
+                if(!blocks.isEmpty())
97
+                {
98
+                    worstIndex = indices.get(i).intValue();
99
+                }
100
+            }
94 101
         }
95 102
         
96 103
         //System.out.println("bestIndex: " + bestIndex);
@@ -118,15 +125,16 @@ public class WorstFitAlgorithm implements baseAlgorithm{
118 125
                 while(worstSizeIndex == -1)
119 126
                 {
120 127
                     //Compact and try again
121
-                    //System.out.println("Compacting memory...");
128
+                    System.out.println("Compacting memory...");
122 129
                     this.compact();
123 130
                     worstSizeIndex = this.getWorstIndex(jobSize);
124 131
                 }
125 132
             }
126
-            else
133
+            if(worstSizeIndex != -1)
127 134
             {
135
+                 System.out.println("Phew");
128 136
                 //System.out.println("The size of the job is: " + jobSize);
129
-                //System.out.println("The best size index is: " + bestSizeIndex);
137
+                //System.out.println("The worst size index is: " + worstSizeIndex);
130 138
                 synchronized(memoryBlock)
131 139
                 {
132 140
                     for(int i = worstSizeIndex; i < jobSize + worstSizeIndex; i++)
@@ -137,10 +145,10 @@ public class WorstFitAlgorithm implements baseAlgorithm{
137 145
                 }
138 146
                 //System.out.println("Successfully allocated! Starting job...");
139 147
                 
140
-                Job newJob = new Job(jobSize, jobID, jobSize, worstSizeIndex, deallocateMethod, this);
141
-        
148
+                Job newJob = new Job(jobTime, jobID, jobSize, worstSizeIndex, deallocateMethod, this);
149
+        System.out.println("Job started!");
142 150
                 jobArray[jobID] = newJob;
143
-
151
+        System.out.println("The size of the job array is: " + jobArray.length);
144 152
                 newJob.start();
145 153
                 
146 154
                 //System.out.println("Job started!");
@@ -148,7 +156,8 @@ public class WorstFitAlgorithm implements baseAlgorithm{
148 156
         }   
149 157
         catch (Exception e)
150 158
         {
151
-            //System.out.println("Could not allocate job with ID " + jobID);
159
+            e.printStackTrace();
160
+	    System.exit(-1);
152 161
         }
153 162
     }
154 163
     /*

+ 1
- 0
memoryManagement.java View File

@@ -42,6 +42,7 @@ public class memoryManagement{
42 42
 		do{							
43 43
 			System.out.println("Type filename to load jobs from a file or just press enter for random jobs");
44 44
 			read = keyboard.nextLine();
45
+			read = read.trim();
45 46
 			file = new File(read + ".txt");
46 47
 			if(!read.equals("") && !file.exists())
47 48
 				System.out.println("File not found, try again");

Loading…
Cancel
Save