Paging
Memory Partitioning Troubles Fragmentation Need for compaction/swapping A process size is limited by the available physical memory Dynamic growth of partition is troublesome No winning policy on allocation/deallocation P2 P3
The Basic Problem A process needs a contiguous partition(s) But Contiguity is difficult to manage Contiguity mandates the use of physical memory addressing (so far)
The Basic Solution Give illusion of a contiguous address space The actual allocation need not be contiguous Use a Memory Management Unit (MMU) to translate from the illusion to reality
A solution: Virtual Addresses Use n-bit to represent virtual or logical addresses A process perceives an address space extending from address 0 to 2n-1 MMU translates from virtual addresses to real ones Processes no longer see real or physical addresses
Paged Memory Subdivide the address space (both virtual and physical) to “pages” of equal size Use MMU to map from virtual pages to physical ones Physical pages are called frames
Paging: Example Virtual Physical Virtual Process 1 Process 0
Key Facts Virtual address spaces of different processes are independent Two or more may have the same address range Yet the mappings differentiate between them A virtual page has no storage of its own It must be backed by a physical frame (real page) that provides the actual storage A contiguous virtual space need not be physically contiguous
Key Facts Physical address space is independent of virtual address spaces They can have different sizes Allows process size to be independent of available physical memory size Page size is always a power of 2, to simplify hardware addressing
Page Tables Virtual Page Table
Page Table Structure Indexed by virtual page number Contains frame number (if any) Contains protection bits Contains reference bit Frame No. v w r x f m Frame No. v w r x f m Frame No. v w r x f m v: valid bit w: write bit r: read bit x: execute bit (rare) f: reference bit m: modified bit
Mapping Virtual to Real Addresses n bits Virtual address virtual page number offset s bits index into page table s bits frame no. offset p bits s: log (page size) Physical address
Example: PDP-11 Page size: 8K Up to 4M mem 16 bits Virtual address vpn offset 13 bits 13 bits frame no. offset 8-entry page table 22 bits Physical address (in hardware)
Weird Stuff: Free Page Management Virtual Physical Virtual Process 0 Process 1 Key fact: A memory frame cannot be accessed unless mapped Free space
Fun Stuff: Sharing Virtual Physical Virtual Process 1 Process 0
Sharing Processes can share pages by mapping their virtual pages to the same memory frame In UNIX and Windows, code segments of processes running the same program to share the pages containing executables saves a lot of memory saves loading time for frequently run programs Fine tuning using protection bits (rwx)
Fork Revisited: Copy-on-Write Virtual Physical Virtual W W W W W W Father Child
Copy-on-Write Fork Initially no memory copying Efficient If an exec follows, no much harm Page tables set the protection to disallow writes If either father or child attempts to write, a page fault occurs
Copy-on-Write Virtual Physical Virtual W W W W W W Father Child
Requirements for Sharing Page frames must have a reference count Cannot be deallocated unless unmapped Adds complexity to memory manager Protection bits must be set properly Must externalize protection bits to user programs (mprotect()) Protection bits meaning could be overloaded (bad)