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 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
#include<stdio.h> #include<stdlib.h> #include<ctype.h> #include<sys/time.h> #include<unistd.h> #include<stdlib.h>
#define BUSY 1 #define IDLE 0
#define COLOR_Exist 0 #define COLOR_NotExist_IDLE 1 #define COLOR_NotExist_NoIDLE 2
double hitCount = 0; int pages[100];
typedef struct _Page { int pageID; } Page; typedef struct _PageQueue { Page page; struct _PageQueue *next; } PageQueue; typedef struct _Block { Page *page; long time; int state; } Block; typedef struct _BlockQueue { Block block; struct _BlockQueue *next; } BlockQueue; typedef struct process { PageQueue pages; unsigned int pageLength; } process;
int Time = 0;
long GetSystemUtime() { struct timeval nowit; gettimeofday(&nowit, NULL); return 1000000 * nowit.tv_sec + nowit.tv_usec;; }
BlockQueue *InitializeBlockQueue(unsigned int size) { BlockQueue *block = NULL, *p = NULL, *q = NULL; for (int i = 0; i < size; i++) { p = (BlockQueue *) malloc(sizeof(BlockQueue)); p->block.time = 0; p->block.state = 0; p->block.page = NULL; p->next = NULL; if (block == NULL) block = p; else q->next = p; q = p; } return block; }
int GetBlockQueueSize(BlockQueue *blockQueue) { BlockQueue *presentBlock = blockQueue; int blockQueueSize = 0; while (presentBlock != NULL) { blockQueueSize++; presentBlock = presentBlock->next; } return blockQueueSize; }
void ResetBlockQueue(BlockQueue *blockQueue) { BlockQueue *presentBlock = blockQueue; while (presentBlock != NULL) { presentBlock->block.page = NULL; presentBlock->block.state = IDLE; presentBlock->block.time = 0; presentBlock = presentBlock->next; } }
PageQueue *InitializePageQueue(unsigned int pageLength, int maxPageID) { srand(100); PageQueue *head = NULL, *p = NULL, *q = NULL; for (int i = 0; i < pageLength; i++) { p = (PageQueue *) malloc(sizeof(PageQueue)); p->page.pageID = rand() % (maxPageID + 1); p->next = NULL; printf("%d ", p->page.pageID); if (head == NULL) head = p; else q->next = p; q = p; } printf("\n"); return head; }
PageQueue *InitializePageQueueWithInput(unsigned int pageLength) { PageQueue *head = NULL, *p = NULL, *q = NULL; for (int i = 0; i < pageLength; i++) { p = (PageQueue *) malloc(sizeof(PageQueue)); p->page.pageID = pages[i]; p->next = NULL; printf("%d ", p->page.pageID); if (head == NULL) head = p; else q->next = p; q = p; } printf("\n"); return head; }
void InitializeProcess(process *proc, unsigned int pageSize, unsigned int maxPageID) { printf("进程初始化:\n"); proc->pageLength = pageSize; proc->pages.next = InitializePageQueue(pageSize, maxPageID); }
void InitializeProcessWithInput(process *proc, unsigned int pageSize) { printf("进程初始化:\n"); proc->pageLength = pageSize; proc->pages.next = InitializePageQueueWithInput(pageSize); }
BlockQueue *SearchPage(BlockQueue *blockQueue, Page page) { BlockQueue *p = blockQueue; while (p != NULL) { if (p->block.page != NULL) { if (p->block.page->pageID == page.pageID) return p; } p = p->next; } return NULL; }
BlockQueue *SearchIdleBlock(BlockQueue *blockQueue) { BlockQueue *p = blockQueue; while (p != NULL) { if (p->block.state == IDLE) return p; else p = p->next; } return NULL; }
int GetBlockLable(BlockQueue *blockQueue, BlockQueue *goalBlock) { BlockQueue *p = blockQueue; int count = 1; while (p != goalBlock) { p = p->next; count++; } return count; }
BlockQueue *GetOldestBlock(BlockQueue *blockQueue) { BlockQueue *p = blockQueue, *oldestAddr; if (p == NULL) return p; long oldest = p->block.time; oldestAddr = p; while (p != NULL) { if (p->block.time < oldest) { oldest = p->block.time; oldestAddr = p; } p = p->next; } return oldestAddr; }
BlockQueue *GetLongestWithoutAccess(BlockQueue *blockQueue, PageQueue *currentPage){ BlockQueue *p = blockQueue, *longestAddr; PageQueue *q = currentPage->next; if (p == NULL) return p; longestAddr = p; int max_count = 0; while (p != NULL){ int count = 0; while(q != NULL){ count++; if(p->block.page->pageID == q->page.pageID) break; q = q->next; } if(count > max_count){ max_count = count; longestAddr = p; } q = currentPage->next; p = p->next; } return longestAddr; }
void PrintBlockList(BlockQueue *blockQueue, int pageID, int color) { BlockQueue *presentBlock = blockQueue; for (int i = 0; i < GetBlockQueueSize(blockQueue); i++) { if (presentBlock == NULL) break; printf("页框%d ", i + 1); if (presentBlock->block.state == IDLE) { printf("| |\n"); } else { if (presentBlock->block.page->pageID != pageID) { printf("| %d |\n", presentBlock->block.page->pageID); } else { switch (color) { case COLOR_Exist: printf("| %d | 命中! 主存已存在该页面,位于页框%d\n", pageID, GetBlockLable(blockQueue, presentBlock)); hitCount++; break; case COLOR_NotExist_IDLE: printf("| %d | 缺页! 主存不存在该页面,载入空闲页框%d\n", pageID, GetBlockLable(blockQueue, presentBlock)); break; case COLOR_NotExist_NoIDLE: printf("| %d | 缺页! 主存不存在该页面,替换页框%d\n", pageID, GetBlockLable(blockQueue, presentBlock)); break; default: break; } } } presentBlock = presentBlock->next; } printf("---------------------------------------------------------\n"); }
void FIFO(BlockQueue *blockQueue, process *proc) { PageQueue *currentPage = proc->pages.next; while (currentPage != NULL) { if (SearchPage(blockQueue, currentPage->page) != NULL) { PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_Exist); } else { BlockQueue *idleBlock = SearchIdleBlock(blockQueue); if (idleBlock != NULL) { idleBlock->block.state = BUSY; idleBlock->block.time = Time++; idleBlock->block.page = (Page *) malloc(sizeof(Page)); idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_IDLE); } else { idleBlock = GetOldestBlock(blockQueue); idleBlock->block.time = Time++; idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_NoIDLE); } } currentPage = currentPage->next; } }
void LRU(BlockQueue *blockQueue, process *proc) { PageQueue *currentPage = proc->pages.next; while (currentPage != NULL) { BlockQueue *searchedBlock = SearchPage(blockQueue, currentPage->page); if (searchedBlock != NULL) { searchedBlock->block.time = Time++; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_Exist); } else { BlockQueue *idleBlock = SearchIdleBlock(blockQueue); if (idleBlock != NULL) { idleBlock->block.state = BUSY; idleBlock->block.time = Time++; idleBlock->block.page = (Page *) malloc(sizeof(Page)); idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_IDLE); } else { idleBlock = GetOldestBlock(blockQueue); idleBlock->block.time = Time++; idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_NoIDLE); } } currentPage = currentPage->next; } }
void OPT(BlockQueue *blockQueue, process *proc){ PageQueue *currentPage = proc->pages.next; while (currentPage != NULL) { if (SearchPage(blockQueue, currentPage->page) != NULL) { PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_Exist); } else { BlockQueue *idleBlock = SearchIdleBlock(blockQueue); if (idleBlock != NULL) { idleBlock->block.state = BUSY; idleBlock->block.time = Time++; idleBlock->block.page = (Page *) malloc(sizeof(Page)); idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_IDLE); } else { idleBlock = GetLongestWithoutAccess(blockQueue,currentPage); idleBlock->block.time = Time++; idleBlock->block.page->pageID = currentPage->page.pageID; PrintBlockList(blockQueue, currentPage->page.pageID, COLOR_NotExist_NoIDLE); } } currentPage = currentPage->next; } }
void SelectAlgorithm(){ printf("\n========================================================\n"); printf("\t1.OPT\t2.FIFO\t3.LRU\t0.exit\n"); printf("========================================================\n"); }
int main() { bool isGoOn = true; while (isGoOn){ unsigned int blockNumber; unsigned int pageNumber; printf("请输入主存块数(即页框数):"); scanf("%u", &blockNumber); printf("请输入进程页面数:"); scanf("%u", &pageNumber); printf("========================================================\n"); printf("\t主存页框数: %u, 进程页面数: %u\n", blockNumber, pageNumber); printf("========================================================\n");
BlockQueue *blocks; process proc; printf("是否手动输入访问序列? y/n\n"); getchar(); if (getchar() == 'y'){ printf("请输入访问序列:"); for (int i = 0; i < pageNumber; ++i) { scanf("%d",&pages[i]); } InitializeProcessWithInput(&proc,pageNumber); } else{ InitializeProcess(&proc, pageNumber, 10); } blocks = InitializeBlockQueue(blockNumber);
SelectAlgorithm(); int oper; while (scanf("%d",&oper)){ int flag = 0; printf("\n---------------------------------------------------------\n"); hitCount = 0; switch (oper){ case 1: OPT(blocks, &proc); break; case 2: FIFO(blocks, &proc); break; case 3: LRU(blocks, &proc); break; case 0: flag = 1; break; default: SelectAlgorithm(); printf("非法输入!请重新输入:"); break; } if (flag == 1) break; printf("缺页中断率为:%.2lf%%\n",(1-(hitCount/pageNumber))*100); ResetBlockQueue(blocks); SelectAlgorithm(); } printf("是否重新输入访问序列? y/n\n"); getchar(); if(getchar() != 'y') isGoOn = false; system("cls"); } }
|