# Page Replacement Algorithm (FIFO, LRU, best, clock)

Source: Internet
Author: User

Page Replacement Algorithm (FIFO, LRU, best, clock)

# Include <iostream> <br/> using namespace STD; <br/> void print (int bc [], int blockcount) <br/> {<br/> for (INT I = 0; I <blockcount; I ++) <br/>{< br/> cout <BC [I] <""; <br/>}< br/> cout <Endl; <br/>}</P> <p> bool travel (int bc [], int blockcount, int X) <br/>{< br/> bool is_found = false; <br/> int I; <br/> for (I = 0; I <blockcount; I ++) <br/>{< br/> If (BC [I] = x) <br/>{< br/> is_found = true; <br/> break; <br/>}< br/> retu Rn is_found; <br/>}</P> <p> void FIFO (int pc [], int BC [], int pagecount, int blockcount) <br/>{< br/> cout <"0: FIFO replacement algorithm" <Endl; <br/> int I; <br/> If (pagecount <= blockcount) <br/> {<br/> cout <"number of missing pages" <0 <Endl; <br/> cout <"page missing rate:" <0 <Endl; <br/>}< br/> else <br/> {<br/> int nopage = 0; <br/> int p = 0; <br/> for (I = 0; I <pagecount; I ++) <br/>{</P> <p> cout <"reference page: "<PC [I] <Endl; <br/> If (! Travel (BC, blockcount, PC [I]) <br/>{< br/> if (I <blockcount) <br/>{< br/> BC [I] = pc [I]; <br/>}< br/> else <br/>{< br/> If (P = blockcount) <br/>{< br/> P = 0; <br/>}< br/> BC [p] = pc [I]; <br/> P ++; </P> <p >}< br/> nopage ++; <br/> cout <"physical fast:"; <br/> Print (BC, blockcount); <br/>}< br/> cout <Endl; <br/>}< br/> cout <"page missing count: "<nopage <Endl; <br/> cout <" page missing rate: "<(float) nopage/pagecount <Endl; <br/>}</P> <p> int foundma Xnum (int A [], int N) <br/>{< br/> int K, J; <br/> K = A [0]; <br/> J = 0; <br/> for (INT I = 0; I <n; I ++) <br/>{< br/> if (a [I]> = k) <br/>{< br/> K = A [I]; <br/> J = I; <br/>}< br/> return J; <br/>}</P> <p> void LRU (int pc [], int BC [], int pagecount, int blockcount) <br/>{ <br/> cout <"1: LRU replacement algorithm" <Endl; <br/> If (pagecount <= blockcount) <br/> {<br/> cout <"number of missing pages is" <0 <Endl; <br/> cout <"page missing rate:" <0 <Endl; <br/>}< br/> else <br/> {< Br/> int nopage = 0; <br/> int I, j, M; <br/> int * BC1 = new int [blockcount]; <br/> for (I = 0; I <blockcount; I ++) <br/>{< br/> BC1 [I] = 0; <br/>}< br/> for (I = 0; I <pagecount; I ++) <br/>{< br/> cout <"reference page: "<PC [I] <Endl; <br/> If (! Travel (BC, blockcount, PC [I]) <br/>{< br/> if (I <blockcount) <br/> {<br/> BC [I] = pc [I]; <br/> for (INT p = 0; P <= I; P ++) <br/>{< br/> BC1 [p] ++; <br/>}< br/> else <br/> {<br/> for (j = 0; j <blockcount; j ++) <br/>{< br/> BC1 [J] ++; <br/>}< br/> int K = foundmaxnum (BC1, blockcount ); <br/> BC [k] = pc [I]; <br/> BC1 [k] = 1; </P> <p >}< br/> nopage ++; <br/> cout <"physical fast:"; <br/> Print (BC, blockcount); <br/>}< br/> else if (travel (BC, Bloc Kcount, PC [I]) <br/>{< br/> if (I <blockcount) <br/>{< br/> for (j = 0; j <= I; j ++) <br/>{< br/> BC1 [J] ++; <br/>}< br/> for (m = 0; m <= I; m ++) <br/> {<br/> If (BC [m] = pc [I]) <br/>{< br/> break; <br/>}< br/> BC1 [m] = 1; <br/> BC [m] = pc [I]; </P> <p >}< br/> else <br/> {<br/> for (j = 0; j <blockcount; j ++) <br/>{< br/> BC1 [J] ++; <br/>}< br/> for (m = 0; m <blockcount; m ++) <br/>{< br/> If (BC [m] = pc [I]) <br/>{< br/> break; <br/>}< B R/> BC1 [m] = 1; <br/> BC [m] = pc [I]; <br/>}< br/> cout <Endl; <br/>}< br/> cout <"page missing count: "<nopage <Endl; <br/> cout <" page missing rate: "<(float) nopage/pagecount <Endl; <br/> Delete BC1; <br/>}</P> <p> void optiomal (int pc [], int BC [], int pagecount, int blockcount) <br/> {<br/> cout <"2: Optimal Replacement Algorithm" <Endl; <br/> If (pagecount <= blockcount) <br/> {<br/> cout <"number of missing pages is" <0 <Endl; <br/> cout <"page missing rate:" <0 <Endl; <br/>}< br/> el Se <br/> {<br/> int nopage = 0; <br/> int I, j, k; <br/> for (I = 0; I <pagecount; I ++) <br/>{< br/> cout <"reference page:" <PC [I] <Endl; <br/> If (! Travel (BC, blockcount, PC [I]) <br/>{< br/> if (I <blockcount) <br/>{< br/> BC [I] = pc [I]; <br/>}< br/> else <br/>{< br/> int max = 0; <br/> int blockindex ;; <br/> for (j = 0; j <blockcount; j ++) <br/>{< br/> for (k = I; k <pagecount; k ++) <br/>{< br/> If (BC [J] = pc [k]) <br/>{< br/> break; <br/>}< br/> If (k> = max) <br/>{< br/> max = K; <br/> blockindex = J; <br/>}< br/> BC [blockindex] = pc [I]; </P> <p >}< br/> nopage ++; <br/> cou T <"physical fast:"; <br/> Print (BC, blockcount); <br/>}< br/> cout <Endl; <br/>}< br/> cout <"page missing count:" <nopage <Endl; <br/> cout <"page missing rate: "<(float) nopage/pagecount <Endl; <br/>}</P> <p> void NRU (int pc [], int BC [], int pagecount, int blockcount) <br/>{< br/> cout <"3: clock replacement algorithm" <Endl; <br/> If (pagecount <= blockcount) <br/> {<br/> cout <"number of missing pages" <0 <Endl; <br/> cout <"page missing rate:" <0 <Endl; <br/>}< br/> else <br/>{< br/> int Nopage = 0; <br/> int I, j; <br/> int * BC1 = new int [blockcount]; <br/> for (I = 0; I <blockcount; I ++) <br/>{< br/> BC1 [I] = 0; <br/>}< br/> for (I = 0; I <pagecount; I ++) <br/>{< br/> cout <"reference page:" <PC [I] <Endl; <br/> If (! Travel (BC, blockcount, PC [I]) <br/>{< br/> for (j = 0; j <blockcount; j ++) <br/>{< br/> If (BC1 [J] = 1) <br/>{< br/> BC1 [J] = 0; <br/>}< br/> else if (BC1 [J] = 0) <br/>{< br/> break; <br/>}< br/> If (j = blockCount-1) <br/> {<br/> J =-1; <br/>}< br/> BC [J] = pc [I]; <br/> BC1 [J] = 1; <br/> nopage ++; <br/> cout <"physical fast:"; <br/> Print (BC, blockcount ); <br/>}< br/> cout <Endl; <br/>}< br/> cout <"number of missing pages:" <nopage <Endl; <br/> cout <"page missing rate:" <(float) nopage/pagecount <Endl; <br/> Delete BC1; <br/>}< br/> int main () <br/>{< br/> int pagecount, blockcount, I; <br/> cout <"Number of input pages" <Endl; <br/> CIN> pagecount; <br/> int * Pc = new int [pagecount]; <br/> cout <"enter page to" <Endl; <br/> for (I = 0; I <pagecount; I ++) <br/>{< br/> CIN> PC [I]; <br/>}< br/> cout <"Number of input physical blocks" <Endl; <br/> CIN> blockcount; <br/> cout <"0: FIFO replacement algorithm" <Endl; <br/> cout <"1: LRU replacement algorithm "<Endl; <br/> cout <" 2: Optimal Replacement Algorithm "<Endl; <br/> cout <" 3: clock replacement algorithm "<Endl; <br/> cout <" select algorithm category by number: "<Endl; <br/> int N; <br/> while (CIN> N) <br/> {<br/> If (n = 0) <br/>{< br/> int * BC = new int [blockcount]; <br/> FIFO (PC, BC, pagecount, blockcount ); <br/> Delete BC; <br/>}< br/> else if (n = 1) <br/>{< br/> int * BC = new int [blockcount]; <br/> LRU (PC, BC, pagecount, blockcount ); <br/> Delete BC; <br/>}< br/> else if (n = 2) <br/>{< br/> int * BC = new int [blockcount]; <br/> optiomal (PC, BC, pagecount, blockcount ); <br/> Delete BC; <br/>}< br/> else if (n = 3) <br/> {<br/> int * BC = new int [blockcount]; <br/> for (I = 0; I <blockcount; I ++) <br/>{< br/> BC [I] =-1; <br/>}< br/> NRU (PC, BC, pagecount, blockcount ); <br/> Delete BC; <br/>}< br/> else break; <br/>}< br/> Delete PC; <br/> return 0; <br/>}< br/>

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.