Because the program may be much larger than memory, you need to introduce virtual memory. The basic idea is that each program has its own address space, which is divided into chunks, each of which is called a page, each page has a contiguous range of addresses, which are mapped to physical memory, but not all pages must be in memory to run the program. When a program references a portion of the address space in physical memory, the hardware performs the necessary mapping immediately, and when the program references a part of the address space that is not in memory, it is the responsibility of the operating system to load the missing part into physical memory and re-execute the failed instruction. If a page is not mapped, the memory snap-in notices that it is not mapped when it causes the CPU to sink into the operating system, a trap called a fault.
"Reprint" About the operating system page replacement algorithm http://blog.csdn.NET/wanghao109/article/details/13003479
Page replacement: In the process of address mapping, if the page is found in the page that is no longer in memory, it will result in a pages break (page fault). When a page break occurs, the operating system must select one of the pages in memory to move it out of memory to make room for the page that is about to be paged in.
There are four typical permutation algorithms, as follows:
OPT: The best replacement algorithm (optional replacement). Replace the page that has the longest current time for the next visit. The OPT algorithm needs to know the future events of the operating system, which is obviously not possible, only as a criterion for measuring other algorithms.
Analysis: (f indicates that page fault appears when the frame is initially filled)
A. Page 2 is required, there is an idle location in memory, add directly to page 2
B. Requires page 3, there is still idle space in memory, directly join the page 3
C. Requires page 2, in memory already exists Page 2, does not join any page
D. requires page 1, there is still idle space in memory, directly join Page 1
E. Need page 5, page 2 distance between the next visit is 1, Page 3 is the next access distance of 3, page 1 no next visit (distance is infinite), so replace page 1
F. Requires page 2, in memory already exists Page 2, does not join any page
G. Need page 4, page 2,3,5 distance from the next visit is 3,2,1, so replace page 2
H. Requires page 5, there is page 5 in memory, does not change
I. Requires page 3, there is page 3 in memory, does not change
J. Need page 2, page 4,3,5 distance from the next visit is infinite, infinity, 1, at this time in order to replace the page 4
K. Requires page 5, there is page 5 in memory, does not change
L. Requires page 2, there is page 2 in memory, does not change
LRU: Least Recently Used (Least recently used). Replaces the page that was last used at the farthest current distance. Based on the principle of locality: replace the most recently inaccessible page. Performance is closest to opt, but difficult to achieve. You can maintain a stack of access pages or a time label that adds last access to each page, but it is expensive.
Analysis: (f indicates that page fault appears when the frame is initially filled)
A. Page 2 is required, there is an idle location in memory, add directly to page 2
B. Requires page 3, there is still idle space in memory, directly join the page 3
C. Requires page 2, in memory already exists Page 2, does not join any page, here resets 2 's recent history access record
D. requires page 1, there is still idle space in memory, directly join Page 1
E. Requires page 5, page 2,3,1 the distance from the most recent history visit is 2,3,1 (2 of the Access records are reset in step c). So replace Page 3.
F. Requires page 2, memory already exists page 2, do not join any page, reset page 2 access record
G. Need page 4, page 2,5,1 distance from the most recent historical visit is a one-way trip, so replace page 1
H. Requires page 5, memory exists page 5, does not change, reset page 5 access record
I. Requires page 3, page 2,5,4 distance from the most recent historical visit is 3,1,2, so replace page 2
J. Page 2 is required, page 3,5,4 distance from the most recent historical visit is one-way, so replace page 4
K. Requires page 5, there is page 5 in memory, does not change
L. Requires page 2, there is page 2 in memory, does not change
FIFO: First-in-one out, the page is treated as a circular buffer and replaced in a circular fashion. This is the simplest algorithm to implement, and the implicit logic is to replace the page that resides in memory for the longest time. However, because a part of the program or data is used very frequently throughout the lifetime of the program, it causes repeated swap-in.
Analysis:
The policy is extremely simple and is replaced directly if there is an idle location in memory. Otherwise, you can first imagine a pointer, start pointing to the first position, the position of the pointer does not change, each time you need to replace the position of the pointer, and then move the pointer back one (if the pointer is at the end, move to the first position of memory).
Clock: Clocks replacement algorithm, which associates one usage bit to each page frame. When the page is first loaded into memory or is re-accessed, the location will be used at 1. Each time a replacement is needed, the lookup replaces it with the first frame with a bit set to 0. During the scan, if a frame with a bit of 1 is encountered, a position of 0 is used and the scan continues. If the so-called frame uses a bit of 0, the first frame is replaced.
Analysis: (Description: Page: "2 (1)" indicates that page 2 uses a bit of 1. "P (N)" means that the scan pointer points to the nth position)
First, the pointer p points to the first frame position, p (1), each time a substitution occurs (or an alternate in memory where there is an idle position), and the pointer is 1.
A. Page 2 is required, there is also an idle location in memory, add directly to page 2, get 2 (1), P (2).
B. Page 3 is required, there is also an idle location in memory, directly to Page 3, get 2 (1), 3 (1), P (3).
C. Requires page 2, the memory already exists Page 2, does not join any page, and this is not the first time page 2 placement, get 2 (1), 3 (1), P (3).
D. Need page 1, directly join Page 1, get 2 (1), 3 (1), 1 (1), p (1)
E. Requires page 5, the first scan will use the 2,3,1 bits are set to 0, and then replace Page 2, get 5 (1), 3 (0), 1 (0), p (2)
F. Requires page 2, scan to page 3 using a bit of 0, so replace Page 3, get 5 (1), 2 (1), 1 (0), p (3)
G. Need page 4, scan to page 1 using bit 0, so replace page 1, get 5 (1), 2 (1), 4 (1), p (1)
H. Requires page 5, memory exists page 5, does not change, get 5 (1), 2 (1), 4 (1), p (1)
I. Requires page 3, the first scan will use the 5,2,4 bits are set to 0, and then replace to page 5, get 3 (1), 2 (0), 4 (0), p (2)
J. Requires page 2, scan to page 2 with a bit of 0, since 2 is re-accessed, the 2 is used at 1, get 3 (1), 2 (1), 4 (0), P (2), and note that the P pointer does not add 1 due to no page fault. Just reset the use bit (remember: each new page is added with a bit of 1)
K. Requires page 5, the first scan will use 2 position 0, replace page 4, get 3 (1), 2 (0), 5 (1), p (1)
L. Requires page 2, reset page 2 using bit 1, pointer p unchanged, get 3 (1), 2 (1), 5 (1), p (1)
Summarize:
1. Page n does not exist in memory, and when there is an idle position in memory, add page N (1), p plus 1
2. There is no page n in memory, and there is no idle position in memory, substitution N (1), p plus 1
3. There is a page n,,p in memory, and page n is reset to n (1) (regardless of whether the bit is 1 or 0 before page N)
Improved clock algorithm: Set use bit u, modify bit M
1. Has not been accessed recently and has not been modified (u=0,m=0)
2. Recently accessed, unmodified (u=1,m=0)
3. Has not been accessed recently, has been modified (u=0,m=1)
4. Recently accessed, modified (u=1,m=1)
A. Start scanning from the current position of the pointer, do not modify the use bit, replace the first found (u=0,m=0)
B. If a fails to find the first (u=0,m=1) to replace, the bit u will be used to 0 during the scan
C, if B fails, the pointer goes back to the starting position, and all frames use a bit of 0, repeat step a, and if necessary, repeat step b until it is found
Four typical page substitution algorithms in operating system virtual memory (Opt,lru,fifo,clock)