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
|
#include <stdio.h> #include <stdlib.h> #include <stdbool.h>
#define MEMORY_SIZE 640 #define MAX_PART_NUM (MEMORY_SIZE + 2) #define MAX_REQ_NUM 50 #define FREE_ID -1 #define NO_FIT -1
#define OP_ALLOC 0 #define OP_FREE 1
typedef struct partition_s { int id; int base; int size; } partition_t;
typedef struct request_s { int no; int job; int op; int size; } request_t;
typedef int (*select_policy)(const partition_t parts[], int count, int need);
request_t g_reqs[MAX_REQ_NUM]; int g_req_count = 0;
static void print_request_data() { printf("原始请求数据\r\n"); printf("---------------------------------------------------------------------\r\n"); printf("\t序号\t作业号\t操作\t\t内存大小\r\n"); for (int i = 0; i < g_req_count; i++) { printf("\t%-8d\t%-8d\t%s\t\t%-8d\r\n", g_reqs[i].no, g_reqs[i].job, g_reqs[i].op == OP_ALLOC ? "申请" : "释放", g_reqs[i].size); } }
static bool read_request_data() { char fname[128]; printf("请输入作业数据文件名: "); scanf("%s", fname);
FILE *fp = fopen(fname, "r"); if (fp == NULL) { printf("打开文件 %s 失败\r\n", fname); return false; }
int no, job, op, size; g_req_count = 0; while (g_req_count < MAX_REQ_NUM && fscanf(fp, "%d %d %d %d", &no, &job, &op, &size) == 4) { g_reqs[g_req_count].no = no; g_reqs[g_req_count].job = job; g_reqs[g_req_count].op = op; g_reqs[g_req_count].size = size; g_req_count++; } fclose(fp);
if (g_req_count == 0) { printf("文件中没有有效的请求数据\r\n"); return false; } return true; }
static void show_memory(const partition_t parts[], int count) { printf("\r\n内存分配状况\r\n"); printf("---------------------------------------------------------------------\r\n"); printf("\t分区号\t起始地址\t分区大小\t状态\r\n"); for (int i = 0; i < count; i++) { const partition_t *p = &parts[i]; char id_str[16]; if (p->id == FREE_ID) { snprintf(id_str, sizeof(id_str), "FREE"); } else { snprintf(id_str, sizeof(id_str), "%d", p->id); } printf("\t%-6s\t%-8d\t%-8d\t%s\r\n", id_str, p->base, p->size, p->id == FREE_ID ? "空闲" : "已分配"); } printf("---------------------------------------------------------------------\r\n"); }
static int pick_first_fit(const partition_t parts[], int count, int need) { for (int i = 0; i < count; i++) { if (parts[i].id == FREE_ID && parts[i].size >= need) { return i; } } return NO_FIT; }
static int pick_best_fit(const partition_t parts[], int count, int need) { int best = NO_FIT; for (int i = 0; i < count; i++) { if (parts[i].id != FREE_ID || parts[i].size < need) { continue; } if (best == NO_FIT || parts[i].size < parts[best].size) { best = i; } } return best; }
static bool alloc_memory(partition_t parts[], int *count, int job_id, int need, select_policy pick) { if (job_id == FREE_ID || need <= 0) { return false; }
int loc = pick(parts, *count, need); if (loc == NO_FIT) { return false; }
partition_t *p = &parts[loc]; if (p->size == need) { p->id = job_id; return true; }
for (int i = *count; i > loc + 1; i--) { parts[i] = parts[i - 1]; } parts[loc + 1].id = FREE_ID; parts[loc + 1].base = p->base + need; parts[loc + 1].size = p->size - need; p->id = job_id; p->size = need; (*count)++; return true; }
static bool recycle_memory(partition_t parts[], int *count, int job_id) { int loc = NO_FIT; for (int i = 0; i < *count; i++) { if (parts[i].id == job_id) { loc = i; break; } } if (loc == NO_FIT) { return false; } parts[loc].id = FREE_ID;
if (loc + 1 < *count && parts[loc + 1].id == FREE_ID) { parts[loc].size += parts[loc + 1].size; for (int i = loc + 1; i < *count - 1; i++) { parts[i] = parts[i + 1]; } (*count)--; } if (loc > 0 && parts[loc - 1].id == FREE_ID) { parts[loc - 1].size += parts[loc].size; for (int i = loc; i < *count - 1; i++) { parts[i] = parts[i + 1]; } (*count)--; } return true; }
static void run_simulation(const char *algo_name, select_policy pick) { partition_t parts[MAX_PART_NUM]; int count = 1; parts[0].id = FREE_ID; parts[0].base = 0; parts[0].size = MEMORY_SIZE;
printf("\r\n%s 算法执行过程\r\n", algo_name); printf("---------------------------------------------------------------------\r\n"); printf("\t序号\t作业号\t操作\t\t内存大小\t结果\r\n"); for (int i = 0; i < g_req_count; i++) { const request_t *r = &g_reqs[i]; printf("\t%-8d\t%-8d\t%s\t\t%-8d\t", r->no, r->job, r->op == OP_ALLOC ? "申请" : "释放", r->size);
if (r->op == OP_ALLOC) { if (alloc_memory(parts, &count, r->job, r->size, pick)) { printf("分配成功\r\n"); } else { printf("分配失败\r\n"); } } else { if (recycle_memory(parts, &count, r->job)) { printf("释放成功\r\n"); } else { printf("释放失败\r\n"); } } show_memory(parts, count); } }
int main() { if (!read_request_data()) { return EXIT_FAILURE; } print_request_data();
run_simulation("FF 首次适应", pick_first_fit); run_simulation("BF 最佳适应", pick_best_fit); return EXIT_SUCCESS; }
|