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

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 

Recently uploaded (20)

Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

第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.