本文主要是介绍实验八 页面置换模拟程序设计,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网上找到的程序得到的答案经过手算验证是错的,所以自己实现了一个,具体实现看代码吧,多余的操作已经去掉了。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>#define VM_PAGE 7 /*假设每个页面可以存放10条指令,则共有32个虚页*/
#define PM_PAGE 4 /*分配给作业的内存块数为41*/
#define TOTAL_INSERT 18
typedef struct
{int vmn;int pmn;int exist;int time;
} vpage_item;
vpage_item page_table[VM_PAGE];vpage_item *ppage_bitmap[PM_PAGE];int vpage_arr[TOTAL_INSERT] = {1, 2, 3, 4, 2, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6};void init_data() // 数据初始化
{for (int i = 0; i < VM_PAGE; i++){page_table[i].vmn = i + 1; // 虚页号page_table[i].pmn = -1; // 实页号page_table[i].exist = 0;page_table[i].time = -1;}for (int i = 0; i < PM_PAGE; i++) /*最初4个物理块为空*/{ppage_bitmap[i] = NULL;}
}void FIFO() /*FIFO页面置换算法*/
{int k = 0;int i;int sum = 0;int missing_page_count = 0;int current_time = 0;bool isleft = true; /*当前物理块中是否有剩余*/while (sum < TOTAL_INSERT){if (page_table[vpage_arr[sum] - 1].exist == 0){missing_page_count++;if (k < 4){if (ppage_bitmap[k] == NULL) /*找到一个空闲物理块*/{ppage_bitmap[k] = &page_table[vpage_arr[sum] - 1];ppage_bitmap[k]->exist = 1;ppage_bitmap[k]->pmn = k;ppage_bitmap[k]->time = current_time;k++;}}else{int temp = ppage_bitmap[0]->time; /*记录物理块中作业最早到达时间*/int j = 0; /*记录应当被替换的物理块号*/for (i = 0; i < PM_PAGE; i++){if (ppage_bitmap[i]->time < temp){temp = ppage_bitmap[i]->time;j = i;}}ppage_bitmap[j]->exist = 0;ppage_bitmap[j] = &page_table[vpage_arr[sum] - 1]; /*更新页表项*/ppage_bitmap[j]->exist = 1;ppage_bitmap[j]->pmn = j;ppage_bitmap[j]->time = current_time;}}current_time++;sum++;}printf("FIFO算法缺页次数为:%d\t缺页率为:%f\t置换次数为:%d\t置换率为:%f\n", missing_page_count, missing_page_count / (float)TOTAL_INSERT, missing_page_count - 4, (missing_page_count - 4) / (float)TOTAL_INSERT);
}
void LRU()
{int k = 0;int i;int sum = 0;int missing_page_count = 0;int isfill = 0;while (sum < TOTAL_INSERT){int page_index = vpage_arr[sum] - 1;if (page_table[page_index].exist == 0){missing_page_count++;if (isfill < PM_PAGE){ppage_bitmap[isfill] = &page_table[page_index];page_table[page_index].pmn = isfill;page_table[page_index].exist = 1;isfill++;}else{int k = 0, mintime = ppage_bitmap[0]->time;for (int i = 0; i < PM_PAGE; i++){if (ppage_bitmap[i]->time < mintime){mintime = ppage_bitmap[i]->time;k = i;}}ppage_bitmap[k]->exist = 0;ppage_bitmap[k]->time = 0;ppage_bitmap[k] = &page_table[page_index];page_table[page_index].exist = 1;ppage_bitmap[k]->pmn = k;}}elsepage_table[page_index].time = 0;for (int i = 0; i < PM_PAGE; i++) // 更新时间if (ppage_bitmap[i] != NULL)ppage_bitmap[i]->time++;sum++;}printf("LRU 算法缺页次数为:%d\t 缺页率为:%f\t置换次数为:%d\t置换率为:%f\n", missing_page_count, missing_page_count / (float)TOTAL_INSERT, missing_page_count - 4, (missing_page_count - 4) / (float)TOTAL_INSERT);
}
int LastestIndex(int l)//找到最久访问页面
{int k = 0;for (int i = 0; i < PM_PAGE; i++){for (int j = l + 1; j < TOTAL_INSERT; j++){if (ppage_bitmap[i]->pmn == vpage_arr[j]){k = k > j ? k : j;break;}}}return vpage_arr[k];
}
void OPT()
{int i, j;int sum = 0;int missing_page_count = 0;int isfill = 0;while (sum < TOTAL_INSERT){int page_index = vpage_arr[sum] - 1;if (page_table[page_index].exist == 0){ // 如果页面不在内存中missing_page_count++; // 缺页次数加1if (isfill < PM_PAGE){// 查找空闲物理块ppage_bitmap[isfill] = &page_table[page_index];page_table[page_index].pmn = isfill;page_table[page_index].exist = 1;isfill++;}else{ // 内存已经满了,置换int last = LastestIndex(sum);for (int i = 0; i < PM_PAGE; i++)if (ppage_bitmap[i]->vmn == last){last = i;break;}page_table[ppage_bitmap[last]->vmn].exist = 0;ppage_bitmap[last] = &page_table[page_index];page_table[page_index].exist = 1;}}sum++;}// 输出结果printf("OPT算法缺页次数为:%d\t 缺页率为:%f\t置换次数为:%d\t置换率为:%f\n", missing_page_count, missing_page_count / (float)TOTAL_INSERT, missing_page_count - 4, (missing_page_count - 4) / (float)TOTAL_INSERT);
}int main()
{int a;printf("请输入需要选择的页面置换算法:1.FIFO\t2.LRU\t3.OPT\t输入0结束\n");do{scanf_s("%d", &a);switch (a){case 1:init_data();FIFO();break;case 2:init_data();LRU();break;case 3:init_data();OPT();break;}} while (a != 0);return 0;
}
这篇关于实验八 页面置换模拟程序设计的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!