Chapter 8
Chapter 8
Chapter 8
Systems:
Internals
and Design Chapter 8
Principles Virtual Memory
Eighth Edition
William Stallings
Hardware and Control Structures
Two characteristics fundamental to memory management:
1) all memory references are logical addresses that are
dynamically translated into physical addresses at run time
2) a process may be broken up into a number of pieces that
don’t need to be contiguously located in main memory
during execution
Physical memory
• main memory, the actual RAM
Virtual memory
• memory on disk
• allows for effective multiprogramming and relieves the
user of tight constraints of main memory
Virtual Memory That is Larger
Than Physical Memory
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Demand Paging
Bring a page into memory only when it is needed.
Less I/O needed
Less memory needed
Faster response
More users
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Valid-Invalid Bit
With each page table entry a valid–invalid bit is associated
(V in-memory, i not-in-memory)
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Page Fault
If there is ever a reference to a page, first reference will trap
to OS page fault
OS looks at another table to decide:
Invalid reference abort.
Just not in memory.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Steps in Handling a Page
Fault
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Process Creation
Virtual memory allows other benefits during
process creation:
Copy-on-Write
Memory-Mapped Files
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Copy-on-Write
Copy-on-Write (COW) allows both parent and child
processes to initially share the same pages in memory.
If either process modifies a shared page, only then is the
page copied.
COW allows more efficient process creation as only
modified pages are copied.
Free pages are allocated from a pool of zeroed-out pages.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Before Process 1 Modifies Page C
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
After Process 1 Modifies Page C
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Memory-Mapped Files
Memory-mapped file I/O allows file I/O to be treated as routine memory access
by mapping a disk block to a page in memory.
A file is initially read using demand paging. A page-sized portion of the file is read
from the file system into a physical page. Subsequent reads/writes to/from the file
are treated as ordinary memory accesses.
Simplifies file access by treating file I/O through memory rather than read()
write() system calls.
Also allows several processes to map the same file allowing the pages in memory to
be shared.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Memory-Mapped Files
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Page Replacement
Prevent over-allocation of memory by modifying page-fault
service routine to include page replacement.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
What happens if there is
no free frame?
Page replacement – find some page in memory, but not really in
use, swap it out.
algorithm
performance – want an algorithm which will result in
minimum number of page faults.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Need For Page Replacement
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Basic Page Replacement
Find the location of the desired page on disk.
Read the desired page into the (newly) free frame. Update the
page and frame tables.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Page Replacement
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
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.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Exercise
Consider the following page reference string:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Exercise
• 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)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
1 1 5 4
• 4 frames 2 2 1 5 10 page faults
3 3 2
4 4 3
• FIFO Replacement – Belady’s Anomaly
▫ more frames more page faults
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
FIFO Page Replacement
Reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
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
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Optimal Page Replacement
Reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Least Recently Used (LRU)
Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 5
2
3 5 4
4 3
Counter implementation
Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter.
When a page needs to be changed, look at the counters to
determine which are to change.
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
LRU Page Replacement
Reference string:
7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1
Adapted from Silberschatz, A. Galvin, P. and Gagne, G. (2013), Operating System Concepts
Thrashing occurs when a
computer’s virtual memory
subsystem is in a constant
state of high paging activity.
Thrashing