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 ();
}