Goals of Memory Management: CSE 451: Operating Systems Spring 2012
Goals of Memory Management: CSE 451: Operating Systems Spring 2012
Goals of Memory Management: CSE 451: Operating Systems Spring 2012
Ed Lazowska
[email protected]
Allen Center 570
© 2012 Gribble, Lazowska, Levy, Zahorjan 5 © 2012 Gribble, Lazowska, Levy, Zahorjan 6
1
• Swapping • Then came multiprogramming
– save a program’s entire state (including its memory image) – multiple processes/jobs in memory at once
to disk
• to overlap I/O and computation between processes/jobs, easing
– allows another program to be run the task of the application programmer
– first program can be swapped back in and re-started right – memory management requirements:
where it was
• protection: restrict which addresses processes can use, so they
can’t stomp on each other
• The first timesharing system, MIT’s “Compatible Time • fast translation: memory lookups must be fast, in spite of the
Sharing System” (CTSS), was a uni-programmed protection scheme
swapping system • fast context switching: when switching between jobs, updating
– only one memory-resident user memory hardware (protection and translation) must be quick
– upon request completion or quantum expiration, a swap took
place
– bow wow wow … but it worked!
© 2012 Gribble, Lazowska, Levy, Zahorjan 7 © 2012 Gribble, Lazowska, Levy, Zahorjan 8
© 2012 Gribble, Lazowska, Levy, Zahorjan 9 © 2012 Gribble, Lazowska, Levy, Zahorjan 10
2
Old technique #2: Variable partitions Mechanics of variable partitions
• Obvious next step: physical memory is broken up into
partitions dynamically – partitions are tailored to programs physical memory
…
partition 4
…
frame Y
page X
© 2012 Gribble, Lazowska, Levy, Zahorjan 15 © 2012 Gribble, Lazowska, Levy, Zahorjan 16
Life is easy …
• For the programmer … • For the protection system
– Processes view memory as a contiguous address space – One process cannot “name” another process’s memory –
from bytes 0 through N – a virtual address space there is complete isolation
– N is independent of the actual hardware • The virtual address 0xDEADBEEF maps to different physical
addresses for different processes
– In reality, virtual pages are scattered across physical
memory frames – not contiguous as earlier
• Virtual-to-physical mapping Note: Assume for now that all pages of the address
• This mapping is invisible to the program space are resident in memory – no “page faults”
• For the memory manager …
– Efficient use of memory, because very little internal
fragmentation
– No external fragmentation at all
• No need to copy big chunks of memory around to coalesce free
space
© 2012 Gribble, Lazowska, Levy, Zahorjan 17 © 2012 Gribble, Lazowska, Levy, Zahorjan 18
3
Address translation Paging (K-byte pages)
• Translating virtual addresses
page table virtual address space physical memory
– a virtual address has two parts: virtual page number & offset
process 0
page frame 0 0
– virtual page number (VPN) is index into a page table page 0 page frame 0
0 3 K 1K
– page table entry contains page frame number (PFN) 1 5 page 1 page frame 1
2K 2K
– physical address is PFN::offset page frame 2
3K
• Page tables page frame 3
4K
– managed by the OS page table virtual address space page frame 4
0 5K
– one page table entry (PTE) per page in virtual address space page frame
page 0 page frame 5
process 1
0 7 K 6K
• i.e., one PTE per VPN page frame 6
1 5 page 1
7K
– map virtual page number (VPN) to page frame number (PFN) 2K
page frame 7
2 - page 2
• VPN is simply an index into the page table 3K 8K
3 1 page frame 8
page 3
4K 9K
page frame 9
10K
? Page fault – next lecture!
© 2012 Gribble, Lazowska, Levy, Zahorjan 19 © 2012 Gribble, Lazowska, Levy, Zahorjan 20
virtual address
• Assume 32 bit addresses
virtual page # offset – assume page size is 4KB (4096 bytes, or 212 bytes)
physical memory – VPN is 20 bits long (220 VPNs), offset is 12 bits long
page
page table frame 0
page • Let’s translate virtual address 0x13325328
physical address
frame 1 – VPN is 0x13325, and offset is 0x328
page
page frame # page frame # offset – assume page table entry 0x13325 contains value 0x03004
frame 2
page • page frame number is 0x03004
frame 3 • VPN 0x13325 maps to PFN 0x03004
– physical address = PFN::offset = 0x03004328
…
page
frame Y
© 2012 Gribble, Lazowska, Levy, Zahorjan 21 © 2012 Gribble, Lazowska, Levy, Zahorjan 22
4
Paging advantages Paging disadvantages
• Easy to allocate physical memory • Can still have internal fragmentation
– Process may not use memory in exact multiples of pages
– physical memory is allocated from free list of frames
– But minor because of small page size relative to address space
• to allocate a frame, just remove it from the free list size
– external fragmentation is not a problem • Memory reference overhead
• managing variable-sized allocations is a huge pain in the neck – 2 references per address lookup (page table, then memory)
– “buddy system” – Solution: use a hardware cache to absorb page table lookups
• translation lookaside buffer (TLB) – next class
• Leads naturally to virtual memory
• Memory required to hold page tables can be large
– entire program need not be memory resident – need one PTE per page in virtual address space
– take page faults using “valid” bit – 32 bit AS with 4KB pages = 220 PTEs = 1,048,576 PTEs
– all “chunks” are the same size (page size) – 4 bytes/PTE = 4MB per page table
– but paging was originally introduced to deal with external • OS’s have separate page tables per process
• 25 processes = 100MB of page tables
fragmentation, not to allow programs to be partially resident
– Solution: page the page tables (!!!)
• (ow, my brain hurts…more later)
© 2012 Gribble, Lazowska, Levy, Zahorjan 25 © 2012 Gribble, Lazowska, Levy, Zahorjan 26
Segmentation
(We will be back to paging soon!)
What’s the point?
• Paging • More “logical”
– mitigates various memory allocation complexities (e.g., – absent segmentation, a linker takes a bunch of independent
fragmentation) modules that call each other and linearizes them
– view an address space as a linear array of bytes – they are really independent; segmentation treats them as
– divide it into pages of equal size (e.g., 4KB) such
– use a page table to map virtual pages to physical page • Facilitates sharing and reuse
frames – a segment is a natural unit of sharing – a subroutine or
• page (logical) => page frame (physical) function
• Segmentation • A natural extension of variable-sized partitions
– partition an address space into logical units – variable-sized partition = 1 segment/process
• stack, code, heap, subroutines, … – segmentation = many segments/process
– a virtual address is <segment #, offset>
© 2012 Gribble, Lazowska, Levy, Zahorjan 27 © 2012 Gribble, Lazowska, Levy, Zahorjan 28
raise
protection fault segment 4
© 2012 Gribble, Lazowska, Levy, Zahorjan 29 © 2012 Gribble, Lazowska, Levy, Zahorjan 30
5
Pros and cons Combining segmentation and paging
• Can combine these techniques
• Yes, it’s “logical” and it facilitates sharing and reuse – x86 architecture supports both segments and paging
• But it has all the horror of a variable partition system • Use segments to manage logical units
– except that linking is simpler, and the “chunks” that must be
– segments vary in size, but are typically large (multiple pages)
allocated are smaller than a “typical” linear address space
• Use pages to partition segments into fixed-size chunks
• What to do?
– each segment has its own page table
• there is a page table per segment, rather than per user address
space
– memory allocation becomes easy once again
• no contiguous allocation, no external fragmentation
• Linux:
– 1 kernel code segment, 1 kernel data segment
– 1 user code segment, 1 user data segment
– all of these segments are paged