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 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
| #include <stdio.h> #include <stdlib.h>
#define MAXJOB 50
typedef struct node { int number; int reach_time; int need_time; int privilege; float excellent; int start_time; int wait_time; int visited; bool isreached; } job;
job jobs[MAXJOB];
int quantity;
void initial_jobs() { int i; for (i = 0; i < MAXJOB; i++) { jobs[i].number = 0; jobs[i].reach_time = 0; jobs[i].privilege = 0; jobs[i].excellent = 0; jobs[i].start_time = 0; jobs[i].wait_time = 0; jobs[i].visited = 0; jobs[i].isreached = false; } quantity = 0; }
void reset_jinfo() { int i; for (i = 0; i < MAXJOB; i++) { jobs[i].start_time = 0; jobs[i].wait_time = 0; jobs[i].visited = 0; } }
int findminjob(job jobs[], int current_time) { int mincost = 1000; int minloc = -1; int i; for (i = 0; i < quantity; i++) { if (jobs[i].reach_time <= current_time && jobs[i].visited == 0) { if (jobs[i].need_time < mincost) { mincost = jobs[i].need_time; minloc = i; } } } if (minloc == -1) { int early_time = 10000; for (i = 0; i < quantity; i++) { if (jobs[i].reach_time < early_time && jobs[i].visited != 1) { early_time = jobs[i].reach_time; mincost = jobs[i].need_time; minloc = i; } else if (jobs[i].reach_time == early_time && jobs[i].need_time < mincost && jobs[i].visited != 1) { mincost = jobs[i].need_time; minloc = i; } } } return minloc; }
int findrearlyjob(job jobs[], int count) { int rearlyloc = -1; int rearlyjob = -1; int i; for (i = 0; i < count; i++) { if (rearlyloc == -1) { if (jobs[i].visited == 0) { rearlyloc = i; rearlyjob = jobs[i].reach_time; } } else if (rearlyjob > jobs[i].reach_time && jobs[i].visited == 0) { rearlyjob = jobs[i].reach_time; rearlyloc = i; } } return rearlyloc; }
void readJobdata() { FILE *fp; char fname[20]; int i; printf("please input job data file name\n"); scanf("%s", fname); if ((fp = fopen(fname, "r")) == NULL) { printf("error, open file failed, please check filename:\n"); } else { while (!feof(fp)) { int num; if (fscanf(fp, "%d %d %d %d", &jobs[quantity].number, &jobs[quantity].reach_time, &jobs[quantity].need_time, &jobs[quantity].privilege)) { quantity++; }
} quantity--; printf("output the origin job data\n"); printf("---------------------------------------------------------------------\n"); printf("\tjobID\treachtime\tneedtime\tprivilege\n"); for (i = 0; i < quantity; i++) { printf("\t%-8d\t%-8d\t%-8d\t%-8d\n", jobs[i].number, jobs[i].reach_time, jobs[i].need_time, jobs[i].privilege); } } }
void FCFS() { int i; int current_time = 0; int loc; int total_waitime = 0; int total_roundtime = 0; loc = findrearlyjob(jobs, quantity); printf("\n\nFCFS算法作业流\n"); printf("------------------------------------------------------------------------\n"); printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n"); current_time = jobs[loc].reach_time; for (i = 0; i < quantity; i++) { if (jobs[loc].reach_time > current_time) { jobs[loc].start_time = jobs[loc].reach_time; current_time = jobs[loc].reach_time; } else { jobs[loc].start_time = current_time; } jobs[loc].wait_time = current_time - jobs[loc].reach_time; printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n", jobs[loc].number, jobs[loc].reach_time, jobs[loc].start_time, jobs[loc].wait_time, jobs[loc].wait_time + jobs[loc].need_time); jobs[loc].visited = 1; current_time += jobs[loc].need_time; total_waitime += jobs[loc].wait_time; total_roundtime = total_roundtime + jobs[loc].wait_time + jobs[loc].need_time; loc = findrearlyjob(jobs, quantity); } printf("总等待时间:%-8d 总周转时间:%-8d\n", total_waitime, total_roundtime); printf("平均等待时间: %4.2f 平均周转时间: %4.2f\n", (float) total_waitime / (quantity), (float) total_roundtime / (quantity)); }
void SFJschdulejob(job jobs[], int count) { int i; int current_time = 0; int total_waitime = 0; int total_roundtime = 0; int loc; int earlyest = findrearlyjob(jobs, quantity); loc = findminjob(jobs, jobs[earlyest].reach_time); printf("\n\nSFJ算法作业流\n"); printf("------------------------------------------------------------------------\n"); printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n"); for (i = 0; i < quantity; i++) { if (jobs[loc].reach_time > current_time) { jobs[loc].start_time = jobs[loc].reach_time; current_time = jobs[loc].reach_time + jobs[loc].need_time; jobs[loc].wait_time = 0; } else { jobs[loc].start_time = current_time; jobs[loc].wait_time = current_time - jobs[loc].reach_time; current_time += jobs[loc].need_time; }
printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n", jobs[loc].number, jobs[loc].reach_time, jobs[loc].start_time, jobs[loc].wait_time, jobs[loc].wait_time + jobs[loc].need_time); total_waitime += jobs[loc].wait_time; total_roundtime += jobs[loc].wait_time + jobs[loc].need_time; jobs[loc].visited = 1; loc = findminjob(jobs, current_time); } printf("总等待时间:%-8d 总周转时间:%-8d\n", total_waitime, total_roundtime); printf("平均等待时间: %4.2f 平均周转时间: %4.2f\n", (float) total_waitime / (quantity), (float) total_roundtime / (quantity)); }
int updatePrivilege(job jobs[], int current_time) { int next = -1; int max_privilege = -1; int i; while (max_privilege == -1) {
for (i = 0; i < quantity && jobs[i].reach_time <= current_time; i++) { if (jobs[i].visited != 1) { jobs[i].wait_time = current_time - jobs[i].reach_time; jobs[i].excellent = (double) jobs[i].wait_time / (double) jobs[i].need_time; if (jobs[i].excellent > max_privilege) { max_privilege = jobs[i].excellent; next = i; } } } if (max_privilege == -1) { int early_time = 10000, i; for (i = 0; i < quantity; i++) { if (jobs[i].reach_time < early_time && jobs[i].reach_time > current_time && jobs[i].visited != 1) { early_time = jobs[i].reach_time; next = i; jobs[i].excellent = (double) jobs[i].wait_time / (double) jobs[i].need_time; max_privilege = jobs[i].excellent; } } } }
return next; }
void HRRFschdulejob() { int i, j; for (i = 0; i < quantity - 1; i++) { for (j = 0; j < quantity - 1 - i; j++) { if (jobs[j].reach_time > jobs[j + 1].reach_time) { job temp = jobs[j]; jobs[j] = jobs[j + 1]; jobs[j + 1] = temp; } } } int current_time = 0; int total_waitime = 0; int total_roundtime = 0; int loc = updatePrivilege(jobs, jobs[0].reach_time); printf("\n\nHRRF算法作业流\n"); printf("------------------------------------------------------------------------\n"); printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n"); for (i = 0; i < quantity; i++) { if (jobs[loc].reach_time > current_time) { jobs[loc].start_time = jobs[loc].reach_time; jobs[loc].wait_time = 0; current_time = jobs[loc].start_time + jobs[loc].need_time; } else { jobs[loc].start_time = current_time; jobs[loc].wait_time = current_time - jobs[loc].reach_time; current_time += jobs[loc].need_time; } printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n", jobs[loc].number, jobs[loc].reach_time, jobs[loc].start_time, jobs[loc].wait_time, jobs[loc].wait_time + jobs[loc].need_time); total_waitime += jobs[loc].wait_time; total_roundtime += jobs[loc].wait_time + jobs[loc].need_time; jobs[loc].visited = 1; if (i != quantity - 1) { loc = updatePrivilege(jobs, current_time); } } printf("总等待时间:%-8d 总周转时间:%-8d\n", total_waitime, total_roundtime); printf("平均等待时间: %4.2f 平均周转时间: %4.2f\n", (float) total_waitime / (quantity), (float) total_roundtime / (quantity)); }
void HPF(job jobs[]) { int i, j; for (i = 0; i < quantity - 1; i++) { for (j = 0; j < quantity - 1 - i; j++) { if (jobs[j].reach_time >= jobs[j + 1].reach_time) { job temp = jobs[j]; jobs[j] = jobs[j + 1]; jobs[j + 1] = temp; } } } for (i = 0; i < quantity - 1; i++) { for (j = 0; j < quantity - 1 - i; j++) { if (jobs[j].reach_time == jobs[j + 1].reach_time && jobs[j].privilege <= jobs[j + 1].privilege) { job temp = jobs[j]; jobs[j] = jobs[j + 1]; jobs[j + 1] = temp; } } } int current_time = 0; int total_waitime = 0; int total_roundtime = 0; int loc = 0; printf("\n\nHRF算法作业流\n"); printf("------------------------------------------------------------------------\n"); printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\n"); for (i = 0; i < quantity; i++) { if (jobs[loc].reach_time > current_time) { jobs[loc].start_time = jobs[loc].reach_time; current_time = jobs[loc].start_time + jobs[loc].need_time; } else { jobs[loc].start_time = current_time; current_time += jobs[loc].need_time; } jobs[loc].wait_time = jobs[loc].start_time - jobs[loc].reach_time; printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n", jobs[loc].number, jobs[loc].reach_time, jobs[loc].start_time, jobs[loc].wait_time, jobs[loc].wait_time + jobs[loc].need_time); total_waitime += jobs[loc].wait_time; total_roundtime += jobs[loc].wait_time + jobs[loc].need_time; jobs[loc].visited = 1; int next = -1; int max_privilege = -1; for (j = 0; j < quantity && current_time >= jobs[j].reach_time; j++) { if (jobs[j].visited != 1 && jobs[j].privilege > max_privilege) { max_privilege = jobs[j].privilege; next = j; } } if (next == -1) { next = loc + 1; } loc = next; } printf("总等待时间:%-8d 总周转时间:%-8d\n", total_waitime, total_roundtime); printf("平均等待时间: %4.2f 平均周转时间: %4.2f\n", (float) total_waitime / (quantity), (float) total_roundtime / (quantity)); }
int main() { initial_jobs(); readJobdata(); FCFS(); initial_jobs(); readJobdata(); SFJschdulejob(jobs, quantity); initial_jobs(); readJobdata(); HRRFschdulejob(); initial_jobs(); readJobdata(); HPF(jobs); system("pause"); return 0; }
|