1. 程式人生 > >頁面置換算法

頁面置換算法

signed 超出範圍 。。 sta string 指導書 範圍 sign mage

要求:

技術分享圖片

技術分享圖片

技術分享圖片

寫了三種算法:FIFO, LRU, OPT.

指導書上有一個錯誤,即要求的磁道是0 - 319, 但是如果按照指導書上的指令生成方法, 可能會生成320這條指令,自己寫個檢測語句就成了。。

FIFO可能有點小問題,就是檢測某頁面是否在內存中是時可能出錯(極低概率),還沒有找出來。

#include<iostream>
#include<cstdio>
#include<stdlib.h>
#include<cstring>
#include<ctime>
using namespace std;

int inst[330
]; //指令 void start() { cout << "Start memory management" << endl; cout << "Producing address flow, wait for while, please." << endl; } void produce_addstream() { srand((unsigned)time(NULL)); int n = 0; int m, m1, m2; while(n < 320
) { m = rand() % 320; if(m == 319) m--; //防止出現320的指令 inst[n++] = m + 1; m1 = rand() % (m + 2); inst[n++] = m1; inst[n++] = m1 + 1; m2 = (rand() % (320 - m1 - 2)) + m1 + 2; } } int check(int page[], int mem, int checkInst) { //檢查內存中是否有當前指令所在頁面
for(int i = 0; i < mem; i++) if(checkInst / 10 == page[i]) return 1; return 0; } int chooseOutPage(int page[], int mem, int i) { int temp[32], outPage = mem; for(int j = 0; j < 32; j++) temp[j] = page[j]; for(int k = i + 1; k < 320; k++) { int ans = inst[k] / 10; for(int s = 0; s < mem; s++) if(temp[s] == ans) { temp[s] = -1; --outPage; break; } if(outPage == 1) { //選擇要置換的頁面 for(int s = 0; s < mem; s++) if(temp[s] != -1) return s; } } for(int s = 0; s < mem; s++) //最後僅剩幾條指令時,選擇要置換的頁面 if(temp[s] != -1) return s; } int chooseOutPage2(int page[], int mem, int i) { int temp[32], outPage = mem; for(int j = 0; j < 32; j++) temp[j] = page[j]; for(int k = i - 1; k >= 0; k--) { int ans = inst[k] / 10; for(int s = 0; s < mem; s++) if(temp[s] == ans) { temp[s] = -1; --outPage; break; } if(outPage == 1) { for(int s = 0; s < mem; s++) if(temp[s] != -1) return s; } } for(int s = 0; s < mem; s++) if(temp[s] != -1) return s; } void FIFO(int mem) { int page[32]; int remain = mem; int notFound = 0; int point = 0; //指針用來標記最先進入的頁面。 memset(page, -1, sizeof(page)); for(int i = 0; i < 320; i++) { if(check(page, mem, inst[i]) == 0) { //內存中無所需頁面 ++notFound; if(remain > 0) { page[mem - remain] = inst[i] / 10; --remain; } else { page[point] = inst[i] / 10; point = (point + 1) % mem; } } } cout << " memPage: " << mem << " notFound: " << notFound << " 命中率:" << float(320 - notFound) / 320 << endl; } void OPT(int mem) { int page[32]; int remain = mem; //內存中的空閑頁面 int notFound = 0; memset(page, -1, sizeof(page)); for(int i = 0; i < 320; i++) { if(check(page, mem, inst[i]) == 0) { //內存中無所需頁面 ++notFound; if(remain > 0) { //如果內存中還有空閑頁面 page[mem - remain] = inst[i] / 10; --remain; } else { int outPage = chooseOutPage(page, mem, i); page[outPage] = inst[i] / 10; } } } cout << " memPage: " << mem << " notFound: " << notFound << " 命中率:" << float(320 - notFound) / 320 << endl; } void LRU(int mem) { int page[32]; int remain = mem; //內存中空閑頁面 int notFound = 0; memset(page, -1, sizeof(page)); for(int i = 0; i < 320; i++) { if(check(page, mem, inst[i]) == 0) { ++notFound; if(remain > 0) { page[mem - remain] = inst[i] / 10; --remain; } else { int outPage = chooseOutPage2(page, mem, i); page[outPage] = inst[i] / 10; } } } cout << " memPage: " << mem << " notFound: " << notFound << " 命中率:" << float(320 - notFound) / 320 << endl; } int main() { int test[35]; //freopen("in.txt", "r", stdin); start(); produce_addstream(); cout << "choose the algorithm(1-4)\n"; int n; cin >> n; switch(n) { case 1: for(int i = 4; i <= 32; i++) OPT(i); break; case 2: for(int i = 4; i <= 32; i++) LRU(i); break; case 3: for(int i = 4; i <= 32; i++) FIFO(i);break; default: break; } memset(test, 0, sizeof(test)); cout << "\n檢測超出範圍的頁面...\n"; for(int i = 0; i < 320; i++) { int x = inst[i] / 10; test[x] = 1; if(inst[i] < 0 || inst[i] >= 320) cout << inst[i] << " ==超出範圍==\n "; } cout << "\n檢測未取到的頁面...\n"; for(int i = 0; i < 32; i++) if(test[i] == 0) cout << i << " "; return 0; }

頁面置換算法