操作系统实验报告三存储器管理实验

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

课程名:操作系统地点:姓名:班级:学号:成绩项目:存储器管理实验目的要求一.理解内存页面调度的机理二.掌握几种理论页面置换算法的实现方法三.了解HASH表数据结构的使用四.通过实验比较各种调度算法的优劣一.Page.h文件#ifndef_PAGE_H#define_PAGE_Hclasscpage{public:intm_nPageNumber,m_nPageFaceNumber,m_nCounter,m_nTime;};#endif二.Memory.h文件#ifndef_MEMORY_H#define_MEMORY_HclassCMemory{public:CMemory();voidinitialize(constintnTotal_pf);voidFIFO(constintnTotal_pf);voidLRU(constintnTotal_pf);voidNUR(constintnTotal_pf);voidOPT(constintnTotal_pf);private:vectorCPage_vDiscPages;vectorCPageControl_vMemoryPages;CPageControl*_pFreepf_head,*_pBusypf_head,*_pBusypf_tail;vectorint_vMain,_vPage,_vOffset;int_nDiseffect;};CMemory::CMemory():_vDiscPages(TOTAL_VP),_vMemoryPages(TOTAL_VP),_vMain(TOTAL_INSTRUCTION),_vPage(TOTAL_INSTRUCTION),_vOffset(TOTAL_INSTRUCTION){intS,i,nRand;srand(getpid()*10);nRand=rand()%32767;S=(float)319*nRand/32767+1;for(i=0;iTOTAL_INSTRUCTION;i+=4){_vMain[i]=S;_vMain[i+1]=_vMain[i]+1;nRand=rand()%32767;_vMain[i+2]=(float)_vMain[i]*nRand/32767;_vMain[i+3]=_vMain[i+2]+1;nRand=rand()%32767;S=(float)nRand*(318-_vMain[i+2])/32767+_vMain[i+2]+2;}for(i=0;iTOTAL_INSTRUCTION;i++){_vPage[i]=_vMain[i]/10;_vOffset[i]=_vMain[i]%10;_vPage[i]%=32;}}voidCMemory::initialize(constintnTotal_pf){intix;_nDiseffect=0;for(ix=0;ix_vDiscPages.size();ix++){_vDiscPages[ix].m_nPageNumber=ix;_vDiscPages[ix].m_nPageFaceNumber=INVALID;_vDiscPages[ix].m_nCounter=0;_vDiscPages[ix].m_nTime=-1;}for(ix=1;ixnTotal_pf;ix++){_vMemoryPages[ix-1].m_pNext=&_vMemoryPages[ix];_vMemoryPages[ix-1].m_nPageFaceNumber=ix-1;}_vMemoryPages[nTotal_pf-1].m_pNext=NULL;_vMemoryPages[nTotal_pf-1].m_nPageFaceNumber=nTotal_pf-1;_pFreepf_head=&_vMemoryPages[0];}voidCMemory::FIFO(constintnTotal_pf){inti;CPageControl*p;initialize(nTotal_pf);_pBusypf_head=_pBusypf_tail=NULL;for(i=0;iTOTAL_INSTRUCTION;i++){if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){_nDiseffect+=1;if(_pFreepf_head==NULL)//noemptypages{p=_pBusypf_head-m_pNext;_vDiscPages[_pBusypf_head-m_nPageNumber].m_nPageFaceNumber=INVALID;_pFreepf_head=_pBusypf_head;_pFreepf_head-m_pNext=NULL;_pBusypf_head=p;}p=_pFreepf_head-m_pNext;_pFreepf_head-m_pNext=NULL;_pFreepf_head-m_nPageNumber=_vPage[i];_vDiscPages[_vPage[i]].m_nPageFaceNumber=_pFreepf_head-m_nPageFaceNumber;if(_pBusypf_tail==NULL)_pBusypf_head=_pBusypf_tail=_pFreepf_head;else{_pBusypf_tail-m_pNext=_pFreepf_head;_pBusypf_tail=_pFreepf_head;}_pFreepf_head=p;}}coutFIFO:1-(float)_nDiseffect/320;}voidCMemory::LRU(constintnTotal_pf){inti,j,nMin,minj,nPresentTime(0);initialize(nTotal_pf);for(i=0;iTOTAL_INSTRUCTION;i++){if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){_nDiseffect++;if(_pFreepf_head==NULL){nMin=32767;for(j=0;jTOTAL_VP;j++)//getthesubscribeoftheleastusedpage//aftertherecycleiMinisthenumberoftimes//usedoftheleastusedpagewhileminjisitssubscribeif(nMin_vDiscPages[j].m_nTime&&_vDiscPages[j].m_nPageFaceNumber!=INVALID){nMin=_vDiscPages[j].m_nTime;minj=j;}_pFreepf_head=&_vMemoryPages[_vDiscPages[minj].m_nPageFaceNumber];_vDiscPages[minj].m_nPageFaceNumber=INVALID;_vDiscPages[minj].m_nTime=-1;_pFreepf_head-m_pNext=NULL;}_vDiscPages[_vPage[i]].m_nPageFaceNumber=_pFreepf_head-m_nPageFaceNumber;_vDiscPages[_vPage[i]].m_nTime=nPresentTime;_pFreepf_head=_pFreepf_head-m_pNext;}else_vDiscPages[_vPage[i]].m_nTime=nPresentTime;nPresentTime++;}coutLRU:1-(float)_nDiseffect/320;}voidCMemory::NUR(constintnTotal_pf){inti,j,nDiscPage,nOld_DiscPage;boolbCont_flag;initialize(nTotal_pf);nDiscPage=0;for(i=0;iTOTAL_INSTRUCTION;i++){if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){_nDiseffect++;if(_pFreepf_head==NULL){bCont_flag=true;nOld_DiscPage=nDiscPage;while(bCont_flag){if(_vDiscPages[nDiscPage].m_nCounter==0&&_vDiscPages[nDiscPage].m_nPageFaceNumber!=INVALID)bCont_flag=false;else{nDiscPage++;if(nDiscPage==TOTAL_VP)nDiscPage=0;if(nDiscPage==nOld_DiscPage)for(j=0;jTOTAL_VP;j++)_vDiscPages[j].m_nCounter=0;}}_pFreepf_head=&_vMemoryPages[_vDiscPages[nDiscPage].m_nPageFaceNumber];_vDiscPages[nDiscPage].m_nPageFaceNumber=INVALID;_pFreepf_head-m_pNext=NULL;}_vDiscPages[_vPage[i]].m_nPageFaceNumber=_pFreepf_head-m_nPageFaceNumber;_pFreepf_head=_pFreepf_head-m_pNext;}else_vDiscPages[_vPage[i]].m_nCounter=1;if(i%CLEAR_PERIOD==0)for(j=0;jTOTAL_VP;j++)_vDiscPages[j].m_nCounter=0;}coutNUR:1-(float)_nDiseffect/320;}voidCMemory::OPT(constintnTotal_pf){inti,j,max,maxpage,nDistance,vDistance[TOTAL_VP];initialize(nTotal_pf);for(i=0;iTOTAL_INSTRUCTION;i++){if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){_nDiseffect++;if(_pFreepf_head==NULL){for(j=0;jTOTAL_VP;j++)if(_vDiscPages[j].m_nPageFaceNumber!=INVALID)vDistance[j]=32767;elsevDistance[j]=0;nDistance=1;for(j=i+1;jTOTAL_INSTRUCTION;j++){if((_vDiscPages[_vPage[j]].m_nPageFaceNumber!=INVALID)&&(vDistance[_vPage[j]]==32767))vDistance[_vPage[j]]=nDistance;nDistance++;}max=-1;for(j=0;jTOTAL_VP;j++)if(maxvDistance[j]){max=vDistance[j];maxpage=j;}_pFreepf_head=&_vMemoryPages[_vDiscPages[maxpage].m_nPageFaceNumber];_pFreepf_head-m_pNext=NULL;_vDiscPages[maxpage].m_nPageFac

1 / 7
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功