Download Virtual Memory - CS 61 and more Exercises Operating Systems in PDF only on Docsity!
Virtual Memory
CS61, Lecture 15
Prof. Stephen Chong
October 20, 2011
Stephen Chong, Harvard University
Announcements
- Midterm review session: Monday Oct 24
- 5:30pm to 7pm, 60 Oxford St. room 330
- Large and small group interaction 2
Stephen Chong, Harvard University (^4)
Today
- Running multiple programs at once
- Virtual memory
- Address spaces
- Benefits
- Physical memory as a cache for virtual memory
- Page tables
- Page hits and page faults
- Virtual memory as a tool for memory management
- Virtual memory as a tool for memory protection
- Address translation
- Table Lookaside Buffer (TLB)
- Example
Stephen Chong, Harvard University (^5)
Running multiple programs at once
- So far, we have discussed memory management for a single
program running on a computer.
- Most modern computers run multiple programs simultaneously.
- This is called multiprogramming or multitasking. I think I have my own CPU So do I!
Stephen Chong, Harvard University (^7)
Virtual memory
- OS also gives every program illusion
of having its own memory!
- Each program running on the machine
is called a process.
- Each process has a process ID (a number) and an owner – the user ID running the process.
- (^) UNIX command ps prints out info about the running processes.
- Each process has its own address
space
- Contents of that process' memory, visible only to that process.
- 4GB on 32 bit machine, ~16 TB on 64 bit machine OS memory Read/write segment .data, .bss Read-only segment .text, .rodata unused Shared libraries Heap (used by malloc) User stack 0x Process address space 0xffffffff
Stephen Chong, Harvard University Problem 1: How Does Everything Fit? Physical main memory: Few Gigabytes
8 OS memory Read/write segment .data, .bss Read-only segment .text, .rodata unused Shared libraries Heap (used by malloc) User stack 4GB to 16 EB virtual memory
Stephen Chong, Harvard University
Problem 3: How To Protect
Physical main memory Process i Process j 10
Stephen Chong, Harvard University
Problem 4: How To Share
Physical main memory Process i Process j 11
Stephen Chong, Harvard University
Aside: indirection
13
David Wheeler
1927– Computer Science Pioneer World’s first PhD in Computer Science All problems in computer science can be solved by another level of indirection... Except for the problem of too many layers of indirection.
Stephen Chong, Harvard University
Address Spaces
- Linear address space:
- Ordered set of contiguous non-negative integer addresses: {0, 1, 2, 3 … }
- Virtual address space:
- Set of N = 2 n virtual addresses {0, 1, 2, 3, …, N-1}
- Physical address space:
- Set of M = 2 m physical addresses {0, 1, 2, 3, …, M-1}
- Clean distinction between data (bytes) and their attributes (addresses)
- Each object can now have multiple addresses
- Every byte in main memory: one physical address, one (or more) virtual addresses 14
Stephen Chong, Harvard University
A System Using Virtual Addressing
- Used in all modern desktops, laptops, workstations
- One of the great ideas in computer science MMU Physical address (PA) Data word Virtual address (VA) CPU Chip 16 0: 1: M-1: Main memory 2: 3: 4: 5: 6: 7: 8: ... CPU
Stephen Chong, Harvard University
Benefits of Virtual Memory (VM)
- Efficient use of limited main memory (RAM)
- Use RAM as a cache for the parts of a virtual address space
- some non-cached parts stored on disk
- some (unallocated) non-cached parts stored nowhere
- Keep only active areas of virtual address space in memory
- transfer data back and forth as needed
- Simplifies memory management for programmers
- Each process gets the same full, private linear address space
- Isolates address spaces
- One process can’t interfere with another’s memory
- because they operate in different address spaces
- User process cannot access privileged information
- different sections of address spaces have different permissions 17
Stephen Chong, Harvard University
VM as a Tool for Caching
- Virtual memory: array of N = 2 n contiguous bytes
- think of the array (allocated part) as being stored on disk
- Physical main memory (DRAM) = cache for allocated virtual memory
- (^) Blocks are called pages ; size = 2 p PP 2 m-p - Physical memory Empty Empty Uncached VP 0 VP 1 VP 2 n-p
Virtual memory Unallocated Cached Uncached Unallocated Cached Uncached PP 0 PP 1 Empty Cached 0 2 n
2 m
0 Virtual pages (VP's) stored on disk Physical pages (PP's) cached in DRAM 19
Stephen Chong, Harvard University (^20) Memory Core 1 Regs Unified L Cache L d-cache L i-cache L Cache Core 2 Regs Unified L Cache L d-cache L i-cache Core 3 Regs Unified L Cache L d-cache L i-cache Core 4 Regs Unified L Cache L d-cache L i-cache
Memory hierarchy: Intel Core i
Processor Disk ~1ns ~3ns ~12ns ~60ns ~8ms 60 × ~100 000×