Simulation of page replacement algorithm (best replacement algorithm, first-in-first-out page replacement algorithm, least recently used algorithm, least used replacement algorithm, clock replacement algorithm simulation)

Experiment 3* ** *Simulation of Page Replacement Algorithm*

3.1 Simulation of optimal permutation algorithm

1. Experimental environment (tools, configuration, etc.)

1. Hardware requirements: a computer.

2. Software requirements: Windows operating system, using any programming language, such as c, c++, Java, etc.

2. Experimental content (experimental scheme, experimental steps, design ideas, etc.)

1. Experimental plan: Check the compiled code on the computer to see if it is as expected.

2. Design idea: Simulate a situation where a process with several processes runs in a given number of real pages and uses FIFO or LRU or LRU algorithm to perform page replacement when a page fault occurs. The number of virtual pages can be given in advance (for example, 10), and the page address streams (the length of which can be given in advance, for example, 20 virtual page accesses) accessed by these virtual pages can be randomly generated by the program or saved in advance in the file. It is required that when the program is running, the screen can display the status information during the replacement process and output the page hit rate at the end of the visit.

3. Experimental results and analysis:

1. Algorithm idea: The pages selected for elimination each time will be pages that will never be used in the future, or pages that will not be accessed for the longest time, so as to ensure the lowest page fault rate.

2. The number of virtual pages has been pre-set to 10, and the number of physical blocks allocated by the system for the process in the memory is dynamically input. Regarding the page access address flow sequence, I set two input methods, one is manual input, and the other is random number generation. As shown in the figure below, the number of physical blocks allocated by the system to the process in the memory is 3, and the random number generates the page access address flow sequence:

1, 8, 3, 2, 0, 5, 2, 2, 6, 4, 9, 5:

insert image description here

Figure 1 Random number generation page access address flow sequence

3. The status information during the replacement process after the page access address flow sequence is generated by random numbers is shown in the figure below. The page hit rate at the end of the output access is 25%, and the page fault rate is 75%.

insert image description here

Figure 2 Status information during the replacement process

4. Manually input the page access address flow sequence 1 8 3 2 0 5 2 2 6 4 9 5, the result is shown in the figure below, the page hit rate at the end of the output access is 25%, and the page fault rate is 75%

insert image description here

Figure 3 The result of manually entering the page access address flow sequence

  1. Analysis: Regardless of whether manual input or random number generation of page access address flow sequences is selected during the entire process, as long as the page access address flow sequences are the same, the results will be the same. During the replacement process, the number of replacements occurred 6 times. The number of pages is 9, the total number of visits to virtual pages is 12, the page fault rate: 9/12 * 100% = 75%, the hit rate is 25%

  2. Problems encountered in the experiment:

    (1) The biggest difficulty is how to determine which pages will never be used in the future, or will not be accessed for the longest time

    (2) Because of a wrong understanding of some functions of the list in python, an error is caused

7. Solution:

(1) Traverse the subsequent address flow sequence and use the if statement to find out which pages will never be used in the future, and when judging which page has been unused for the longest time, find the node that is farthest unused, as shown in the following figure:
insert image description here

(2) Query books and materials to supplement basic knowledge.

5. Attached flow chart and source program

1. The flow chart is shown in the figure below:

insert image description here

Figure 5 OPT flow chart

3.2 Simulation of FIFO page replacement algorithm

1. Experimental content
1. Algorithm idea: The page that is selected and eliminated each time is the first page that enters the memory.
2. The number of virtual pages has been pre-set to 10, and the number of physical blocks allocated by the system for the process in the memory is dynamically input. Regarding the page access address flow sequence, I set two input methods, one is manual input, and the other is random number generation. As shown in the figure below, the number of physical blocks allocated by the system to the process in the memory is 3, and the random number generates a page access address flow sequence: 5, 9, 6, 2, 1, 6, 0, 0, 0, 2, 1 , 7
insert image description here

Figure 1 Random number generation page access address flow sequence

3. The state information during the replacement process after the page access address flow sequence is generated by random numbers is shown in the figure below. The page hit rate at the end of the output access is 41.666666666666664 %, and the page fault rate is 58.333333333333336 %
insert image description here

Figure 2 Status information during the replacement process

4. Adopt manual input, select the same page access address flow sequence 1 8 3 2 0 5 2 2 6 4 9 5 as the OPT experiment, the result is shown in the figure below, the page hit rate at the end of the output access is 16.6666666666666664 %, the page fault rate 83.33333333333334 %
insert image description here

Figure 3 The result of manually entering the page access address flow sequence

5. Analysis: The same page access address flow sequence is selected through manual input and OPT experiment, and the process is different. The hit rate of OPT is 25%, which is higher than FIFO
insert image description here

2. Flowchart
insert image description here

3.3 Simulation of the least recently used algorithm

1. Experimental content
1. Algorithm idea: The pages eliminated each time are the pages that have not been used for the longest time recently.
2. The number of virtual pages has been pre-set to 10, and the number of physical blocks allocated by the system for the process in the memory is dynamically input. Regarding the page access address flow sequence, I set two input methods, one is manual input, and the other is random number generation. As shown in the figure below, the number of physical blocks allocated by the system to the process in the memory is 3, and the random number generates a page access address flow sequence:
0, 5, 3, 6, 4, 9, 8, 8, 5, 5, 2 , 9
insert image description here

Figure 1 Random number generation page access address flow sequence

3. The status information during the replacement process after the page access address flow sequence is generated by random numbers is shown in the figure below. The page hit rate at the end of the output access is 16.666666666666664 %, and the page fault rate is 83.33333333333334 %
insert image description here

Figure 2 Status information during the replacement process

4. Adopt manual input, select the same page access address flow sequence 1 8 3 2 0 5 2 2 6 4 9 5 as the OPT experiment, the result is shown in the figure below, the page hit rate at the end of the output access is 16.6666666666666664 %, the page fault rate 83.33333333333334 %
insert image description here

Figure 3 The result of manually entering the page access address flow sequence

5. Analysis: The same page access address flow sequence is selected through manual input and OPT experiment, and the process is different. The hit rate of OPT is 25%, which is higher than that of LRU. From Figure 3 and Figure 5, it can be seen that the LRU images in the first five time periods are the same as OPT, but this is not an inevitable result, because OPT starts from the perspective of "looking backward", that is, according to the usage of each page in the future Judgment, and LRU is "looking forward", judged according to the previous usage of each page, and there is no necessary connection between the past and future direction of the page.
insert image description here

Figure 4 Results of OPT page access address flow sequence

2. Flowchart
insert image description here

3.4 Simulation using least permutation algorithm

1. Experimental content
1. Algorithm idea: LFU (algorithm eliminates data according to the historical access frequency of the data. If the data has been accessed many times in the past, the frequency of future access will be higher. 2. The number of
virtual pages has been pre-set Set to 10, the number of physical blocks allocated by the system to the process in the memory is 3, regarding the page access address flow sequence, here is a dynamic input, such as 1 8 3 2 0 5 2 2 6 4 9 5, the result is shown in the figure below Indicates that -1 indicates that the physical block is empty and there is no page.
insert image description here

Figure 1 Dynamic input page access address flow sequence

3. The state information during the replacement process after the input page access address flow sequence is shown in the figure below (when a hit occurs, the page in the physical block does not change, it will not be displayed, but the number of hits is increased by 1), and the page hit rate at the end of the output access 0.0833333, page fault rate 0.916667
insert image description here

Figure 2 Replacement results

4. Analysis: Each data block of LFU has a reference count. All data blocks are sorted according to the reference count, and data blocks with the same reference count are sorted according to time. The final result is shown in the figure above, with a hit rate of 0.0833333. The page fault rate is 0.916667, which is in line with the expected result.
5. Problems encountered in the experiment:
(1) The difficulty of the experiment is mainly to eliminate data according to the historical access frequency of the data.
(2) Mistakes due to accidentally mistaking the data when calculating the hit rate.
6. Solution:
(1) Each data block of LFU has a reference count, all data blocks are sorted by reference count, and data blocks with the same reference count are sorted by time.
(2) Through debugging, the error was finally found

2. Flowchart
insert image description here

3.5 Simulation of Clock Replacement Algorithm

1. Experimental content
1. Algorithm idea: (1) Simple Clock algorithm: When using a simple Clock algorithm, it is necessary to add an access bit to each page, and then link all pages in the memory into a circular queue. It is convenient for cyclic access. At the beginning, the access bits are all 0, and after being accessed, the access bits are set to 1.
In the process, if the access bit of the page is 0, you can swap it out, if it is 1, change its access bit to 0, and then find the next one. Until the end is found, cycle next time.
(2) Improved clock replacement algorithm: In addition to the access bit, a modification bit is added. The specific replacement rules:
(0,0): Not used or modified recently, the best state!
(0,1): Modified but not used recently, the second considered state
(1,0): Used but not modified, the page may be accessed again
(1,1): Used and modified, the page The page may be accessed again
2. Simple Clock algorithm: The number of virtual pages has been preset to 10, the number of physical blocks allocated by the system to the process in memory is set to 3, and the random number method randomly generates virtual page access The address flow sequence, as shown in the figure: choose 4 simple Clock algorithms, and the randomly generated virtual page access address flow sequence is 9 8 3 7 8 8 5 0 8 3:
insert image description here

Figure 1 Simple Clock algorithm randomly generates access address flow sequence

3. The state information in the replacement process after the page access address flow sequence is generated by random numbers is shown in Figure 2. The page hit rate at the end of the output access is 0.3, and the page fault rate is 0.7
insert image description here

Figure 2 The page replacement result of the simple Clock algorithm

4. Improved Clock algorithm: The number of virtual pages has been pre-set to 10, and the number of physical blocks allocated by the system to the process in the memory is set to 3, and the random number method randomly generates virtual page access address flow sequences, such as As shown in the figure: 5 improved Clock algorithms are selected, and the randomly generated virtual page access address flow sequence is 2 4 8 3 1 8 0 7 2 0:
insert image description here

Figure 3 The improved Clock algorithm randomly generates access address flow sequences

5. The state information in the replacement process after the page access address flow sequence is generated by the improved Clock algorithm random number is shown in Figure 4. The page hit rate at the end of the output access is 0.1, and the page fault rate is 0.9
insert image description here

Figure 4 The page replacement result of the improved Clock algorithm

6. Analysis: Simple clock replacement algorithms only take into account whether a page has been visited recently. In fact, if the evicted page has not been modified, there is no need to perform I/O operations to write back to external memory. Only when the eliminated pages are modified and obsolete, do they need to be written back to external storage. Therefore, in addition to considering whether a page has been accessed recently, the operating system should also consider whether the page has been modified. When other conditions are the same, pages that have not been modified should be eliminated first to avoid I/O operations. This is the improved Clock algorithm.
2. Flowchart
insert image description here

If you need experimental source code, please comment in the comment area

Guess you like

Origin blog.csdn.net/m0_53788135/article/details/125688863