virtual memory (ch.10) - etic upfrramirez/os/l06.pdfleast frequently used (lfu) algorithm: replaces...
TRANSCRIPT
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 1
Virtual Memory (Ch.10)
! Background ! Demand Paging ! Page Faults ! Page Replacement ! Page Replacement Algorithms ! Thrashing ! Strategies for Thrashing Prevention
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 2
Background ! Virtual memory – Memory management
technique –virtualizes memory (RAM and disk) l Only part of the program needs to be in memory for
execution (error routines, portions of arrays, lists, etc.) l Logical address space can therefore be much larger than
physical address space. l Allows address spaces to be shared by several processes.
! Virtual memory can be implemented via: l Demand paging l Demand segmentation
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 3
Virtual Memory Larger Than Physical Memory
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 4
Demand Paging ! Bring a page into memory from secondary storage
only when it is needed l Less I/O needed l Less memory needed l Faster response l More users
! Page is needed ⇒ reference to it l invalid reference ⇒ abort l not-in-memory ⇒ bring to memory
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 5
Valid-Invalid Bit ! With each page table entry a valid–invalid bit is associated
(1 ⇒ in-memory, 0 ⇒ not-in-memory) ! Initially valid–invalid bit is set to 0 on all entries. ! Example of a page table snapshot.
! During address translation, if valid–invalid bit in page table entry is 0 ⇒ page fault.
1!1!1!1!0
0!0!
!
Frame #! valid-invalid bit!
page table!
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 6
Page Table When Some Pages Are Not in Main Memory
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 7
Page Fault ! If there is ever a reference to a page, first reference
will trap to OS ⇒ page fault ! OS looks at another table (in PCB) to decide:
l Invalid reference ⇒ abort. l Just not in memory.
! Get empty frame. ! Copy page into frame. ! Reset tables, validation bit = 1. ! Restart instruction that was interrupted
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 9
Paging Issues ! Pure demand paging
l Never bring a page into memory until it is required l Can lead to unacceptable system performance
particularly when a page fault results in the access of several pages (one for instructions and several for data)
l Analysis has shown that programs tend to have locality of reference which results in reasonable performance
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 10
What happens if there is no free frame?
! Page replacement – find some page in memory, but not really in use, swap it out. l algorithm l performance – want an algorithm which will result in
minimum number of page faults.
! Same page may be brought into memory several times.
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 11
Demand Paging Example
! Memory access time = 1 µs (microsecond) ! Swap Page Time = 10 ms = 10000 µs ! Page-fault rate = p ! 50% of the time the page that is being replaced has
been modified and therefore needs to be swapped out.
EAT = (1 – p) × 1 + p × 15000 ≈ 1 + 15000p (in µs)
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 13
Basic Page Replacement 1. Find the location of the desired page on disk. 2. Find a free frame:
- If there is a free frame, use it. - If there is no free frame, use a page replacement algorithm to select a victim frame.
3. Read the desired page into the (newly) free frame. Update the page and frame tables.
4. Restart the process. ! Use modify (dirty) bit to reduce overhead of page transfers –
only modified pages are written to disk. ! Page replacement completes separation between logical
memory and physical memory – large virtual memory can be provided on a smaller physical memory.
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 15
Page Replacement Algorithms ! Want lowest page-fault rate. ! Evaluate algorithm by running it on a particular
string of memory references (reference string) and computing the number of page faults on that string.
! In the next examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 16
Graph of Page Faults Versus The Number of Frames
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 17
First-In-First-Out (FIFO) Page replacement
! What happens when we have 4 frames?
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 18
First-In-First-Out (FIFO) Algorithm ! Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 ! 3 frames (3 pages can be in memory at a time per
process) ! 4 frames
! This is an example of Belady’s anomaly – more faults with more frames, opposite is what would think is true
1!
2!
3!
1!
2!
3!
4!
1!
2!
5!
3!
4!
9 page faults!
1!
2!
3!
1!
2!
3!
5!
1!
2!
4!
5! 10 page faults!
4!4! 3!
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 19
FIFO Illustrating Belady’s Anamoly
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 20
Optimal Algorithm ! Replace page that will not be used for longest period of time. ! 4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
! How do you know this? Requires future knowledge! ! Used as yardstick to evaluate how well other algorithms
perform.
1!
2!
3!
4!
6 page faults!
4! 5!
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 22
Least Recently Used (LRU) Algorithm ! The optimal algorithm uses the time when a page
is to be used next ! FIFO uses the time when a page was brought into
memory ! LRU – use the recent past as an approximation of
the near future. Replace the page that has not been used for the longest period of time
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 23
Least Recently Used (LRU) Algorithm ! Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
! Counter implementation l Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter. l When a page needs to be changed, look at the counters to
determine which one is the smallest value and use that frame.
1!
2!
3!
5!
4!
4! 3!
5!
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 24
LRU Page Replacement
Note: Optimal and LRU replacement do not suffer from Belady’s anomaly
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 25
Counting-Based Page Replacement ! Keep a counter of the number of references that have been
made to each page. ! Least Frequently Used (LFU) Algorithm: replaces page
with smallest count. l Problem: A page used heavily in the initial phase of a process will have a
large count and remain in memory even though it may not be used again l Solution: Age pages as they remain in memory by shifting the counts 1 bit
to the right
! Most Frequently Used (MFU) Algorithm: based on the argument that the page with the smallest count was probably just brought in and has yet to be used.
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 26
Global vs. Local Allocation ! Global replacement – process selects a replacement
frame from the set of all frames; one process can take a frame from another. l A process is no longer in control of its page-fault rate l Usually results in greater system throughput and its
therefore more commonly used
! Local replacement – each process selects from only its own set of allocated frames. l Does not take advantage of less used pages in other
processes which could improve overall system throughput
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 27
Thrashing ! If a process does not have “enough” pages, the page-
fault rate is very high. This leads to: l low CPU utilization. l operating system thinks that it needs to increase the
degree of multiprogramming. l another process added to the system to increase
multiprogramming
! Thrashing ≡ a process is busy swapping pages in and out rather than executing
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 28
Locality Model
! Locality model l A locality is a set of pages that are actively used together. A program is
generally composed of several different localities which may overlap l Locality is defined by the program structure and its data structures l As a process executes it migrates from one locality to another (e.g.
subroutine) ! Why does thrashing occur? size of locality > number of frames
allocated
Strategies for Thrashing Prevention
! Working-set strategy
! Page-fault frequency scheme
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 29
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 31
Working-Set Model ! Δ ≡ working-set window ≡ a fixed number of page references
Example: 10000 instructions ! WSSi (working set of Process Pi) = approximation of the program’s
locality - total number of pages referenced in the most recent Δ (varies in time) l if Δ too small will not encompass entire locality. l if Δ too large will encompass several localities. l if Δ = ∞ ⇒ will encompass entire program lifetime.
! D = Σ WSSi ≡ total demand for frames, m ≡ number of frames ! if D > m ⇒ Thrashing ! Policy: if D > m, then suspend one of the processes, writing its
frames to disk, and reallocate the frames to another process ! Working set strategy prevents thrashing while keeping the
degree of multiprogramming as high as possible
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 32
Other Considerations ! Prepaging - prevent the high level of initial paging by bringing
into memory at one time all the pages that will be needed l Use the working set whenever swap a process resumes
! Page size selection l Fragmentation – bigger page more wasted space l table size – 4,096 pages of 1KB or 512 pages of 8KB l Locality – smaller page size allows each page to match program
locality more accurately, better resolution, but larger page generates less page faults
Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 33
Other Considerations (Cont.) ! Program structure
l int A[][] = new int[1024][1024]; l Each row is stored in one page l Program 1 for (j = 0; j < A.length; j++)
for (i = 0; i < A.length; i++) A[i,j] = 0;
1024 x 1024 page faults
l Program 2 for (i = 0; i < A.length; i++) for (j = 0; j < A.length; j++) A[i,j] = 0;
1024 page faults