Top Banner
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
33

Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Apr 14, 2018

Download

Documents

vanque
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 2: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 3: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 3

Virtual Memory Larger Than Physical Memory

Page 4: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 5: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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!

Page 6: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 6

Page Table When Some Pages Are Not in Main Memory

Page 7: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 8: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 8

Steps in Handling a Page Fault

Page 9: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 10: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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.

Page 11: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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)

Page 12: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 12

Need For Page Replacement

Page 13: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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.

Page 14: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 14

Page Replacement

Page 15: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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.

Page 16: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 16

Graph of Page Faults Versus The Number of Frames

Page 17: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 17

First-In-First-Out (FIFO) Page replacement

!   What happens when we have 4 frames?

Page 18: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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!

Page 19: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 19

FIFO Illustrating Belady’s Anamoly

Page 20: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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!

Page 21: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 21

Optimal Page Replacement

Page 22: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 23: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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!

Page 24: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 25: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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.

Page 26: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 27: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 28: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 29: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Strategies for Thrashing Prevention

!   Working-set strategy

!   Page-fault frequency scheme

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 29

Page 30: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

Silberschatz / OS Concepts / 6e - Chapter 10 Virtual Memory Slide 30

Working-Set Model

Page 31: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 32: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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

Page 33: Virtual Memory (Ch.10) - ETIC UPFrramirez/os/L06.pdfLeast Frequently Used (LFU) Algorithm: replaces page with smallest count. ! Problem: A page used heavily in the initial phase of

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