Process Scheduling with Min Heap
Oct 28, 2013 at 12:30am UTC
I want to implement a shortest job first routine using CC++. Priority of Jobs are based on their processing time. I kinda want to implement using a few categories of jobs that will be entering the priority queue.
1. Jobs are processed using a binary (min) heap. There are three types of jobs.
Type 1 is when jobs come in between every 4-6 seconds, with processing times between 4-6.
Type 2 job comes in between 8-12 seconds, with processing times between 8-12.
Type 3 job comes in between 24-26 seconds, with processing times between 14-16.
So far, I have written the binary heap functionality, but It doesnt really work all that great. I get thrown into infinite loops somewhere, Im unsure of how to solve it. I've narrowed it down to the remove() function, still not sure what to fix though. Any thoughts or help would be appreciated.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
int timecounting = 500;
struct process{
int atime;
int ptime;
int type;
};
class pque{
private :
int count;
public :
process pheap[700];
process type1[700];
process type2[700];
process type3[700];
process type4[700];
pque(){
count = 0;
}
void swap(int a, int b){
process tempa = pheap[a];
process tempb = pheap[b];
pheap[b] = tempa;
pheap[a] = tempb;
}
void add(process c){
int current;
count++;
pheap[count] = c;
if (count > 0){
current = count;
while (pheap[current/2].ptime > pheap[current].ptime){
swap(current/2, current);
current = current/2;
}
}
}
void remove(){
if (count!=0)
{
int n = 1;
pheap[1] = pheap[count]; //takes last process in heap, and puts it at the root
pheap[count].atime = -858993461;
pheap[count].ptime = -858993461;
pheap[count].type = -858993461;
count--;
if (count>1) // if count =1, no swapping necessary
{
int leftchild = 2*n;
int rightchild = 2*n + 1;
while (leftchild < count || rightchild < count) // does left or right child exist
{
if (rightchild >= count)
{
swap(leftchild, n);
n = leftchild;
leftchild = 2*n;
rightchild = 2*n+1;
}
else if (pheap[leftchild].ptime < pheap[rightchild].ptime)
{
if (pheap[leftchild].ptime < pheap[n].ptime) // if smallest ptime is left child
{
swap(leftchild, n);
n = leftchild;
leftchild = 2*n;
rightchild = 2*n + 1;
}
}
else if (pheap[rightchild].ptime <= pheap[n].ptime ) // if smallest ptime is right child
{
if (pheap[rightchild].ptime < pheap[n].ptime && rightchild <= count)
{
swap(rightchild, n);
n = rightchild;
leftchild = 2*n;
rightchild = 2*n + 1;
}
}
if (pheap[rightchild].ptime == pheap[n].ptime || pheap[leftchild].ptime == pheap[n].ptime )
{
break ;
}
}
}
}
}
void spawn1(){
process p;
process p1;
p1.atime = 0;
int i = 0;
//srand(time(NULL));
while (i < timecounting)
{
p.atime = rand()%3 + 4 + p1.atime;
p.ptime = rand()%5 + 1;
p1.atime = p.atime;
p.type = 1;
type1[i+1] = p;
i++;
}
}
void spawn2(){
process p;
process p1;
p1.atime = 0;
//srand(time(NULL));
int i = 0;
while (i < timecounting)
{
p.atime = rand()%3 + 9 + p1.atime;
p.ptime = rand()%5 + 6;
p1.atime = p.atime;
p.type = 2;
type2[i+1] = p;
i++;
}
}
void spawn3(){
process p;
process p1;
p1.atime = 0;
//srand(time(NULL));
int i = 0;
while (i < timecounting)
{
p.atime = rand()%3 + 25 + p1.atime;
p.ptime = rand()%5 + 11;
p1.atime = p.atime;
p.type = 3;
type3[i+1] = p;
i++;
}
}
void spawn4(){
process p;
process p1;
p1.atime = 0;
//srand(time(NULL));
int i = 0;
while (i < timecounting)
{
p.atime = rand()%6 + 30 + p1.atime;
p.ptime = rand()%5 + 8;
p1.atime = p.atime;
p.type = 4;
type4[i+1] = p;
i++;
}
}
void processor()
{
process p;
process p1;
p1.atime = 0;
int n = 1;
int n1 = 1;
int n2 = 1;
for (int i = 0; i<timecounting;i++)
{
// throw all things into the prioirty queue at their arrival times
if (type1[n].atime <= i)
{
add(type1[n]);
n++;
}
if (type2[n1].atime <= i)
{
add(type2[n1]);
n1++;
}
if (type3[n2].atime <= i)
{
add(type3[n2]);
n2++;
}
// processor
if (pheap[1].atime <= i && pheap[1].atime >0)
{
while (pheap[1].ptime > 0 && i<timecounting)
{
pheap[1].ptime--;
i++;
if (pheap[1].ptime == 0)
{
i--;
continue ;
}
}
}
if (pheap[1].ptime == 0)
{
remove();
}
}
}
};
Topic archived. No new replies allowed.