SlideShare a Scribd company logo
1 of 29
第 4 章  存储器管理实验 ,[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object],#ifndef _PAGE_H #define _PAGE_H class cpage  { public: int m_nPageNumber, m_nPageFaceNumber, m_nCounter, m_nTime; }; #endif
#ifndef _PAGECONTROL_H #define _PAGECONTROL_H class CpageControl  { public: int m_nPageNumber,m_nPageFaceNumber; class CPageControl  *  m_pNext; }; #endif
#ifndef _MEMORY_H #define _MEMORY_H   class CMemory  { public: CMemory(); void initialize(const int nTotal_pf); void FIFO(const int nTotal_pf); void LRU(const int nTotal_pf); void NUR(const int nTotal_pf); void OPT(const int nTotal_pf);
private: vector<CPage> _vDiscPages; vector<CPageControl> _vMemoryPages; CPageControl  * _pFreepf_head, * _pBusypf_head,  * _pBusypf_tail; vector<int> _vMain,_vPage,_vOffset; int _nDiseffect; };  
CMemory::CMemory(): _vDiscPages(TOTAL_VP),   _vMemoryPages(TOTAL_VP),   _vMain(TOTAL_INSTRUCTION),   _vPage(TOTAL_INSTRUCTION),   _vOffset(TOTAL_INSTRUCTION) { int S,i,nRand; srand(getpid() * 10);
nRand=rand()%32767; S=(float)319 * nRand/32767+1; for(i=0;i<TOTAL_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;i<TOTAL_INSTRUCTION;i++)  { _vPage[i]=_vMain[i]/10; _vOffset[i]=_vMain[i]%10; _vPage[i]%=32; } }
void CMemory::initialize(const int nTotal_pf){ int ix; _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;ix<nTotal_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_nPageFace Number=nTotal_pf-1; _pFreepf_head=&_vMemoryPages[0]; }   void CMemory::FIFO(const int nTotal_pf)  { int i; CPageControl *p; initialize(nTotal_pf); _pBusypf_head=_pBusypf_tail=NULL; for(i=0;i<TOTAL_INSTRUCTION;i++)  {
if(_vDiscPages[_vPage[i]].m_nPageFaceNumber ==INVALID)  {   _nDiseffect+=1; if(_pFreepf_head==NULL)  //  无空闲页面 {   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;   }   } cout<<&quot;FIFO: &quot;<<1-(float)_nDiseffect/320; }   void CMemory::LRU(const int nTotal_pf)  { int i,j,nMin,minj,nPresentTime(0); initialize(nTotal_pf);
for(i=0;i<TOTAL_INSTRUCTION;i++)  { if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){ _nDiseffect++; if(_pFreepf_head==NULL)   { nMin=32767; for(j=0;j<TOTAL_VP;j++)  //  得到最近最少使用的页面的页号 //  循环结束后 ,iMin 表示最近最少使用页面的访问次数 ;minj 表示需要换出的页号 if(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++; } cout<<&quot;LRU: &quot;<<1-(float)_nDiseffect/320; } void CMemory::NUR(const int nTotal_pf)  { int i,j,nDiscPage,nOld_DiscPage; bool bCont_flag; initialize(nTotal_pf); nDiscPage=0;
for(i=0;i<TOTAL_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;j<TOTAL_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;j<TOTAL_VP;j++)   _vDiscPages[j].m_nCounter=0; }
cout<<&quot;NUR:&quot;<<1-(float)_nDiseffect/320; }  void CMemory::OPT(const int nTotal_pf) { int i,j,max,maxpage,nDistance,vDistance[TOTAL_VP]; initialize(nTotal_pf); for(i=0;i<TOTAL_INSTRUCTION;i++)  { if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID) { _nDiseffect++;   if(_pFreepf_head==NULL)   {   for(j=0;j<TOTAL_VP;j++) if(_vDiscPages[j].m_nPageFaceNumber!=INVALID)   vDistance[j]=32767; else   vDistance[j]=0;
nDistance=1; for(j=i+1;j<TOTAL_INSTRUCTION;j++) { if((_vDiscPages[_vPage[j]].m_nPageFaceNumber!=INVALID)&& (vDistance[_vPage[j]]==32767)) vDistance[_vPage[j]]=nDistance;  nDistance++; } max =-1; for(j=0;j<TOTAL_VP;j++) if(max<vDistance[j]) { max=vDistance[j]; maxpage=j;}
_pFreepf_head=&_vMemoryPages[_vDiscPages [maxpage].m_nPageFaceNumber]; _pFreepf_head->m_pNext=NULL; _vDiscPages[maxpage].m_nPageFaceNumber=INVALID; } _vDiscPages[_vPage[i]].m_nPageFaceNumber= _pFreepf_head->m_nPageFaceNumber;   _pFreepf_head=_pFreepf_head->m_pNext; } } cout<<&quot;OPT:&quot;<<1-(float)_nDiseffect/320; } #endif
#include <iostream> #include <string> #include <vector> #include <cstdlib> #include <cstdio> #include <unistd.h> using namespace std; #define INVALID -1 const int TOTAL_INSTRUCTION(320); const int TOTAL_VP(32); const int CLEAR_PERIOD(50);
#include &quot;Page.h&quot; #include &quot;PageControl.h&quot; #include &quot;Memory.h&quot; int main()  { int i; CMemory a; for(i=4;i<=32;i++)  {   a.FIFO(i);   a.LRU(i);   a.NUR(i);   a.OPT(i);   cout<<&quot;&quot;;}  return 0;}
[object Object],g++ -o main main.cpp ↙ ./main ↙  

More Related Content

What's hot

言語の設計判断
言語の設計判断言語の設計判断
言語の設計判断nishio
 
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo Haitham Nabil
 
メディアアートにおけるプログラミング言語Rubyの役割
メディアアートにおけるプログラミング言語Rubyの役割メディアアートにおけるプログラミング言語Rubyの役割
メディアアートにおけるプログラミング言語Rubyの役割Koichiro Eto
 
BDD revolution - or how we came back from hell
BDD revolution - or how we came back from hellBDD revolution - or how we came back from hell
BDD revolution - or how we came back from hellMateusz Zalewski
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !Matheus Marabesi
 
Phing - PHP Conference 2015
Phing -  PHP Conference 2015Phing -  PHP Conference 2015
Phing - PHP Conference 2015Matheus Marabesi
 
Hadoop, HDFS, MapReduce and Pig
Hadoop, HDFS, MapReduce and PigHadoop, HDFS, MapReduce and Pig
Hadoop, HDFS, MapReduce and PigTomasz Bednarz
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the bookVishal Ghadge
 
Pim Elshoff "Final Class Aggregate"
Pim Elshoff "Final Class Aggregate"Pim Elshoff "Final Class Aggregate"
Pim Elshoff "Final Class Aggregate"Fwdays
 
Todos os passos para a certificação PHP - PHPExperience2017
Todos os passos para a certificação PHP - PHPExperience2017 Todos os passos para a certificação PHP - PHPExperience2017
Todos os passos para a certificação PHP - PHPExperience2017 Matheus Marabesi
 
Dr Strangler and Mr Hype - Strangler pattern w praktyce
Dr Strangler and Mr Hype - Strangler pattern w praktyceDr Strangler and Mr Hype - Strangler pattern w praktyce
Dr Strangler and Mr Hype - Strangler pattern w praktyceMichał Kurzeja
 
Trinity Kings World Leadership: Conference of Chief Justices
Trinity Kings World Leadership: Conference of Chief JusticesTrinity Kings World Leadership: Conference of Chief Justices
Trinity Kings World Leadership: Conference of Chief JusticesTerrell Patillo
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซีkramsri
 
An Introduction to Tinkerpop
An Introduction to TinkerpopAn Introduction to Tinkerpop
An Introduction to TinkerpopTakahiro Inoue
 

What's hot (20)

Database api
Database apiDatabase api
Database api
 
Netcfg 122078
Netcfg 122078Netcfg 122078
Netcfg 122078
 
言語の設計判断
言語の設計判断言語の設計判断
言語の設計判断
 
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo
 
メディアアートにおけるプログラミング言語Rubyの役割
メディアアートにおけるプログラミング言語Rubyの役割メディアアートにおけるプログラミング言語Rubyの役割
メディアアートにおけるプログラミング言語Rubyの役割
 
BDD revolution - or how we came back from hell
BDD revolution - or how we came back from hellBDD revolution - or how we came back from hell
BDD revolution - or how we came back from hell
 
TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !TDC2015 Porto Alegre - Automate everything with Phing !
TDC2015 Porto Alegre - Automate everything with Phing !
 
Pop3ck sh
Pop3ck shPop3ck sh
Pop3ck sh
 
Linux tips
Linux tipsLinux tips
Linux tips
 
Phing - PHP Conference 2015
Phing -  PHP Conference 2015Phing -  PHP Conference 2015
Phing - PHP Conference 2015
 
Hadoop, HDFS, MapReduce and Pig
Hadoop, HDFS, MapReduce and PigHadoop, HDFS, MapReduce and Pig
Hadoop, HDFS, MapReduce and Pig
 
Burrowing through go! the book
Burrowing through go! the bookBurrowing through go! the book
Burrowing through go! the book
 
Pim Elshoff "Final Class Aggregate"
Pim Elshoff "Final Class Aggregate"Pim Elshoff "Final Class Aggregate"
Pim Elshoff "Final Class Aggregate"
 
Todos os passos para a certificação PHP - PHPExperience2017
Todos os passos para a certificação PHP - PHPExperience2017 Todos os passos para a certificação PHP - PHPExperience2017
Todos os passos para a certificação PHP - PHPExperience2017
 
Fragmentation
FragmentationFragmentation
Fragmentation
 
TerminalでTwitter
TerminalでTwitterTerminalでTwitter
TerminalでTwitter
 
Dr Strangler and Mr Hype - Strangler pattern w praktyce
Dr Strangler and Mr Hype - Strangler pattern w praktyceDr Strangler and Mr Hype - Strangler pattern w praktyce
Dr Strangler and Mr Hype - Strangler pattern w praktyce
 
Trinity Kings World Leadership: Conference of Chief Justices
Trinity Kings World Leadership: Conference of Chief JusticesTrinity Kings World Leadership: Conference of Chief Justices
Trinity Kings World Leadership: Conference of Chief Justices
 
ภาษาซี
ภาษาซีภาษาซี
ภาษาซี
 
An Introduction to Tinkerpop
An Introduction to TinkerpopAn Introduction to Tinkerpop
An Introduction to Tinkerpop
 

Viewers also liked

Open Data France, example of project worth expanding
Open Data France, example of project worth expandingOpen Data France, example of project worth expanding
Open Data France, example of project worth expandingliberTIC
 
chuyên đề thực tập tốt nghiệp
chuyên đề thực tập tốt nghiệpchuyên đề thực tập tốt nghiệp
chuyên đề thực tập tốt nghiệpguest3c41775
 
20 bí quyết bán hàng
20 bí quyết bán hàng20 bí quyết bán hàng
20 bí quyết bán hàngguest3c41775
 
Softexpert GRC Kurumsal Yönetişim Risk ve Uyumluluk
Softexpert GRC Kurumsal Yönetişim Risk ve UyumlulukSoftexpert GRC Kurumsal Yönetişim Risk ve Uyumluluk
Softexpert GRC Kurumsal Yönetişim Risk ve UyumlulukHydron Consulting Grup
 
BISB New Lighted Inflatables
BISB New Lighted InflatablesBISB New Lighted Inflatables
BISB New Lighted InflatablesKennethC
 
Utviklingsfondets vanntiltak
Utviklingsfondets vanntiltakUtviklingsfondets vanntiltak
Utviklingsfondets vanntiltakUtviklingsfondet
 
Odwm stevenflower-animations1645
Odwm stevenflower-animations1645Odwm stevenflower-animations1645
Odwm stevenflower-animations1645liberTIC
 
Chuyên đề tốt nghiệp
Chuyên đề tốt nghiệpChuyên đề tốt nghiệp
Chuyên đề tốt nghiệpguest3c41775
 
Phim Tat Trong Excel
Phim Tat Trong ExcelPhim Tat Trong Excel
Phim Tat Trong Excelguest3c41775
 
Open Food Facts presentation ODWM
Open Food Facts presentation ODWMOpen Food Facts presentation ODWM
Open Food Facts presentation ODWMliberTIC
 
Enterprise Digital Writing
Enterprise Digital WritingEnterprise Digital Writing
Enterprise Digital Writingmcrussell
 
MEDICARE EHR incentive program registration user guide for eligible professio...
MEDICARE EHR incentive program registration user guide for eligible professio...MEDICARE EHR incentive program registration user guide for eligible professio...
MEDICARE EHR incentive program registration user guide for eligible professio...The Fox Group, LLC
 

Viewers also liked (20)

Open Data France, example of project worth expanding
Open Data France, example of project worth expandingOpen Data France, example of project worth expanding
Open Data France, example of project worth expanding
 
chuyên đề thực tập tốt nghiệp
chuyên đề thực tập tốt nghiệpchuyên đề thực tập tốt nghiệp
chuyên đề thực tập tốt nghiệp
 
Mayur Work
Mayur WorkMayur Work
Mayur Work
 
20 bí quyết bán hàng
20 bí quyết bán hàng20 bí quyết bán hàng
20 bí quyết bán hàng
 
Softexpert GRC Kurumsal Yönetişim Risk ve Uyumluluk
Softexpert GRC Kurumsal Yönetişim Risk ve UyumlulukSoftexpert GRC Kurumsal Yönetişim Risk ve Uyumluluk
Softexpert GRC Kurumsal Yönetişim Risk ve Uyumluluk
 
Vw chattanooga plant part 2
Vw chattanooga plant part 2Vw chattanooga plant part 2
Vw chattanooga plant part 2
 
BISB New Lighted Inflatables
BISB New Lighted InflatablesBISB New Lighted Inflatables
BISB New Lighted Inflatables
 
Utviklingsfondets vanntiltak
Utviklingsfondets vanntiltakUtviklingsfondets vanntiltak
Utviklingsfondets vanntiltak
 
Odwm stevenflower-animations1645
Odwm stevenflower-animations1645Odwm stevenflower-animations1645
Odwm stevenflower-animations1645
 
Basic
BasicBasic
Basic
 
Chuyên đề tốt nghiệp
Chuyên đề tốt nghiệpChuyên đề tốt nghiệp
Chuyên đề tốt nghiệp
 
The Road to Bratislava II
The Road to Bratislava IIThe Road to Bratislava II
The Road to Bratislava II
 
KYE Keynote
KYE KeynoteKYE Keynote
KYE Keynote
 
IBM Proventia IPS
IBM Proventia IPSIBM Proventia IPS
IBM Proventia IPS
 
Phim Tat Trong Excel
Phim Tat Trong ExcelPhim Tat Trong Excel
Phim Tat Trong Excel
 
Open Food Facts presentation ODWM
Open Food Facts presentation ODWMOpen Food Facts presentation ODWM
Open Food Facts presentation ODWM
 
Enterprise Digital Writing
Enterprise Digital WritingEnterprise Digital Writing
Enterprise Digital Writing
 
Andres E Pp Short Web
Andres E Pp Short WebAndres E Pp Short Web
Andres E Pp Short Web
 
Hydron Group Kurumsal Sunumu
Hydron Group Kurumsal SunumuHydron Group Kurumsal Sunumu
Hydron Group Kurumsal Sunumu
 
MEDICARE EHR incentive program registration user guide for eligible professio...
MEDICARE EHR incentive program registration user guide for eligible professio...MEDICARE EHR incentive program registration user guide for eligible professio...
MEDICARE EHR incentive program registration user guide for eligible professio...
 

Similar to 第4章 存储器管理实验 (20)

BCSL 058 solved assignment
BCSL 058 solved assignmentBCSL 058 solved assignment
BCSL 058 solved assignment
 
#include stdio.h #include stdlib.h #include unistd.h .pdf
 #include stdio.h #include stdlib.h #include unistd.h .pdf #include stdio.h #include stdlib.h #include unistd.h .pdf
#include stdio.h #include stdlib.h #include unistd.h .pdf
 
Потоки в перле изнутри
Потоки в перле изнутриПотоки в перле изнутри
Потоки в перле изнутри
 
All I know about rsc.io/c2go
All I know about rsc.io/c2goAll I know about rsc.io/c2go
All I know about rsc.io/c2go
 
Fx oracle - AS400
Fx oracle - AS400Fx oracle - AS400
Fx oracle - AS400
 
Marat-Slides
Marat-SlidesMarat-Slides
Marat-Slides
 
3
33
3
 
Double linked list
Double linked listDouble linked list
Double linked list
 
Chapter5.pptx
Chapter5.pptxChapter5.pptx
Chapter5.pptx
 
Building Maintainable Applications in Apex
Building Maintainable Applications in ApexBuilding Maintainable Applications in Apex
Building Maintainable Applications in Apex
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
深入淺出C語言
深入淺出C語言深入淺出C語言
深入淺出C語言
 
C語言函式
C語言函式C語言函式
C語言函式
 
cpract.docx
cpract.docxcpract.docx
cpract.docx
 
Native Payment - Part 3.pdf
Native Payment - Part 3.pdfNative Payment - Part 3.pdf
Native Payment - Part 3.pdf
 
How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15망고100 보드로 놀아보자 15
망고100 보드로 놀아보자 15
 
Stored procedure
Stored procedureStored procedure
Stored procedure
 

Recently uploaded

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 

Recently uploaded (20)

Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 

第4章 存储器管理实验

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7. #ifndef _PAGECONTROL_H #define _PAGECONTROL_H class CpageControl { public: int m_nPageNumber,m_nPageFaceNumber; class CPageControl * m_pNext; }; #endif
  • 8. #ifndef _MEMORY_H #define _MEMORY_H   class CMemory { public: CMemory(); void initialize(const int nTotal_pf); void FIFO(const int nTotal_pf); void LRU(const int nTotal_pf); void NUR(const int nTotal_pf); void OPT(const int nTotal_pf);
  • 9. private: vector<CPage> _vDiscPages; vector<CPageControl> _vMemoryPages; CPageControl * _pFreepf_head, * _pBusypf_head, * _pBusypf_tail; vector<int> _vMain,_vPage,_vOffset; int _nDiseffect; };  
  • 10. CMemory::CMemory(): _vDiscPages(TOTAL_VP), _vMemoryPages(TOTAL_VP), _vMain(TOTAL_INSTRUCTION), _vPage(TOTAL_INSTRUCTION), _vOffset(TOTAL_INSTRUCTION) { int S,i,nRand; srand(getpid() * 10);
  • 11. nRand=rand()%32767; S=(float)319 * nRand/32767+1; for(i=0;i<TOTAL_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;
  • 12. S=(float)nRand * (318-_vMain[i+2])/ 32767+_vMain[i+2]+2; } for(i=0;i<TOTAL_INSTRUCTION;i++) { _vPage[i]=_vMain[i]/10; _vOffset[i]=_vMain[i]%10; _vPage[i]%=32; } }
  • 13. void CMemory::initialize(const int nTotal_pf){ int ix; _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;ix<nTotal_pf;ix++) { _vMemoryPages[ix-1].m_pNext=&_vMemoryPages[ix]; _vMemoryPages[ix-1].m_nPageFaceNumber=ix-1; }
  • 14. _vMemoryPages[nTotal_pf-1].m_pNext=NULL; _vMemoryPages[nTotal_pf-1].m_nPageFace Number=nTotal_pf-1; _pFreepf_head=&_vMemoryPages[0]; }   void CMemory::FIFO(const int nTotal_pf) { int i; CPageControl *p; initialize(nTotal_pf); _pBusypf_head=_pBusypf_tail=NULL; for(i=0;i<TOTAL_INSTRUCTION;i++) {
  • 15. if(_vDiscPages[_vPage[i]].m_nPageFaceNumber ==INVALID) { _nDiseffect+=1; if(_pFreepf_head==NULL) // 无空闲页面 { 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; }
  • 16. 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; }
  • 17. _pFreepf_head=p; } } cout<<&quot;FIFO: &quot;<<1-(float)_nDiseffect/320; }   void CMemory::LRU(const int nTotal_pf) { int i,j,nMin,minj,nPresentTime(0); initialize(nTotal_pf);
  • 18. for(i=0;i<TOTAL_INSTRUCTION;i++) { if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID){ _nDiseffect++; if(_pFreepf_head==NULL) { nMin=32767; for(j=0;j<TOTAL_VP;j++) // 得到最近最少使用的页面的页号 // 循环结束后 ,iMin 表示最近最少使用页面的访问次数 ;minj 表示需要换出的页号 if(nMin>_vDiscPages[j].m_nTime &&_vDiscPages[j].m_nPageFaceNumber!=INVALID) { nMin=_vDiscPages[j].m_nTime; minj=j;}
  • 19. _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; }
  • 20. else _vDiscPages[_vPage[i]].m_nTime=nPresentTime; nPresentTime++; } cout<<&quot;LRU: &quot;<<1-(float)_nDiseffect/320; } void CMemory::NUR(const int nTotal_pf) { int i,j,nDiscPage,nOld_DiscPage; bool bCont_flag; initialize(nTotal_pf); nDiscPage=0;
  • 21. for(i=0;i<TOTAL_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;
  • 22. else { nDiscPage++; if(nDiscPage==TOTAL_VP) nDiscPage=0; if(nDiscPage==nOld_DiscPage) for(j=0;j<TOTAL_VP;j++) _vDiscPages[j].m_nCounter=0; } } _pFreepf_head=&_vMemoryPages [_vDiscPages[nDiscPage].m_nPageFaceNumber];
  • 23. _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;j<TOTAL_VP;j++) _vDiscPages[j].m_nCounter=0; }
  • 24. cout<<&quot;NUR:&quot;<<1-(float)_nDiseffect/320; }  void CMemory::OPT(const int nTotal_pf) { int i,j,max,maxpage,nDistance,vDistance[TOTAL_VP]; initialize(nTotal_pf); for(i=0;i<TOTAL_INSTRUCTION;i++) { if(_vDiscPages[_vPage[i]].m_nPageFaceNumber==INVALID) { _nDiseffect++; if(_pFreepf_head==NULL) { for(j=0;j<TOTAL_VP;j++) if(_vDiscPages[j].m_nPageFaceNumber!=INVALID) vDistance[j]=32767; else vDistance[j]=0;
  • 25. nDistance=1; for(j=i+1;j<TOTAL_INSTRUCTION;j++) { if((_vDiscPages[_vPage[j]].m_nPageFaceNumber!=INVALID)&& (vDistance[_vPage[j]]==32767)) vDistance[_vPage[j]]=nDistance; nDistance++; } max =-1; for(j=0;j<TOTAL_VP;j++) if(max<vDistance[j]) { max=vDistance[j]; maxpage=j;}
  • 26. _pFreepf_head=&_vMemoryPages[_vDiscPages [maxpage].m_nPageFaceNumber]; _pFreepf_head->m_pNext=NULL; _vDiscPages[maxpage].m_nPageFaceNumber=INVALID; } _vDiscPages[_vPage[i]].m_nPageFaceNumber= _pFreepf_head->m_nPageFaceNumber; _pFreepf_head=_pFreepf_head->m_pNext; } } cout<<&quot;OPT:&quot;<<1-(float)_nDiseffect/320; } #endif
  • 27. #include <iostream> #include <string> #include <vector> #include <cstdlib> #include <cstdio> #include <unistd.h> using namespace std; #define INVALID -1 const int TOTAL_INSTRUCTION(320); const int TOTAL_VP(32); const int CLEAR_PERIOD(50);
  • 28. #include &quot;Page.h&quot; #include &quot;PageControl.h&quot; #include &quot;Memory.h&quot; int main() { int i; CMemory a; for(i=4;i<=32;i++) { a.FIFO(i); a.LRU(i); a.NUR(i); a.OPT(i); cout<<&quot;&quot;;} return 0;}
  • 29.