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
|
#include <stdio.h> #include <stdlib.h> #include <stdbool.h>
#define MAX_JOB_NUM 50 #define INVALID -1
typedef struct job_s { bool done; int number; int need_time; int reach_time; int start_time; int wait_time; int turn_time; int privilege; } job_t;
typedef int (*pick_policy)(const job_t jobs[], int count, int current_time);
job_t g_jobs[MAX_JOB_NUM]; int g_job_count = 0;
static void print_input_data() { printf("原始作业数据\r\n"); printf("---------------------------------------------------------------------\r\n"); printf("\tjobID\treachtime\tneedtime\tprivilege\r\n"); for (int i = 0; i < g_job_count; i++) { printf("\t%-8d\t%-8d\t%-8d\t%-8d\r\n", g_jobs[i].number, g_jobs[i].reach_time, g_jobs[i].need_time, g_jobs[i].privilege); } }
static void print_header(const char *algo_name) { printf("\r\n%s 算法作业流\r\n", algo_name); printf("---------------------------------------------------------------------\r\n"); printf("\tjobID\treachtime\tstarttime\twaittime\troundtime\r\n"); }
static void print_job_row(const job_t *job) { printf("\t%-8d\t%-8d\t%-8d\t%-8d\t%-8d\n", job->number, job->reach_time, job->start_time, job->wait_time, job->turn_time); }
static void print_summary(int count, int total_wait, int total_turn) { printf("总等待时间:%-8d 总周转时间:%-8d\r\n", total_wait, total_turn); printf("平均等待时间: %4.2f 平均周转时间: %4.2f\n\n", (float)total_wait / count, (float)total_turn / count); }
static bool read_job_data() { char fname[128]; int number, reach, need, privilege; printf("请输入作业数据文件名: "); scanf("%s", fname);
FILE *fp = fopen(fname, "r"); if (fp == NULL) { printf("打开文件 %s 失败\r\n", fname); return false; }
g_job_count = 0; while (g_job_count < MAX_JOB_NUM && fscanf(fp, "%d %d %d %d", &number, &reach, &need, &privilege) == 4) { g_jobs[g_job_count].number = number; g_jobs[g_job_count].reach_time = reach; g_jobs[g_job_count].need_time = need; g_jobs[g_job_count].privilege = privilege; g_job_count++; } fclose(fp);
if (g_job_count == 0) { printf("文件中没有有效的作业数据\r\n"); return false; } return true; }
static int pick_fcfs(const job_t jobs[], int count, int current_time) { int best = INVALID; for (int i = 0; i < count; i++) { if (jobs[i].done || jobs[i].reach_time > current_time) { continue; } if (best == INVALID || jobs[i].reach_time < jobs[best].reach_time) { best = i; } } return best; }
static int pick_sjf(const job_t jobs[], int count, int current_time) { int best = INVALID; for (int i = 0; i < count; i++) { if (jobs[i].done || jobs[i].reach_time > current_time) { continue; } if (best == INVALID || jobs[i].need_time < jobs[best].need_time) { best = i; } } return best; }
static int pick_hrrf(const job_t jobs[], int count, int current_time) { int best = INVALID; float best_ratio = -1.0f; for (int i = 0; i < count; i++) { if (jobs[i].done || jobs[i].reach_time > current_time) { continue; } float ratio = (float)(current_time - jobs[i].reach_time) / jobs[i].need_time; if (best == INVALID || ratio > best_ratio) { best = i; best_ratio = ratio; } } return best; }
static int pick_hpf(const job_t jobs[], int count, int current_time) { int best = INVALID; for (int i = 0; i < count; i++) { if (jobs[i].done || jobs[i].reach_time > current_time) { continue; } if (best == INVALID || jobs[i].privilege > jobs[best].privilege) { best = i; } } return best; }
static int pick_earliest_arrival(const job_t jobs[], int count) { int best = INVALID; for (int i = 0; i < count; i++) { if (jobs[i].done) { continue; } if (best == INVALID || jobs[i].reach_time < jobs[best].reach_time) { best = i; } } return best; }
static void run_scheduler(const job_t master[], int count, const char *algo_name, pick_policy pick) { job_t work[MAX_JOB_NUM]; for (int i = 0; i < count; i++) { work[i] = master[i]; }
int current = 0; int scheduled = 0; int total_wait = 0; int total_turn = 0;
print_header(algo_name); while (scheduled < count) { int loc = pick(work, count, current);
if (loc == INVALID) { current = work[pick_earliest_arrival(work, count)].reach_time; loc = pick(work, count, current); }
work[loc].start_time = current; work[loc].wait_time = current - work[loc].reach_time; work[loc].turn_time = work[loc].wait_time + work[loc].need_time; print_job_row(&work[loc]);
total_wait += work[loc].wait_time; total_turn += work[loc].turn_time; current += work[loc].need_time; work[loc].done = true; scheduled++; } print_summary(count, total_wait, total_turn); }
int main() { if (!read_job_data()) { return EXIT_FAILURE; } print_input_data();
run_scheduler(g_jobs, g_job_count, "FCFS", pick_fcfs); run_scheduler(g_jobs, g_job_count, "SJF", pick_sjf); run_scheduler(g_jobs, g_job_count, "HRRF", pick_hrrf); run_scheduler(g_jobs, g_job_count, "HPF", pick_hpf); return EXIT_SUCCESS; }
|