How to simulate virtual storage system, using LRU page replacement algorithm for virtual page substitution
#include <iostream> #include <cstring> #include <algorithm> #include <list> #include <vector > #define MAX 4//initial maximum number of pages #define N 1024x768 #define HASHX #define Page_size, using namespace std;struct node{int Addr BOOL had; Effective bit boolused; Using bit}mainaddr[n];list<int>vpage; Virtual page table, mount bit vector<int>ans[n];int n,a[n];int pushpage (int x) {///Virtual page Table push if (vpage.size () ==MAXN) {//Virtual page table full when replace VPa Ge.pop_front (); Vpage.push_back (x); } else{//Virtual page is not full when direct push vpage.push_back (x);}} void record (int x) {list<int>::iteratorit; For (It=vpage.begin (); It!=vpage.end (); it++) Ans[x].push_back (*it); } void Output () {cout<< "time:"; for (inti=1;i<=n;++i) cout<<i<< ""; cout<<endl; cout<< "\ n page:"; for (inti=1;i<=n;++i) cout<<a[i]<< ""; cout<< "\N\NLRU:"; for (Inti=0;i<max;++i) {if (i) cout<< ""; for (INTJ=1;J<=N;++J) {if (Ans[j].size () >i) cout<<ans[j][i]<< ""; else cout<< ""; } cout<<endl; } cout<<endl; } int main () {cout<< "Enter the number and address stream of the virtual page address stream:" <<endl; cin>>n; for (Inti=1;i<=n;++i) cin>>a[i]; for (inti=1;i<=n;++i) {List<int>::iteratorit=find (Vpage.begin (), Vpage.end (), a[i]); if (It!=vpage.end ()) {//Hit vpage.erase (IT); Pushpage (A[i]); }else{//Miss Pushpage (A[i]); record (i); } cout<< Endl; Output (); return 0;}
How analog virtual storage systems work, replacing with LRU page replacement algorithms