Algorithm-Operating System-three page replacement Algorithms

Source: Internet
Author: User
Algorithm-Operating System-three types of page replacement algorithms-general Linux technology-Linux programming and kernel information. For more information, see the following. Algorithm serialization (7)-three page replacement algorithms of the Operating System
Author: From: Reading times: 58033 [large, small]


1. problem description and Design Concept: when a process is running, if the page to be accessed is not in the memory, it needs to be transferred to the memory, but the memory has no free space, to ensure that the process can run normally, the system must call up one page of program or data from the memory and send it to the swap area of the disk. But which page should be called up, so it needs to be determined according to certain algorithms. The following are the design ideas of the three algorithms.
OPTIMAL: The best replacement algorithm. The page it chooses will never be used in the future, or will not be accessed in the longest (future) period.
FIFO: the first-in-first-out replacement algorithm. This algorithm always removes the page that first enters the memory, and selects the page that stays in the memory for the longest time to be eliminated.
LRU: the replacement algorithm has not been used for the last time. This algorithm assigns an access field to each page to record the time T that a page has experienced since its last access. When a page needs to be deleted, select the biggest value of tin the existing page for elimination.


# Include

# Define Bsize 3
# Define Psize 20

Struct pageInfor
{
Int content; // page number
Int timer; // access tag
};

Class HPA
{
Public:
UPA (void );
Int findSpace (void); // you can check whether idle memory exists.
Int findExist (int curpage); // you can check whether the page exists in the memory.
Int findReplace (void); // find the page to be replaced
Void display (void); // display
Void FIFO (void); // FIFO Algorithm
Void LRU (void); // LRU Algorithm
Void Optimal (void); // OPTIMAL Algorithm
Void BlockClear (void); // BLOCK recovery
PageInfor * block; // physical block
PageInfor * page; // page number string

Private:

};

Panel: Panel (void)
{
Int QString [20] = };

Block = new pageInfor [Bsize];
For (int I = 0; I {
Block . Content =-1;
Block. Timer = 0;
}

Page = new pageInfor [Psize];
For (I = 0; I {
Page. Content = QString;
Page. Timer = 0;
}
}

Int upa: findSpace (void)
{
For (int I = 0; I If (block. Content =-1)
Return I; // find the idle memory and return the position in the BLOCK.
Return-1;
}

Int upa: findExist (int curpage)
{

For (int I = 0; I If (block. Content = page [curpage]. content)
Return I; // find the page in the memory and return the location in the BLOCK.

Return-1;
}

Int upa: findReplace (void)
{
Int pos = 0;

For (int I = 0; I If (block. Timer> = block [pos]. timer)
Pos = I; // find the page to be replaced and return the position in the BLOCK.
Return pos;
}

Void upa: display (void)
{


For (int I = 0; I If (block. Content! =-1)
Cout < . Content <"";
Cout < }


Void upa: Optimal (void)
{
Int exist, space, position;

For (int I = 0; I {
Exist = findExist (I );
If (exist! =-1)
{Cout <"no page missing" < Else
{
Space = findSpace ();
If (space! =-1)
{
Block [space] = page ;
Display ();
}
Else
{
For (int k = 0; k For (int j = I; j {
If (block [k]. content! = Page [j]. content)
{Block [k]. timer = 1000;} // it will not be used in the future. set TIMER to a large number.
Else
{
Block [k]. timer = j;
Break;
}
}
Position = findReplace ();
Block [position] = page;
Display ();
}
}
}
}

Void upa: LRU (void)
{
Int exist, space, position;

For (int I = 0; I {
Exist = findExist (I );
If (exist! =-1)
{
Cout <"no page missing" < Block [exist]. timer =-1; // restore the existing and accessed BLOCK. The page TIMER is-1.
}
Else
{
Space = findSpace ();
If (space! =-1)
{
Block [space] = page;
Display ();
}
Else
{
Position = findReplace ();
Block [position] = page;
Display ();
}
}
For (int j = 0; j Block [j]. timer ++;
}
}

Void upa: FIFO (void)
{

Int exist, space, position;

For (int I = 0; I {
Exist = findExist (I );
If (exist! =-1)
{Cout <"no page missing" <
Else
{
Space = findSpace ();
If (space! =-1)
{
Block [space] = page;
Display ();
}
Else
{
Position = findReplace ();
Block [position] = page;
Display ();
}
}
For (int j = 0; j Block [j]. timer ++; // All pages in the BLOCK: TIMER ++
}
}

Void upa: BlockClear (void)
{
For (int I = 0; I {
Block. Content =-1;
Block. Timer = 0;
}
}


Void main (void)
{
Cout <"| ---------- page change calculation method ---------- |" < Cout <"| --- power by zhanjiantao (028054115) --- |" < Cout <"| ------------------------------------- |" < Cout <"reference string of the page number:, 0," < Cout <"----------------------------------------------------" < Cout <"select <1> apply Optimal algorithm" < Cout <"select <2> apply FIFO algorithm" < Cout <"select <3> apply LRU algorithm" < Cout <"select <0> exit" < Int select;
Panel test;


While (select)
{
Cin> select;
Switch (select)
{
Case 0:
Break;
Case 1:
Cout <"the Optimal algorithm result is as follows:" < Test. Optimal ();
Test. BlockClear ();
Cout <"----------------------" < Break;
Case 2:
Cout <"FIFO algorithm Result:" < Test. FIFO ();
Test. BlockClear ();
Cout <"----------------------" < Break;
Case 3:
Cout <"LRU algorithm Result:" < Test. LRU ();
Test. BlockClear ();
Cout <"----------------------" < Break;
Default:
Cout <"enter the correct function number" < Break;
}

}

}
Release date: 2005-4-15

[Last edited by frog on]

Contact Us

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.