I. Demand analysis
1. Tasks and objectives of programming
Through this experiment, deepen the understanding of the concept of virtual memory page replacement, further master the FIFO, the best replacement OPI and the most recently unused LRU page replacement algorithm implementation methods.
2. Input form and input value range
Minimum number of physical blocks m, number of pages n, page access sequence P1… Pn, the algorithm selected 1-FIFO, 2-OPI, 3-LRU.
3. The form of output
The number of page misses and the page miss rate for each algorithm.
4. The functions that the program can achieve
The program is designed to simulate the working process of FIFO, OPI and LRU page replacement algorithm. Assuming that the minimum number of physical blocks allocated to each process in memory is M, the number of pages to be accessed during the process is N, and the page access sequence is P1,… Pn, respectively use different page replacement algorithms to schedule the page access sequence of the process, give the replacement process of the page access sequence, and calculate the page absence times and page absence rate of each algorithm.
5, test data, including correct input and output results and contains error input and output results
Correct use cases
The input
MinBlockNum: 4 Number of pages PageNum: 12 Number of page 1 PageOrder[1] : 4 Number of page 2 PageOrder[2] : 3 Number of page 3 PageOrder[3] : PageOrder[4] : 1 Page5 PageOrder[5] : 4 Page6 PageOrder[6] : 3 Page7 PageOrder[7] : 5 Page8 PageOrder[8] : PageOrder[9] : 3 Page10 PageOrder[10] : 2 Page11 PageOrder[11] : 1 Page12 PageOrder[12] : 5 Please select the algorithm you want to use first (1-First-in, first-out (FIFO) page replacement algorithm, 2-Best (OPI) page replacement algorithm, 3-most recently unused (LRU) page replacement algorithm) : 1Copy the code
The output
Please select the algorithm you want to use first (1-First-in, first-out (FIFO) page replacement algorithm, 2-Best (OPI) page replacement algorithm, 3-most recently unused (LRU) page replacement algorithm) : 1 you choose is 1 - first in first out (FIFO) page replacement algorithm of page 7 to replace physical block 1 page page 8 replace physical block 2 pages in the page 9 to replace physical block 3 pages in the page 10 replace physical block 4 pages in the page of 11 replace physical block 1 page 12 replace physical block of page 2 The simulation process of page replacement algorithm is as follows: Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page10 Page11 Page12 BlockNum 1 4 4 4 4 4 4 5 5 5 5 1 1 BlockNum 2, 3, 3 3 3 3, 3, 4, 4, 4, 4, 5 BlockNum 3 2 2 2 2 2 2, 3, 3 3 3 BlockNum 4 1 1 1 1 1 1 2 2 2 page replacement algorithm of page one number is: 10 page replacement algorithm page fault rate is: 83%Copy the code
Error use case (page 0 in input, page number set to status quantity)
The input
MinBlockNum: 4 Number of pages PageNum: 12 Number of page 1 PageOrder[1] : 4 Number of page 2 PageOrder[2] : 3 Number of page 3 PageOrder[3] : PageOrder[4] : 1 Page 5 PageOrder[5] : 4 Page 6 PageOrder[6] : 0 Page 7 PageOrder[7] : 5 Page 8 PageOrder[8] : PageOrder[9] : 0 Page10 PageOrder[10] : 2 Page11 PageOrder[11] : 1 Page12 PageOrder[12] : 0 Please select the algorithm you want to use first (1-First-in, first-out (FIFO) page replacement algorithm, 2-Best (OPI) page replacement algorithm, 3-most recently unused (LRU) page replacement algorithm) : 2Copy the code
The output
You have selected 2-optimal (OPI) page replacement algorithm page 6 to replace the page in the second physical block at the farthest distance 100 page 9 to replace the page in the first physical block at the farthest distance 100 page replacement algorithm page 0 at the farthest distance 0 simulation process is as follows: Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page10 Page11 Page12 BlockNum 1 4 4 4 4 4 4 4 4 2 2 2 BlockNum 2 3 3 3 3 5 5 5 5 5 5 BlockNum 3 2 2 2 2 2 2 2 2 2 2 BlockNum 4 1 1 1 1 1 1 1 1 1 page replacement algorithm of page one number is: 9 page replacement algorithm page fault rate is: 75%Copy the code
Second, outline design
Definition of abstract data types
int PageOrder[MaxNumber];// Page sequence
int Simulate[MaxNumber][MaxNumber];// Simulate the process
int PageNum;/ / page number
int MinBlockNum;// Minimum number of physical blocks
int LackNum;/ / pages missing
double LackPageRate;/ / page fault rate
bool found;
int isAlgorithm;// Algorithm selection
Copy the code
2. The flow of the main program
int main(a) {
virtualMemoryPageReplacementAlgorithm virtualMemoryPageReplacementAlgorithm{};
virtualMemoryPageReplacementAlgorithm.Input();
return 0;
}
Copy the code
3. Hierarchical (call) relationship between each program module
int main(a) {
virtualMemoryPageReplacementAlgorithm virtualMemoryPageReplacementAlgorithm{};
virtualMemoryPageReplacementAlgorithm.Input();
return 0;
}
// The InputAlgorithm function is called to select the input function
void Input(a) {
···
InputAlgorithm();
}
// Call IsAlgorithm for algorithm storage validation
void InputAlgorithm(a) {
···
IsAlgorithm();
}
// After the algorithm is confirmed, call different algorithm functions according to the confirmation result. If the confirmation fails, re-enter the algorithm
void IsAlgorithm(a) {...switch (isAlgorithm) {
case 1:
AlgorithmFIFO();
case 2:
AlgorithmOPI();
case 3:
AlgorithmLRU();
default: InputAlgorithm(); }}// the algorithm function only takes FIFO as an example. After the algorithm is processed, Print function is called to output the algorithm result, and NextAlgorithm function is called to ask the NextAlgorithm
void AlgorithmFIFO(a) {... the Print (); NextAlgorithm(); }// Call the algorithm again to confirm the loop process, if trigger the termination condition to end the process
void NextAlgorithm(a) {
···
IsAlgorithm();
}
Copy the code
Third, detailed design
Implement the specific algorithm of the program module
1, first in first out (FIFO) page replacement algorithm
// Call the first-in, first-out (FIFO) page replacement algorithm for scheduling calculations
void AlgorithmFIFO(a) {
// Initialize the number of missing pages
LackNum = 0;
// Define a queue pointer to the next page to be paged out
int pointer = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// If it is the first page, add a page to Simulate[1][I] and the page number is +1
if (i == 1) {...continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing
if (Simulate[j][i - 1] == PageOrder[I]) {···break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {...break; }}// If there is a normal page missing, do the replacement and print the replacement steps
if(! Found) {···}} LackPageRate = (double) LackNum / (double) PageNum;
Print();
NextAlgorithm();
}
Copy the code
2. Optimal (OPI) page replacement algorithm
// Call the optimal (OPI) page replacement algorithm for scheduling calculations
void AlgorithmOPI(a) {
// Initialize the number of missing pages
LackNum = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// Initialize the queue pointer to the next paged page
int pointer = 0;
// Initialize the farthest distance
int distance = 0;
// If it is the first page, add the page directly, the number of missing pages +1
if (i == 1) {...continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing...break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {...break; }}if(! found) {for (int j = 1; j <= MinBlockNum; j++) {
Simulate[j][i] = Simulate[j][i - 1];
// Find the optimal permutation bit
for (int k = i + 1; k <= PageNum; k++) {
if (Simulate[j][i] == PageOrder[k]) {
if (k - i > distance) {
distance = k - i;
pointer = j;
}
break;
}
// Determine that there is no such requirement in the list of pages, and directly use this page as a replacement page
if(k == PageNum && distance < MaxNumber) { distance = MaxNumber; pointer = j; LackPageRate = (double) LackNum / (double) PageNum;
Print();
NextAlgorithm();
}
Copy the code
3. Most recently not used (LRU) page replacement algorithm
// Call the most recently unused (LRU) page replacement algorithm for scheduling calculations
void AlgorithmLRU(a) {
// Initialize the number of missing pages
LackNum = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// Initialize the queue pointer to the next paged page
int pointer = 0;
// Initialize the farthest distance
int distance = 0;
// If it is the first page, add the page directly, the number of missing pages +1
if (i == 1) {...continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing...break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {...break; }}if(! found) {for (int j = 1; j <= MinBlockNum; j++) {
Simulate[j][i] = Simulate[j][i - 1];
// Find the optimal permutation bit
for (int k = i - 1; k > 0; k--) {
if (Simulate[j][i] == PageOrder[k]) {
if (i - k > distance) {
distance = i - k;
pointer = j;
}
break;
}
// Determine that there is no such requirement in the list of pages, and directly use this page as a replacement page
if (k == 1&& distance < MaxNumber) { distance = MaxNumber; pointer = j; LackPageRate = (double) LackNum / (double) PageNum;
Print();
NextAlgorithm();
}
Copy the code
Four, debugging analysis
Problems encountered in the debugging process and solutions, design and implementation review and analysis
The problem | describe | The solution |
---|---|---|
OPI and LRU algorithms deal with the problem with the same priority | When multiple pages in OPI and LRU algorithms are no longer used in the sequence of the page, an infinite loop will be caused due to judgment reasons | Use the if (k == 1 && distance < MaxNumber) conditional statement to solve this problem |
The performance analysis of the algorithm (including the time complexity and space complexity analysis of basic operations and other algorithms) and its improvement idea
Performance analysis
algorithm | Time complexity | Spatial complexity |
---|---|---|
First in, first out (FIFO) page replacement algorithm | T(n) = O(n2) | S(n) = O(n2) |
Optimal (OPI) page replacement algorithm | T(n) = O(n2) | S(n) = O(n2) |
Most recently unused (LRU) page replacement algorithm | T(n) = O(n2) | S(n) = O(n2) |
Print data output | T(n) = O(n) | S(n) = O(n2) |
NextAlgorithm algorithm | T(n) = O(1) | S(n) = O(1) |
Improve vision
In cases where setting page 0 as a state prevents page number 0 from being used, consider designing it as a data structure
5. User instructions
Directions for use
- The console will prompt the user to enter, as prompted to enter the content
- Page 0 cannot be entered (the page number is set to status quantity)
- After the selection algorithm is input, the virtual memory page replacement process and simulation list will be output
- You can choose whether to use other algorithms for virtual memory page replacement or exit as required
6. Test results
Test results, both input and output
MinBlockNum: 4 Number of pages PageNum: 12 Number of page 1 PageOrder[1] : 4 Number of page 2 PageOrder[2] : 3 Number of page 3 PageOrder[3] : PageOrder[4] : 1 Page5 PageOrder[5] : 4 Page6 PageOrder[6] : 3 Page7 PageOrder[7] : 5 Page8 PageOrder[8] : PageOrder[9] : 3 Page10 PageOrder[10] : 2 Page11 PageOrder[11] : 1 Page12 PageOrder[12] : 5 Please select the algorithm you want to use first (1-First-in, first-out (FIFO) page replacement algorithm, 2-Best (OPI) page replacement algorithm, 3-most recently unused (LRU) page replacement algorithm) : 1 you choose is 1 - first in first out (FIFO) page replacement algorithm of page 7 to replace physical block 1 page page 8 replace physical block 2 pages in the page 9 to replace physical block 3 pages in the page 10 replace physical block 4 pages in the page of 11 replace physical block 1 page 12 replace physical block of page 2 The simulation process of page replacement algorithm is as follows: Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page10 Page11 Page12 BlockNum 1 4 4 4 4 4 4 5 5 5 5 1 1 BlockNum 2, 3, 3 3 3 3, 3, 4, 4, 4, 4, 5 BlockNum 3 2 2 2 2 2 2, 3, 3 3 3 BlockNum 4 1 1 1 1 1 1 2 2 2 page replacement algorithm of page one number is: 10 page replacement algorithm page fault rate is: 83% Please ask whether other algorithms need to be carried out, if so, please enter (1-3 values); If not, please input any character (1- FIRST in, First out (FIFO) page replacement algorithm, 2- Best (OPI) page replacement algorithm, 3- Most recently not used (LRU) page replacement algorithm) : 2 You chose 2-best (OPI) page replacement algorithm page 7 replace the page in the fourth physical block at the farthest distance 4 page 11 replace the page in the first physical block at the farthest distance 100 page replacement algorithm simulation process is as follows: Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page10 Page11 Page12 BlockNum 1 4 4 4 4 4 4 4 4 4 4 1 1 BlockNum 3 3 3 3 3 3 3 3 3 3 3 3 3 BlockNum 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 1 1 5 5 5 5 5 5 5 50% is it necessary to perform other algorithms? If yes, please enter (1-3 values); If not, please input any character (1- FIRST in, First out (FIFO) page replacement algorithm, 2- Best (OPI) page replacement algorithm, 3- Most recently not used (LRU) page replacement algorithm) : 3 long ago you choose 3 - most recently used (LRU) page replacement algorithm has 7 4 replace physical block 3 in the farthest distance in the page page 10 longest distance between 6 to replace physical block 4 pages in the page 11 in April farthest distance to replace physical block 3 pages in the page 12 in the farthest distance 4 replace physical block of page 1 The simulation process of page replacement algorithm is as follows: Page 1 Page 2 Page 3 Page 4 Page 5 Page 6 Page 7 Page 8 Page 9 Page10 Page11 Page12 BlockNum 1 4 4 4 4 4 4 4 4 4 4 4 5 BlockNum 2, 3, 3 3 3 3 3 3 3 3 3 3 BlockNum 3 2 2 2 2 5 5 5 5 BlockNum 4 1 1 1 1 1 1 1 1 2 2 2 page replacement algorithm of page one number is: 8 page replacement algorithm page fault rate is: 67% is it necessary to perform other algorithms? If yes, please enter (1-3 values); If no, enter any characters (1- FIFO page replacement algorithm, 2- OPI page replacement algorithm, 3- LRU page replacement algorithm) : 0 Process finished with exit code 0Copy the code
Seven, the appendix
Annotated source programs
#include <iostream>
#include <iomanip>
using namespace std;
#define MaxNumber 100
class virtualMemoryPageReplacementAlgorithm {
public:
int PageOrder[MaxNumber];// Page sequence
int Simulate[MaxNumber][MaxNumber];// Simulate the process
// int PageCount[MaxNumber]; // The distance between the current memory and the next occurrence
int PageNum;/ / page number
int MinBlockNum;// Minimum number of physical blocks
int LackNum;/ / pages missing
double LackPageRate;/ / page fault rate
bool found;
int isAlgorithm;// Algorithm selection
// Enter the number of free partitions, the size of free partitions, the number of processes, and the size of partitions required by the process
void Input(a) {
cout << "Please enter the minimum number of physical blocks MinBlockNum:";
cin >> MinBlockNum;
cout << "Please enter the number of pages PageNum:";
cin >> PageNum;
for (int i = 1; i <= PageNum; i++) {
cout << "Please enter the page" << i << "Number" << "PageOrder[" << i << "]:";
cin >> PageOrder[i];
}
InputAlgorithm();
}
// Get algorithm selection input
void InputAlgorithm(a) {
cout << endl
<< Please select the algorithm you want to use first (1-First-in, first-out (FIFO) page replacement algorithm, 2-Best (OPI) page replacement algorithm, 3-most recently unused (LRU) page replacement algorithm) :;
cin >> isAlgorithm;
IsAlgorithm();
}
// Algorithm store validation
void IsAlgorithm(a) {
switch (isAlgorithm) {
case 1:
cout << endl << "You have selected the 1-first-in, first-out (FIFO) page replacement algorithm." << endl;
AlgorithmFIFO();
break;
case 2:
cout << endl << "You chose the 2-best (OPI) page replacement algorithm." << endl;
AlgorithmOPI();
break;
case 3:
cout << endl << "You have selected 3- Most recently Unused (LRU) page replacement algorithm" << endl;
AlgorithmLRU();
break;
default:
cout << "Algorithm value:" << isAlgorithm
<< "Error, please re-enter the correct algorithm type (1- First in first Out (FIFO) page replacement algorithm, 2- Best (OPI) page replacement algorithm, 3- Most recently unused (LRU) page replacement algorithm)"
<< endl; InputAlgorithm(); }}// Ask if you want to do the rest of the algorithm
void NextAlgorithm(a) {
cout << endl
<< "Do you want to do other algorithms, if yes, please enter (1-3 values); If not, please enter any character (1- FIRST in first Out (FIFO) page replacement algorithm, 2- Best (OPI) page replacement algorithm, 3- Most recently not used (LRU) page replacement algorithm) :";
cin >> isAlgorithm;
if(isAlgorithm ! =1&& isAlgorithm ! =2&& isAlgorithm ! =3) {
return;
}
IsAlgorithm();
}
// Output the page replacement algorithm simulation process and the number of page missing and page missing rate
void Print(a) {
cout << endl << "Page replacement algorithm simulation process is as follows:" << endl;
// Simulate the process
cout << left << setw(10) < <"";
for (int i = 1; i <= PageNum; i++) {
cout << right << setw(8) < <"Page" << setw(2) << i;
}
for (int i = 1; i <= MinBlockNum; i++) {
cout << endl << left << setw(8) < <"BlockNum" << right << setw(2) << i;
for (int j = 1; j <= PageNum; j++) {
if(Simulate[i][j] ! =0) {
cout << right << setw(10) << Simulate[i][j];
} else {
cout << setw(10) < <""; }}}cout << endl << "The number of page missing times in page replacement algorithm is:" << LackNum << endl;
cout << "Page replacement algorithm page miss rate is:" << setprecision(2) << LackPageRate * 100 << "%" << endl;
}
// Call the first-in, first-out (FIFO) page replacement algorithm for scheduling calculations
void AlgorithmFIFO(a) {
// Initialize the number of missing pages
LackNum = 0;
// Define a queue pointer to the next page to be paged out
int pointer = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// If it is the first page, add the page directly, the number of missing pages +1
if (i == 1) {
Simulate[1][i] = PageOrder[i];
LackNum++;
pointer = 1;
continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing
if (Simulate[j][i - 1] == PageOrder[i]) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
found = true;
break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
Simulate[j][i] = PageOrder[i];
LackNum++;
found = true;
break; }}if(! found) {for (int j = 1; j <= MinBlockNum; j++) {
Simulate[j][i] = Simulate[j][i - 1];
}
cout << "Page" << i << "Change the number" << pointer << "Pages in physical Blocks" << endl;
Simulate[pointer][i] = PageOrder[i];
LackNum++;
if (pointer == MinBlockNum) {
pointer = 1;
} else {
pointer++;
}
}
}
LackPageRate = (double) LackNum / (double) PageNum;
Print();
NextAlgorithm();
}
// Call the optimal (OPI) page replacement algorithm for scheduling calculations
void AlgorithmOPI(a) {
// Initialize the number of missing pages
LackNum = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// Initialize the queue pointer to the next paged page
int pointer = 0;
// Initialize the farthest distance
int distance = 0;
// If it is the first page, add the page directly, the number of missing pages +1
if (i == 1) {
Simulate[1][i] = PageOrder[i];
LackNum++;
continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing
if (Simulate[j][i - 1] == PageOrder[i]) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
found = true;
break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
Simulate[j][i] = PageOrder[i];
LackNum++;
found = true;
break; }}if(! found) {for (int j = 1; j <= MinBlockNum; j++) {
Simulate[j][i] = Simulate[j][i - 1];
// Find the optimal permutation bit
for (int k = i + 1; k <= PageNum; k++) {
if (Simulate[j][i] == PageOrder[k]) {
if (k - i > distance) {
distance = k - i;
pointer = j;
}
break;
}
// Determine that there is no such requirement in the list of pages, and directly use this page as a replacement page
if(k == PageNum && distance < MaxNumber) { distance = MaxNumber; pointer = j; }}}cout << "Page" << i << "At the furthest distance" << distance << "Change the number" << pointer << "Pages in physical Blocks" << endl;
Simulate[pointer][i] = PageOrder[i];
LackNum++;
}
}
LackPageRate = (double) LackNum / (double) PageNum;
Print();
NextAlgorithm();
}
// Call the most recently unused (LRU) page replacement algorithm for scheduling calculations
void AlgorithmLRU(a) {
// Initialize the number of missing pages
LackNum = 0;
for (int i = 1; i <= PageNum; i++) {
// Initialize found
found = false;
// Initialize the queue pointer to the next paged page
int pointer = 0;
// Initialize the farthest distance
int distance = 0;
// If it is the first page, add the page directly, the number of missing pages +1
if (i == 1) {
Simulate[1][i] = PageOrder[i];
LackNum++;
continue;
}
for (int j = 1; j <= MinBlockNum; j++) {
// Check whether the page is in the physical block. If so, the page is not missing
if (Simulate[j][i - 1] == PageOrder[i]) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
found = true;
break;
}
// If the page is missing and the physical block is empty, do not replace, directly fill in
if (Simulate[j][i - 1] = =0) {
for (int k = 1; k <= MinBlockNum; k++) {
Simulate[k][i] = Simulate[k][i - 1];
}
Simulate[j][i] = PageOrder[i];
LackNum++;
found = true;
break; }}if(! found) {for (int j = 1; j <= MinBlockNum; j++) {
Simulate[j][i] = Simulate[j][i - 1];
// Find the optimal permutation bit
for (int k = i - 1; k > 0; k--) {
if (Simulate[j][i] == PageOrder[k]) {
if (i - k > distance) {
distance = i - k;
pointer = j;
}
break;
}
// Determine that there is no such requirement in the list of pages, and directly use this page as a replacement page
if (k == 1&& distance < MaxNumber) { distance = MaxNumber; pointer = j; }}}cout << "Page" << i << "At the furthest distance" << distance << "Change the number" << pointer << "Pages in physical Blocks" << endl;
Simulate[pointer][i] = PageOrder[i];
LackNum++;
}
}
LackPageRate = (double) LackNum / (double) PageNum; Print(); NextAlgorithm(); }};int main(a) {
virtualMemoryPageReplacementAlgorithm virtualMemoryPageReplacementAlgorithm{};
virtualMemoryPageReplacementAlgorithm.Input();
return 0;
}
Copy the code