操作系统的内存管理算法主要分为最近最久未使用算法(LRU),最近最少使用算法(LFU),先进先出算法,最优置换算法。这些算法都比较容易实现,在上课时做了一个课程实验,写了相关的程序:
#include <iostream> #include <queue> #include <stack> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <set> using namespace std; const int maxn=320; const int address_stream_length=320; //int op[maxn]={70,0,10,20,0,30,0,40,20,30,0,30,20,10,20,0,10,70,0,10},lost,num,upper; int op[maxn],lost,num,upper; double target; bool inmemory[35]; void optimization() { set<int> task; for(int i=0;i<maxn;i++) { //cout<<"i="<<i<<endl; int page=op[i]/10+1; if(inmemory[page]) { continue; } else { //cout<<"i="<<i<<" "<<"op[i]="<<op[i]<<" page="<<page<<endl; lost++; if(num<upper) { num++; inmemory[page]=true; task.insert(page); } else { int sum=0,out; bool count[maxn]={false}; for(int j=i+1;j<maxn;j++) { if(inmemory[op[j]/10+1]&&!count[op[j]/10+1]) { sum++; //cout<<"sum="<<sum<<endl; count[op[j]/10+1]=true; if(sum==upper) { out=op[j]/10+1; break; } } } if(sum<upper) { for(int j=1;j<=30;j++) { if(inmemory[j]&&!count[j]) { out=j; break; } } } /* cout<<"out="<<out<<" before="; for(set<int>::iterator i=task.begin();i!=task.end();i++) cout<<(*i)<<‘ ‘; cout<<" after="; */ inmemory[out]=false; task.erase(task.find(out)); task.insert(page); inmemory[page]=true; /* for(set<int>::iterator i=task.begin();i!=task.end();i++) cout<<(*i)<<‘ ‘; cout<<endl; */ } } } task.clear(); } void LRU() { set<int> task; for(int i=0;i<maxn;i++) { int page=op[i]/10+1; if(inmemory[page]) { continue; } else { lost++; //cout<<"i="<<i<<" page="<<page-1<<endl; if(num<upper) { task.insert(page); inmemory[page]=true; num++; } else { int sum=0; bool vis[maxn]={false}; for(int j=i-1;j>=0;j--) { if(!vis[op[j]/10+1]&&inmemory[op[j]/10+1]) { sum++; vis[op[j]/10+1]=true; if(sum==upper) { int out=op[j]/10+1; //cout<<"out="<<out-1<<endl; inmemory[out]=false; task.erase(task.find(out)); inmemory[page]=true; task.insert(page); break; } } } } } } task.clear(); } void FIFO() { queue<int> task; for(int i=0;i<maxn;i++) { int page=op[i]/10+1; if(inmemory[page]) { continue; } else { lost++; //cout<<"lost page="<<page<<endl; if(num<upper) { task.push(page); inmemory[page]=true; num++; } else { int tag=task.front(); inmemory[tag]=false; task.pop(); task.push(page); inmemory[page]=true; } } } while(!task.empty()) { task.pop(); } } void LFU() { int cnt[35]={0}; memset(cnt,0,sizeof(cnt)); set<int> task; for(int i=0;i<maxn;i++) { int page=op[i]/10+1; if(inmemory[page]) { cnt[page]++; continue; } else { lost++; //cout<<"i="<<i<<" page="<<page<<endl; if(num<upper) { num++; cnt[page]++; inmemory[page]=true; task.insert(page); } else { int tag=0,minm=10000; for(int j=0;j<i;j++) { int page2=op[j]/10+1; if(inmemory[page2]&&cnt[page2]<minm) { tag=page2; minm=cnt[page2]; } } //cout<<"out="<<tag<<endl; task.erase(task.find(tag)); inmemory[tag]=false; task.insert(page); inmemory[page]=true; cnt[page]++; } } } task.clear(); } int main() { srand(time(NULL)); upper=rand()%32+1; cout<<"Start memory management."<<endl; cout<<"Processing address flow,wait for while,please."<<endl; for(int i=0;i<160;i++) op[i]=rand()%320+1; for(int i=160;i<240;i++) op[i]=rand()%160+1; for(int i=240;i<320;i++) op[i]=rand()%160+160; cout<<"There are algorithms in the program"<<endl; cout<<" 1.Optimization algorithm"<<endl; cout<<" 2.Least recently used algorithm"<<endl; cout<<" 3.First in first out algorithm"<<endl; cout<<" 4.Least frequently used algorithm"<<endl; cout<<"Select an algorithm number,please:"<<endl; int tag; while(scanf("%d",&tag)!=EOF) { if(tag>=1&&tag<=4) { num=0; lost=0; memset(inmemory,false,sizeof(inmemory)); if(tag==1) optimization(); if(tag==2) LRU(); if(tag==3) FIFO(); if(tag==4) LFU(); cout<<"lost="<<lost<<endl; target=1-double(lost)/address_stream_length; cout<<"The target="<<target<<endl; cout<<"Do you want to try again with another algorithm?(y/n)"<<endl; char ch; cin>>ch; if(ch==‘y‘) continue; else if(ch==‘n‘) break; } else cout<<"There is not the algorithm in the program"<<endl; } return 0; }
时间: 2024-10-10 17:38:39