Introduction to Operating Systems Lecture 6: Memory Management MING GAO SE@ecnu (for course related communications) mgao@sei.ecnu.edu.cn Apr. 22, 2015
Outline 1 Issues of main memory 2 Main memory management for uniprogramming 3 Main memory management for multiprogramming Segmentation and its implementation Paging and its implementation MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 2 / 36
issues Process and thread revision Threads encapsulate concurrency Active component of a process Address spaces encapsulate protection Keeps buggy program from trashing the system Passive component of a process MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 3 / 36
issues Issues of main memory Controlled overlap Separate state of threads should not collide in physical memory. Conversely, would like the ability to overlap when desired (for comm.) Translation Ability to translate accesses from virtual address space to the physical one Protection Can be used to avoid overlap Can be used to give uniform view of memory to programs Prevent access to private memory of other processes Different pages of memory can be given special behavior (Read Only, Invisible to user programs, etc). Kernel data protected from user programs Programs protected from themselves MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 4 / 36
issues From source code to a process MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 5 / 36
uniprogramming Uniprogramming Apply to single-user and single-task OS only Require system and user partitions only System partition can be protected by limit address MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 6 / 36
Naive multiprogramming No translation and protection Use Loader/Linker: Adjust addresses while program loaded into memory Everything adjusted to memory location of program Translation done by a linker-loader Was pretty common in early days MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 7 / 36
Protection A pair of base and limit registers define the logical address space CPU must check every memory access generated in user mode to be sure it is between base and limit for that program MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 8 / 36
Protected multiprogramming w/o. translation Use two special registers: base and limit If user tries to access illegal address, cause an error Context switch kernel loads new base/limit from PCB users are not allowed to change base/limit registers MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 9 / 36
Outline multiprogramming segmentation 1 Issues of main memory 2 Main memory management for uniprogramming 3 Main memory management for multiprogramming Segmentation and its implementation Paging and its implementation MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 10 / 36
segmentation Segmentation: Multiprog. w/ translation Dynamic address translation using base/limit User may have multiple segments available (e.g x86) Loads and stores include segment ID in opcode: Operating system moves around segment base pointers as necessary MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 11 / 36
segmentation Logical view of segmentation Logical address consists of a two-tuple: < segmentid, offset > Segment table maps two-dimensional physical addresses: base and limit Segment-table base register (STBR) points to the segment table s location in memory Segment-table length register (STLR) indicates number of segments used by a program MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 12 / 36
segmentation Implementation of segmentation Segments Every segment of physical memory may not be equivalent Memory allocation is a dynamic storage-allocation problem Segment table Resides in physical memory Contains physical addresses and permission for each virtual segment permission include: Valid bits, Read, Write, etc. MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 13 / 36
segmentation Issues of simple segmentation Fragmentation problem Not every process is the same size Over time, memory space becomes fragmented Hard to do inter-process sharing Want to share code segments when possible Want to share memory between processes Helped by providing multiple segments per process Need enough physical memory for every process MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 14 / 36
Contiguous allocation multiprogramming segmentation Hole Block of available memory; holes of various size are scattered throughout memory When a process arrives, it is allocated memory from a hole large enough to accommodate it Operating system maintains information about: allocated partitions free partitions (hole) MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 15 / 36
segmentation Dynamic storage allocation First-fit Allocate the first hole that is big enough Best-fit Allocate the smallest hole that is big enough; must search entire list, unless ordered by size Produces the smallest leftover hole Worst-fit Allocate the largest hole; must also search entire list Produces the largest leftover hole MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 16 / 36
segmentation Fragmentation External Fragmentation total memory space exists to satisfy a request, but it is not contiguous Internal Fragmentation allocated memory may be slightly larger than requested memory; this size difference is memory internal to a partition, but not being used Compaction Shuffle memory contents to place all free memory together in one large block? Relocation must be dynamic, and is done at execution time I/O problem MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 17 / 36
segmentation More on segmentation implementation Logical View multiple separate segments Typical: Code, Data, Stack Others: memory sharing, etc Each segment is given region of contiguous memory Has a base and limit Can reside anywhere in physical memory How to support heap and stack to grow independently? How to do inter-process sharing? How to implement multiple segments? segment table? MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 18 / 36
segmentation Swapping Extreme form of Context Switch: Swapping In order to make room for next process, some or all of the previous process is moved to disk Likely need to send out complete segments This greatly increases the cost of context-switching Desirable alternative? Some way to keep only active portions of a process in memory at any one time Need finer granularity control over physical memory MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 19 / 36
Outline multiprogramming paging 1 Issues of main memory 2 Main memory management for uniprogramming 3 Main memory management for multiprogramming Segmentation and its implementation Paging and its implementation MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 20 / 36
paging Paging: Multiprogramming w/ translation Aims Isolate processes and kernel from one another Allow flexible translation that: Doesn t lead to external fragmentation Allows easy sharing between processes Allows only part of process to be resident in physical memory MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 21 / 36
paging Memory management unit (MMU) Hardware device that maps virtual to physical address In MMU scheme, the value in the relocation register is added to every address generated by a user process at the time it is sent to memory The user program deals with logical addresses; it never sees the real physical addresses MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 22 / 36
paging Implementation of paging Every chunk of physical memory is equivalent (1K - 16K) Page table (one per process) Resides in physical memory Contains physical page and permission for each virtual page Permissions include: Valid bits, Read, Write, etc Internal fragmentation Page size = 2,048 bytes, process size = 7,766 bytes How to compute the internal fragmentation So small frame sizes desirable? MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 23 / 36
paging Implementation of paging cont d Virtual address consists of page number and page offset How to map virtual address 7,098 to physical address when page size=1k. MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 24 / 36
Sharing in paging? multiprogramming paging Two entries from different page-table (of two different processes) point to the same shared page MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 25 / 36
paging Discussion on simple paging Pros Simple memory allocation Easy to Share Cons What if address space is sparse? E.g. on UNIX, code starts at 0, stack starts at (2 31 1). With 1K pages, need 2 million page table entries! What if table really big? Not all pages used all the time would be nice to have working set of page table in memory MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 26 / 36
paging Multi-level translation: Segmentation with paging A tree of tables Lowest level page table: memory still allocated with bitmap Higher levels often segmented MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 27 / 36
paging Multi-level translation: Multi-level page table A tree of page tables Tables fixed size (1024 entries) Valid bits on Page Table Entries Don t need every 2nd-level table Even when exist, 2nd-level tables can reside on disk if not in use MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 28 / 36
paging Multi-level translation analysis Pros Only need to allocate as many page table entries as we need for application sparse address spaces are easy Easy memory allocation Easy Sharing Share at segment or page level (need additional reference counting) Cons One pointer per page (typically 4K - 16K pages today) Page tables need to be contiguous However, previous example keeps tables to exactly one page in size Two (or more, if > 2 levels) lookups per reference Seems very expensive! MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 29 / 36
paging More on page-table implementation The problem of forward page tables Size of page table is at least as large as amount of virtual memory allocated to processes Physical memory may be much less Much of process space may be out on disk or not in use Inverted page table Search is expensive Only one page table for the whole system MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 30 / 36
paging More on page-table implementation Hashed page table Size is independent of virtual address space Directly related to amount of physical memory Very attractive option for 64-bit address spaces Complexity of managing hash changes Often in hardware! MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 31 / 36
paging More on paging: TLB Too expensive to translate on every access At least two DRAM accesses per actual DRAM access Or: perhaps I/O if page table partially on disk! Associative memory parallel search of (p, d) If p is in associative register, get frame # out Otherwise get frame # from page table in memory MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 32 / 36
paging TLB: Effective access time (EAT) Associative lookup ɛ (time unit) Hit ratio α (percentage of a page number if found in the associative registers) Memory cycle time 1 (time unit) EAT = (1 + ɛ) α + (2 + ɛ) (1 α) = 2 + ɛ α (time unit) MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 33 / 36
Take-aways Take-aways Observing main memory management from different point-of-views Translation Multiprogramming Protection Sharing Caching MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 34 / 36
Take-aways Terms and their Chinese translations MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 35 / 36
Take-aways Acknowledgement Many slides are copied or adapted from: Slides provided by authors of the textbook (http://codex.cs.yale.edu/avi/os-book/os7/) Prof. Anthony D. Joseph s slides for course CS162 (2006) at EECS@UCBerkeley (http://inst.eecs.berkeley.edu/~cs162) (Copyright @ 2008 UCB) MING GAO (SE@ecnu) Introduction to Operating Systems Apr. 22, 2015 36 / 36