和之前的實驗一樣,仍然是模擬,這次是模擬作業系統的記憶體管理。
要求實現opt置換演算法與clock演算法。
演算法具體不再贅述,參考下文。
clock演算法概要:
當頁表未滿時,若新到達程序未在頁表中,進表且指標+1,若新到達程序已在頁表中,指標不移動。
頁表滿後,若新到達程序已在頁表中,將該程序使用狀態改為true(注:無論該程序當前程序使用狀態是true或false,都執行將使用狀態改為true的操作),且指標不移動,且指標不移動,且指標不移動。參見下圖所示樣例。
圖注:深色帶星號表示true,淺色表示false。
(三張圖都是新到程序已在頁表中,體現了直接修改狀態而不移動指標的特點。)
若新到達程序不在頁表中,則從指標所在位置開始掃瞄,若指向位置使用狀態為true,改為false,若指向位置使用狀態為false,將新到達程序置於此位置,並將此位置的使用狀態改為true(掃瞄過程中注意對角標取模避免越界,最開始我就栽在這裡)。
page fault簡單粗暴版識別概要:
(必要條件:頁表已滿)將當前頁表與前乙個頁表比較,若不一樣,page fault++;
例:
下方帶f的頁表都與前乙個頁表不同。
同理:
/*
title:opt
author:uranuslight
school:swjtu
build date:2017/05/26
last revision:2017/05/26
version:1.0
*/#include#include#include#include#define multiple 3
#define inf 0x3f3f3f3f
using namespace std;
int pagesize;//cin >> pagesize
int processnum;//num of process
int page[10];
int nowsize = 0;//is pageframe full?
int origin[100];
int pagefault = 0;
typedef struct
target;
void opt(int len,int origin[100])//len processnum*multiple
else
break;
}}//find fail then variable success is 0
if (success == 0)//find fail,this process is not in next array
}if (success == 1)
}} cout << "step " << i << ":";
for (int k = 0; k> pagesize >> processnum;
if (pagesize==0 || processnum==0)
break;
int len = processnum*multiple;
for (int i = 0; i < len; i++)
cout << endl;
opt(len, origin);
} system("pause");
return 0;
}
/*
title:clock
author:uranuslight
school:swjtu
build date:2017/05/27
last revision:2017/05/27
version:1.0
*/#include#include#include#include#define multiple 3
#define inf 0x3f3f3f3f
using namespace std;
typedef struct page;
int pagesize;//cin >> pagesize
int processnum;//num of process
int nowsize = 0;//is pageframe full?
int origin[100];
int pagefault = 0;
int pointer = 0;
page page[10];
void clock(int len, int origin[100])//len processnum*multiple
cout << endl;
clock(len, origin);
} system("pause");
return 0;
}
示例**test5.c
lru與fifo演算法,一起貼出來好了。
#include #include #include #define random(x) (rand() % x)
#define multiple 3
typedef struct page_s page;
char *menu = ;
int getchoice(char *greet, char *choices)
do while(selected == '\n');
option = choices;
while(*option)
option++;
}if(!chosen)
} while(!chosen);
return selected;
}void buildpagereference(int size, page **reference, page *program)
return count;
}int findlastmax(page *frame, int size)
}return j;
}int findlastmin(page *frame, int size)
else
print(i, frame, fsize);
}printf("page fault : %d\n", f);
}int lru(int fsize, page *frame, int rsize, page **pager) else
print(i, frame, fsize);
}printf("page fault : %d\n", f);
}int main()
}while(choice != 'q');
exit(0);
}
作業系統記憶體管理
作業系統記憶體管理 一 程序的虛擬位址空間 每個程序都被賦予自己的虛擬位址空間,對於32位程序來說,這個位址空間為4g,因此程序中的位址可以為0x00000000至0xffffffff之間的任何乙個值。其中4g空間中的低區的2g空間留給程序使用,而高區的2g空間則留給系統使用。在windows200...
作業系統記憶體管理
作業系統記憶體管理一 程序的虛擬位址空間 每個程序都被賦予自己的虛擬位址空間,對於 32位程序來說,這個位址空間為 4g,因此程序中的位址可以為 0x00000000 至0xffffffff 之間的任何乙個值。其中 4g空間中的低區的 2g空間留給程序使用,而高區的 2g空間則留給系統使用。在win...
作業系統記憶體管理
記憶體,毫無疑問是最重要的資源,顯然,作業系統對記憶體的管理我必須清楚。這裡主要介紹了分頁管理和分段管理。1.頁式管理 a.頁式管理的基本思想 打破儲存分配的連續性 將邏輯上連續的使用者程式對映到離散的記憶體塊 使用者程式與記憶體空間被劃分為若干等長的區域 邏輯頁 與 物理頁 使用者程式的劃分由系統...