Operating System-page replacement algorithm (FIFO, OPT, LRU)

Source: Internet
Author: User

Use the FIFO, OPT, and LRU replacement algorithms to simulate the page replacement process. (For Linux and Windows)
Input: 3 // page Frames

7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 // pending page
Output: Changes of frames and the number of page errors during Page Replacement
[Cpp]
# Include <iostream>
Using namespace std;
Int input [20] = };
Class page
{
Public:
Int num;
Int mark;
Page ()
{
Num = 0;
Mark = 21;
}
};
Void FIFO ()
{
Cout <"------ FIFO -----------" <endl;
Int error = 0;
Page frame [3]; // page frame
For (int I = 0; I <3; I ++) // process the first three references
{
Frame [I]. num = input [I];
Error ++;
Cout <frame [I]. num <"| ";
For (int j = 0; j <= I; j ++)
Cout <frame [j]. num <'';
Cout <endl;
}
For (int I = 3; I <20; I ++)
{
Int j;
For (j = 0; j <3; j ++)
If (input [I] = frame [j]. num)
{
Cout <input [I] <endl;
Break;
}
If (j = 3)
{
Error ++;
Frame [(error-1) % 3)]. num = input [I]; // Replace the oldest page
Cout <input [I] <"| ";
For (int k = 0; k <3; k ++)
Cout <frame [k]. num <'';
Cout <endl;
}
}
Cout <"Frame Error:" <error <endl;
}
Void OPT ()
{
Cout <"------ OPT ------------" <endl;
Int error = 0;
Page frame [3];
For (int I = 0; I <3; I ++) // process the first three references
{
Frame [I]. num = input [I];
Error ++;
Cout <frame [I]. num <"| ";
For (int j = 0; j <= I; j ++)
Cout <frame [j]. num <'';
Cout <endl;
}
For (int I = 3; I <20; I ++)
{
Int j;
For (j = 0; j <3; j ++)
If (input [I] = frame [j]. num)
{
Cout <input [I] <endl;
Break;
}
If (j = 3)
{
Error ++;
For (j = 0; j <3; j ++)
{
Frame [j]. mark = 21;
For (int k = 20; k> = I; k --) // traverse backward to find the page that is not used for the longest time
{
If (frame [j]. num = input [k])
Frame [j]. mark = k;
}
}
If (frame [0]. mark> frame [1]. mark & frame [0]. mark> frame [2]. mark)
Frame [0]. num = input [I];
Else if (frame [1]. mark> frame [0]. mark & frame [1]. mark> frame [2]. mark)
Frame [1]. num = input [I];
Else
Frame [2]. num = input [I];
Cout <input [I] <"| ";
For (int k = 0; k <3; k ++)
Cout <frame [k]. num <'';
Cout <endl;
}
}
Cout <"Frame Error:" <error <endl;
}
Void LRU ()
{
Cout <"------ LRU ------------" <endl;
Int error = 0;
Page frame [3];
For (int I = 0; I <3; I ++) // process the first three references
{
Frame [I]. num = input [I];
Error ++;
Cout <frame [I]. num <"| ";
For (int j = 0; j <= I; j ++)
Cout <frame [j]. num <'';
Cout <endl;
}
For (int I = 3; I <20; I ++)
{
Int j;
For (j = 0; j <3; j ++)
If (input [I] = frame [j]. num)
{
Cout <input [I] <endl;
Break;
}
If (j = 3)
{
Error ++;
For (j = 0; j <3; j ++)
{
Frame [j]. mark = 0;
For (int k = 0; k <= I; k ++) // traverse forward to find the least recently used
{
If (frame [j]. num = input [k])
Frame [j]. mark = k;
}
}
If (frame [0]. mark <frame [1]. mark & frame [0]. mark <frame [2]. mark)
Frame [0]. num = input [I];
Else if (frame [1]. mark <frame [0]. mark & frame [1]. mark <frame [2]. mark)
Frame [1]. num = input [I];
Else
Frame [2]. num = input [I];
Cout <input [I] <"| ";
For (int k = 0; k <3; k ++)
Cout <frame [k]. num <'';
Cout <endl;
}
}
Cout <"Frame Error:" <error <endl;
}
Int main ()
{
FIFO ();
OPT ();
LRU ();
}

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.