Chapter 8a: Virtual Memory I Silberschatz, Galvin and Gagne ©2013

advertisement

Chapter 8a: Virtual Memory I

Operating System Concepts Essentials – 2 nd Edition

Silberschatz, Galvin and Gagne ©2013

Chapter 8a: Virtual Memory I

 Background

 Demand Paging

 Copy-on-Write

 Page Replacement

Operating System Concepts Essentials – 2 nd Edition 8.2

Silberschatz, Galvin and Gagne ©2013

Objectives

 Describe benefits of virtual memory

 Explain concepts of:

 Demand paging

 Page-replacement algorithms

 Allocation of page frames

 Discuss principle of working-set model

 Explore how kernel memory is managed

Operating System Concepts Essentials – 2 nd Edition 8.3

Silberschatz, Galvin and Gagne ©2013

Background

 Code must be in memory to execute...

... but entire program rarely used

 Error code

 Unusual routines

 Large data structures

 Entire program code not needed at same time

Operating System Concepts Essentials – 2 nd Edition 8.4

Silberschatz, Galvin and Gagne ©2013

Background

 Execute partially-loaded program

 No longer constrained by physical memory limits

 Program uses less memory while running

 More programs can run at same time

 Increased CPU utilization

 Increased throughput

 No increase in response or turnaround time

 Less I/O needed to load or swap programs into memory

 Each user program runs faster

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.5

Background (Cont.)

 Virtual memory – separates logical (virtual) memory from physical memory

 Only part of must be in memory for execution

 Logical address space can be much larger than physical address space

 Allows shared address spaces

 More efficient process creation

 More programs can run concurrently

 Less I/O needed to load or swap processes

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.6

Background (Cont.)

 Virtual address space – logical view of process stored in memory

 Usually starts at address 0x0

 Contains contiguous addresses

 Physical memory organized into page frames

 MMU maps logical to physical memory

 MMU = Memory Management Unit

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.7

Background (Cont.)

 Virtual memory implemented via:

 Demand paging

 Demand segmentation

Operating System Concepts Essentials – 2 nd Edition 8.8

Silberschatz, Galvin and Gagne ©2013

Virtual Memory That is Larger Than Physical Memory

Operating System Concepts Essentials – 2 nd Edition 8.9

Silberschatz, Galvin and Gagne ©2013

Virtual-address Space

 Logical address space:

 Stack starts at highest logical address, grows “down”

 Heap grows “up”

 Maximizes address space usage

 Holeunused address space between stack and heap

 No physical memory needed until stack or heap grows

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.10

Virtual-address Space

 Enables sparse address space

 Holes filled as needed

 System libraries shared via mapping into virtual address space

 Shared memory by mapping pages read-write into virtual address space

 Pages shared during fork() , speeding process creation

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.11

Shared Library Using Virtual Memory

Operating System Concepts Essentials – 2 nd Edition 8.12

Silberschatz, Galvin and Gagne ©2013

Demand Paging

 Bring page into memory only when it ’s needed

 Less I/O needed

 No unnecessary I/O

 Less memory needed

 Faster response

 More users

 Similar to paging system with swapping

Operating System Concepts Essentials – 2 nd Edition 8.13

Silberschatz, Galvin and Gagne ©2013

Demand Paging

 Page is needed

 reference to it

Invalid reference

 abort

Not-in-memory

 bring to memory

 Lazy swapper – never swap page into memory unless page will be needed

 Swapper that deals with pages is called pager

Operating System Concepts Essentials – 2 nd Edition 8.14

Silberschatz, Galvin and Gagne ©2013

Basic Concepts

 With swapping , pager guesses which pages will be used before swapping out again

 Instead, pager brings in only needed pages into memory

 How to determine set of pages?

 Need new MMU functionality to implement demand paging

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.15

Basic Concepts

 If pages needed are already memory resident

 No difference from non demand-paging

 If page needed and not memory resident

 Need to detect and load page into memory from storage

 Without changing program behavior

 Without programmer needing to change code

Operating System Concepts Essentials – 2 nd Edition 8.16

Silberschatz, Galvin and Gagne ©2013

Valid-Invalid Bit

 Valid –invalid bit associated with each entry in page table

 v => memory resident

 i => not-in-memory

 Initially set to i for all entries

 E.g.,

Operating System Concepts Essentials – 2 nd Edition 8.17

Silberschatz, Galvin and Gagne ©2013

Page Table When Some Pages Are Not in Main Memory

Operating System Concepts Essentials – 2 nd Edition 8.18

Silberschatz, Galvin and Gagne ©2013

Page Fault

 First reference to a page will trap to OS: page fault

1.

OS checks another table to decide:

Invalid reference => abort

Not in memory

2.

Find free frame in physical memory

3.

Swap page into frame via scheduled disk operation

4.

Reset tables to indicate page now in memory

 validation bit = v

5.

Restart instruction that caused page fault

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.19

Steps in Handling a Page Fault

Operating System Concepts Essentials – 2 nd Edition 8.20

Silberschatz, Galvin and Gagne ©2013

Aspects of Demand Paging

 Hardware support needed for demand paging

 Page table with valid / invalid bit

 Secondary memory (device with swap space )

 Instruction restart

Operating System Concepts Essentials – 2 nd Edition 8.21

Silberschatz, Galvin and Gagne ©2013

Aspects of Demand Paging

 Extreme case – start process with no pages in memory

 Pure demand paging

 OS sets instruction pointer to first instruction of process

 Non-memory-resident causes page fault

Operating System Concepts Essentials – 2 nd Edition 8.22

Silberschatz, Galvin and Gagne ©2013

Aspects of Demand Paging

 A given instruction could access multiple pages

 Cause multiple page faults

 E.g., instruction C = A + B

 A, B, and C on different pages

 Overhead decreased because of locality of reference

 Data often close together in memory

Operating System Concepts Essentials – 2 nd Edition 8.23

Silberschatz, Galvin and Gagne ©2013

Stages of Demand Paging

1.

Trap to OS

2.

Save user registers and process state

3.

Determine if interrupt was page fault

4.

Check if page reference was legal, determine location of page on disk

Operating System Concepts Essentials – 2 nd Edition 8.24

Silberschatz, Galvin and Gagne ©2013

Stages of Demand Paging

5.

Issue a read from disk to a free frame:

 Wait in queue for device until read request serviced

 Wait for device seek and/or latency time

 Begin transfer of page to free frame

6.

While waiting, allocate CPU to some other user

7.

Receive interrupt from disk I/O subsystem (done)

8.

Save registers and process state for other user

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.25

Stages of Demand Paging

9.

Determine that interrupt was from disk

10.

Correct the page table and other tables to show page is now in memory

11.

Wait for CPU to be allocated to this process

12.

Restore user registers, process state, and new page table, resume interrupted instruction

Operating System Concepts Essentials – 2 nd Edition 8.26

Silberschatz, Galvin and Gagne ©2013

Performance of Demand Paging

 Three major activities:

1.

2.

3.

Service interrupt – careful coding req’d

Read page from disk

– lots of time

Restart process – small amount of time

Operating System Concepts Essentials – 2 nd Edition 8.27

Silberschatz, Galvin and Gagne ©2013

Performance of Demand Paging

 Page Fault Rate 0

 p

1

 if p = 0, no page faults

 if p = 1, every reference causes fault

 Effective Access Time (EAT)

EAT = (1 – p ) x memory access

+ p (page fault overhead

+ swap page out

+ swap page in )

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.28

Demand Paging Example

 Memory access time = 200 nanoseconds

 Average page-fault service time = 8 milliseconds

 EAT = (1 – p) x 200 + p (8 milliseconds)

= (1 – p) x 200 + p x 8,000,000

= 200 + p x 7,999,800

Operating System Concepts Essentials – 2 nd Edition 8.29

Silberschatz, Galvin and Gagne ©2013

Demand Paging Example

 If p = 0.001, then EAT = 8199.8 nanoseconds

 40.9X slowdown

 If want performance degradation < 10%

 220 > 200 + 7,999,800 x p

 20 > 7,999,800 x p

 => p < .0000025

 less than one page fault in every 400,000 memory accesses

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.30

Demand Paging Optimizations

 Swap space I/O faster than file system I/O (even if on same device)

 Swap allocated in larger chunks

 Less management needed than file system

 Copy entire process image to swap space at process load time

 Page in and out of swap space

 Used in older BSD Unix

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.31

Demand Paging Optimizations

 When free frame, discard rather than page out

 Used in Solaris and current BSD

 Still need to write to swap space

 Pages not associated with a file (like stack and heap) – anonymous memory

 Pages modified in memory but not yet written back to file system

Operating System Concepts Essentials – 2 nd Edition 8.32

Silberschatz, Galvin and Gagne ©2013

Demand Paging Optimizations

 Mobile systems

 Typically don ’t support swapping

 Instead, demand page from file system and reclaim read-only pages (such as code)

Operating System Concepts Essentials – 2 nd Edition 8.33

Silberschatz, Galvin and Gagne ©2013

Copy-on-Write

 Copy-on-Write (COW) allows parent and child processes to initially share same pages in memory

 If either modifies shared page, then page copied

 COW allows more efficient process creation

 Only modified pages copied

Operating System Concepts Essentials – 2 nd Edition 8.34

Silberschatz, Galvin and Gagne ©2013

Copy-on-Write

 In general, free pages allocated from pool of zero-fill-on-demand pages

 Pool should always have free frames for fast demand page execution

 Freeing frames on page faults inefficient

Operating System Concepts Essentials – 2 nd Edition 8.35

Silberschatz, Galvin and Gagne ©2013

Copy-on-Write

 vfork() variation on fork() system call has parent suspend and child using copy-on-write address space of parent

 Designed to have child call exec()

 Very efficient

Operating System Concepts Essentials – 2 nd Edition 8.36

Silberschatz, Galvin and Gagne ©2013

Before Process 1 Modifies Page C

Operating System Concepts Essentials – 2 nd Edition 8.37

Silberschatz, Galvin and Gagne ©2013

After Process 1 Modifies Page C

Operating System Concepts Essentials – 2 nd Edition 8.38

Silberschatz, Galvin and Gagne ©2013

What Happens if There is no Free Frame?

 Used up by process pages

 Pages used by kernel, I/O buffers, etc.

 How many pages to allocate each?

 How to deal with frequently used pages?

Operating System Concepts Essentials – 2 nd Edition 8.39

Silberschatz, Galvin and Gagne ©2013

What Happens if There is no Free Frame?

 Page replacement – find some page in memory to page out

 Algorithm – terminate? swap out? replace the page?

 Performance – want algorithm with minimum number of page faults

Operating System Concepts Essentials – 2 nd Edition 8.40

Silberschatz, Galvin and Gagne ©2013

Page Replacement

 Prevent over-allocation of memory

 Include page replacement in page fault service

 Use modify bit to reduce overhead of transfers

 Only modified pages are written to disk

 Page replacement completes separation between logical memory and physical memory

 Large virtual memory can be provided by smaller physical memory

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.41

Need For Page Replacement

Operating System Concepts Essentials – 2 nd Edition 8.42

Silberschatz, Galvin and Gagne ©2013

Basic Page Replacement

1.

Find location of desired page on disk

2.

Find a free frame:

 If free frame exists, use it

 If no free frames, use page replacement algorithm to select a victim frame

 If victim frame modified (dirty), write to disk

Operating System Concepts Essentials – 2 nd Edition 8.43

Silberschatz, Galvin and Gagne ©2013

Basic Page Replacement

3.

Bring desired page into (newly) free frame

Update page and frame tables

4.

Continue process by restarting instruction that caused page fault trap

Operating System Concepts Essentials – 2 nd Edition 8.44

Silberschatz, Galvin and Gagne ©2013

Page Replacement

Operating System Concepts Essentials – 2 nd Edition 8.45

Silberschatz, Galvin and Gagne ©2013

Page and Frame Replacement Algorithms

 Frame-allocation algorithm determines

 How many frames per process

 Which frames to replace

 Page-replacement algorithm

 Want lowest page-fault rates

Operating System Concepts Essentials – 2 nd Edition 8.46

Silberschatz, Galvin and Gagne ©2013

Page and Frame Replacement Algorithms

 Evaluate algorithm against string of memory references, compute number of page faults

 String represents page numbers, not full addresses

 Repeated access to page does not cause page fault

 Results depend on number of frames available

 E.g., reference string of referenced page numbers:

7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.47

Graph of Page Faults Versus The Number of Frames

Operating System Concepts Essentials – 2 nd Edition 8.48

Silberschatz, Galvin and Gagne ©2013

First-In-First-Out (FIFO) Algorithm

 When a page must be replaced, use the oldest page

 Can be implemented with FIFO queue

 New page put on tail

 Replace page at head of queue

Operating System Concepts Essentials – 2 nd Edition 8.49

15 page faults

Silberschatz, Galvin and Gagne ©2013

FIFO Illustrating Belady ’ s Anomaly

more frames != fewer page faults

Operating System Concepts Essentials – 2 nd Edition 8.50

Silberschatz, Galvin and Gagne ©2013

Optimal Algorithm

 Replace page that will not be used for longest period of time

 How do you know this? Can ’t read future

 Used as benchmark test

 Theoretical best

Operating System Concepts Essentials – 2 nd Edition 8.51

9 page faults

Silberschatz, Galvin and Gagne ©2013

Least Recently Used (LRU) Algorithm

 Replace page that has not been used in the most amount of time

 Use past knowledge rather than future

 Associate time of last use with each page

Operating System Concepts Essentials – 2 nd Edition 8.52

12 page faults

Silberschatz, Galvin and Gagne ©2013

LRU Algorithm (Cont.)

 Counter implementation

 Every page entry has counter

 Every time page referenced, copy clock into counter

 When page needs to be changed, look at counters, find smallest value

 Search through table needed

Operating System Concepts Essentials – 2 nd Edition 8.53

Silberschatz, Galvin and Gagne ©2013

LRU Algorithm (Cont.)

 Stack implementation

 Keep stack of page numbers in double link form:

 Page referenced:

 Move page to top

 Requires 6 pointers to be changed

 Each update more expensive

 On replacement- no search required

 Bottom of stack..

Operating System Concepts Essentials – 2 nd Edition 8.54

Silberschatz, Galvin and Gagne ©2013

Stack Algorithms

 LRU and OPT are stack algorithms

 Do NOT exhibit Belady’s Anomaly

Operating System Concepts Essentials – 2 nd Edition 8.55

Silberschatz, Galvin and Gagne ©2013

Use Of A Stack to Record Most Recent Page References

Operating System Concepts Essentials – 2 nd Edition 8.56

Silberschatz, Galvin and Gagne ©2013

LRU Approximation Algorithms

 LRU needs special hardware and still slow

 Reference bit

 Associate a bit with each page (initially 0)

 When page referenced, set bit to 1

 Replace any page with reference bit = 0 (if exists)

 Order unknown, however

Operating System Concepts Essentials – 2 nd Edition 8.57

Silberschatz, Galvin and Gagne ©2013

LRU Approximation Algorithms

 Second-chance algorithm

 Generally FIFO, plus HW reference bit

 Clock replacement

 If page to be replaced has

 Reference bit = 0, then replace it

 Reference bit = 1 then:

– Set reference bit 0, leave page in memory

– Replace next page, subject to same rules

Operating System Concepts Essentials – 2 nd Edition 8.58

Silberschatz, Galvin and Gagne ©2013

Second-Chance (clock) Page-Replacement Algorithm

Operating System Concepts Essentials – 2 nd Edition 8.59

Silberschatz, Galvin and Gagne ©2013

Enhanced Second-Chance Algorithm

 Improve algorithm by using reference bit and modify bit (if available) in concert

 Take ordered pair (reference, modify)

1.

(0, 0) neither recently used not modified

2.

(0, 1) not recently used but modified

3.

(1, 0) recently used but clean

4.

(1, 1) recently used and modified

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.60

Enhanced Second-Chance Algorithm

 When page replacement needed, use clock scheme

 Replace page in lowest non-empty <ref, mod> class

 Might need to search circular queue several times

Operating System Concepts Essentials – 2 nd Edition 8.61

Silberschatz, Galvin and Gagne ©2013

Counting Algorithms

 Keep counter to track number of references for each page

 Not common

 Lease Frequently Used ( LFU ) Algorithm : replace page with smallest count

 Most Frequently Used ( MFU ) Algorithm : replace page with largest count

 Idea: page with smallest count most recently brought into memory...

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.62

Page-Buffering Algorithms

 Always keep pool of free frames

 Frame available when needed, not found at page fault

 Read page into free frame

 Select victim to evict and add to free pool

 Only when convenient

Operating System Concepts Essentials – 2 nd Edition 8.63

Silberschatz, Galvin and Gagne ©2013

Page-Buffering Algorithms

 If possible, keep list of modified pages

 Write dirty pages to backing store when it ’s idle

 Set page to non-dirty

 If possible, keep free frame contents intact

 If referenced before reused, no need to load contents again from disk

 Reduces penalty if wrong victim frame selected

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.64

Applications and Page Replacement

 All PR algorithms have OS guessing about future page access

 Some applications have better knowledge

 E.g., databases

 Memory intensive applications can cause double buffering

 OS keeps copy of page in memory as I/O buffer

 Application keeps page in memory for its own work

Silberschatz, Galvin and Gagne ©2013

Operating System Concepts Essentials – 2 nd Edition 8.65

Applications and Page Replacement

 Operating system can given direct access to the disk, getting out of the way of the applications

 Raw disk mode

 Bypasses buffering, locking, etc

Operating System Concepts Essentials – 2 nd Edition 8.66

Silberschatz, Galvin and Gagne ©2013

Download