Memory Management DVGBO1: Operativsystem Mohammad Rajiullah kau.se/cs Based on the slides from book and Umass OS • • • Memory Mangement Program needs to be in memory for CPU access Multiple programs share memory that need to be coordinated Most solutions require hardware support. Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Background • Program executable starts out on disk • Main memory and registers are only storage CPU can access directly • Memory unit only sees a stream of: • addresses + read requests, or • address + data and write requests • Protection of memory required to ensure correct operation Credit: Wikimedia Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Memory Management Terminilogies • Segment: A chunk of memory assigned to a process. • Virtual Address: an address relative to the start of a process's address space. OS 400 0 A 500 0 300 900 1100 C 0 1400 Physical Address memory 0 Virtual Address • Physical Address: a real address in Memory 2000 B 2400 400 Segments Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Where does Address Come From • How do programs generate instruction and data addresses? • Compile time: The compiler generates the exact physical location in memory starting from some xed starting position k. The OS does nothing. • Load time: Compiler generates an address, but at load time the OS determines the process' starting position. Once the process loads, it does not move in memory. • Execution time: Compiler generates an address, and OS can place it any where it wants in memory. Multistep processing of a user program Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Logical Vs. Physical Address • Logical address address generated by the CPU; also referred to as virtual • Physical address address seen by the memory unit • Logical and physical addresses are the same in compile-time and loadtime address-binding schemes; logical (virtual) and physical addresses di er in execution-time address-binding scheme ff Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs • • • • • • • Uniprogramming OS gets a xed part of memory (highest memory in DOS). One process executes at a time. Process is always loaded starting at address 0. Process executes in a contiguous section of memory. Compiler can generate physical addresses. Maximum address = Memory Size - OS Size OS is protected from process by checking addresses used by process. Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Uniprogramming Memory 0 Memory 0 Memory 0 B A C OS 2200 2400 OS 2200 2400 OS 2200 2400 Process A, B and C Simple, but does not allow for overlap of I/O and computation. Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Multiprogramming • Transparency: • We want multiple processes to coexist in memory. • No process should be aware that memory is shared. • Processes should not care what physical portion of memory they are assigned to. • Safety: • Processes must not be able to corrupt each other. • Processes must not be able to corrupt the OS. • E ciency: • Performance of CPU and memory should not be degraded badly due to sharing. ffi Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs • Assume at compile/link time that the process starts at 0 with a maximum address = memory size - OS size. Load a process by allocating a contiguous segment of memory in which the process ts. Memory Mohammad Rajiullah 0 Memory 400 C Operativsystem: Memory Management 400 A A 900 OS 0 B B The rst (smallest) physical address of the process is the base address and the largest physical address the process can access is the limit address. fi • Put the OS in the highest memory. fi • • Relocaltion 2000 2400 C OS 900 1200 2000 2400 kau.se/cs Static Relocation: • • • at load time, the OS adjusts the addresses in a process to re ect its position in memory. Once a process is assigned a place in memory and starts executing it, the OS cannot move it. Dynamic Relocation: • • • hardware adds relocation register (base) to virtual address to get a physical address; hardware compares address with limit register (address must be less than limit). If test fails, the processor takes an address trap and ignores the physical address. Mohammad Rajiullah kau.se/cs Operativsystem: Memory Management fl • Relocation Dynamic Relocation Advantages: • • • • OS can easily move a process during execution. OS can allow a process to grow over time. Simple, fast hardware: two special registers, an add, and a compare. Disadvantages: • • • • • Slows down hardware due to the add on every memory reference. Can't share memory (such as program text) between processes. Process is still limited to physical memory size. Degree of multiprogramming is very limited since all memory of all active processes must t in memory. Complicates memory management. Mohammad Rajiullah kau.se/cs Operativsystem: Memory Management fi • Relocation: Properties • Transparency: processes are largely unaware of sharing. • Safety: each memory reference is checked. • E ciency: memory checks and virtual to physical address translation are fast as they are done in hardware, BUT if a process grows, it may have to be moved which is very slow. ffi Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Recap Uniprogramming Memory 0 Static Relocation OS Contiguous allocation 400 0 A 500 0 300 900 1100 C 0 1400 Physical Address Dynamic Relocation Virtual Address • • • • 2000 B 2400 400 Segments Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Dynamic Loading • • • • • • The entire program does not need to be in memory to execute Routine is not loaded until it is called Better memory-space utilization; unused routine is never loaded All routines kept on disk in relocatable load format Useful when large amounts of code are needed to handle infrequently occurring cases No special support from the operating system is required • Implemented through program design • OS can help by providing libraries to implement dynamic loading Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs • Static linking Dynamic Linking system libraries and program code combined by the loader into the binary program image • Dynamic linking linking postponed until execution time • Dynamic linking is particularly useful for libraries • System also known as shared libraries Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Memory Allocation • As processes enter the system, grow, and terminate, the OS must keep track of which memory is available and utilized. • Holes: pieces of free memory (shaded above in gure) • Given a new process, the OS must decide which hole to use for the process Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Memory Allocation Policies • First-Fit: allocate the rst one in the list in which the process ts. The search can start with the rst hole, or where the previous rst- t search ended. • Best-Fit: Allocate the smallest hole that is big enough to hold the process. The OS must search the entire list or store the list sorted by size hole list. • Worst-Fit: Allocate the largest hole to the process. Again the OS must search the entire list or keep the list sorted. • Simulations show rst- t and best- t usually yield better storage utilization than worst- t; rst- t is generally faster than best- t. fi fi fi fi fi fi fi fi fi fi fi Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs 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 di erence is memory internal to a partition, but not being used • Consider a process of size 8846 bytes and a block of size 8848 bytes it is more e cient to allocate the process the entire 8848 block than it is to keep track of 2 free bytes • Internal fragmentation exists when memory internal to a partition that is wasted Operativsystem: Memory Management ffi ff Mohammad Rajiullah kau.se/cs Compaction • Reduce external fragmentation by compaction • Shu e memory contents to place all free memory together in one large block • Compaction is possible only if relocation is dynamic, and is done at execution time OS OS 20K hole Process 1 Process 1 Process 2 10K hole Process 2 Free 10K ffl Mohammad Rajiullah Free 20+10+10= 40K Operativsystem: Memory Management kau.se/cs Swapping • Roll out a process to disk, releasing all the memory it holds. • When process becomes active again, the OS must reload it in memory. • With static relocation, the process must be put in the same position. • With dynamic relocation, the OS nds a new position in memory for the process and updates the relocation and limit registers. • If swapping is part of the system, compaction is easy to add. Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Another possible solution is non-contiguos memory allocation Paging: motivation & features 90/10 rule: Processes spend 90% of their time accessing 10% of their space in memory. => Keep only those parts of a process in memory that are actually being used • Pages greatly simplify the hole tting problem • The logical memory of the process is contiguous, but pages need not be allocated contiguously in memory. • By dividing memory into xed size pages, we can eliminate external fragmentation. • Paging does not eliminate internal fragmentation fi Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Paging Processes typically do not use their entire space in memory all the time. Paging 1. divides and assigns processes to xed sized pages, 2. then selectively allocates pages to frames in memory, and 3. manages (moves, removes, reallocates) pages in memory. Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Paging: Example Mapping pages in logical memory to frames in physical memory Logical memory Frames in memory A0 F0 OS A1 F1 OS A2 F2 A0 A3 F3 A5 A4 F4 A4 A5 F5 Process A in 6 pages F6 A1 F7 0 400 800 1200 1600 F8 A3 2000 F11 A2 2400 F9 f10 Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Paging Hardware • Problem: How do we nd addresses when pages are not allocated contiguously in memory? • Virtual Address: • Processes use a virtual (logical) address to name memory locations. • Process generates contiguous, virtual addresses from 0 to size of the process. • The OS lays the process down on pages and the paging hardware translates virtual addresses to actual physical addresses in memory. • In paging, the virtual address identi es the page and the page o set. • page table keeps track of the page frame in memory in which the page is located. ff fi Operativsystem: Memory Management fi Mohammad Rajiullah kau.se/cs Paging Hardware ' Translating a virtual address to a physical address. Physical address Logical address CPU f p d d frame e 0x000 d frame ff 0x f ff frame g Page table physical memory ff Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Paging Hardware • Paging is a form of dynamic relocation, where each virtual address is bound by the paging hardware to a physical address. • Think of the page table as a set of relocation registers, one for each frame. • Mapping is invisible to the process; the OS maintains the mapping and the hardware does the translation. • Protection is provided with the same mechanisms as used in dynamic relocation. Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Address Translation Scheme Address generated by CPU is divided into: • Page number (p) used as an index into a page table which contains base address of each page in physical memory • Page o set (d) combined with base address to de ne the physical memory address that is sent to the memory unit page number p d m -n n For given logical address space 2m and page size 2n Operativsystem: Memory Management fi Mohammad Rajiullah ff • page offset kau.se/cs Paging model of logical and physical memory Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Paging Example Logical address: n = 2 and m = 4. Using a page size of 4 bytes and a physical memory of 32 bytes (8 frames) Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Paging Example frame t Page ⑧ O page 4 k O ⑨ I m= 4 r m E 16 i ↑ = 5 3 2 ~ page pagel N 24 logical byte memory22 byte page & P -> 4 first page E e u ↓ logical - 2 2 2 2 bis t P page france 2 fratal last I bits address X Mohammad Rajiullah i 2 -> 13 Of - d freet t 2 u frame Y Ist item 3 Operativsystem: Memory Management kau.se/cs Paging example Consider a machine with 64 MB physical memory and a 32-bit virtual address space. If the page size is 4KB, what is the approximate size of the page table? 210 = 1 K 220 = 1 M 230 = 1 G Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs • Paging Example (more) Frames in Memory How big is the page table? • 16 entries (256 memory bytes / 16 byte pages) • How many bits for an address, assuming we can address 1 byte increments? • 8 bits ( to address 256 bytes) • What is p, and d? Page table A0 0 2 A1 1 6 A2 2 A3 3 11 9 f0 f1 Page size = 16 bytes 32 bytes A0 f2 f3 64 bytes f4 F5 Memory size = 256 bytes • 4 bits for page and 4 for o set 96 bytes A1 F6 F7 128 bytes F8 Given virtual address 24, do the virtual to physical translation A3 F9 • Page p=1, o set d=8 A2 F11 160 bytes F10 192 bytes F12 • Frame f=6, o ser d=8 F13 224 bytes F14 Operativsystem: Memory Management ff ff Mohammad Rajiullah ff • Virtual memory 0 F15 kau.se/cs 256 bytes • Paging Example (more) Frames in Memory How big is the page table? • 16 entries (256 memory bytes / 16 byte pages) • How many bits for an address, assuming we can address 1 byte increments? • 8 bits ( to address 256 bytes) • 0 2 A1 1 6 A2 2 A3 3 11 9 f0 f1 f3 64 bytes f4 F5 Page size = 16 bytes 32 bytes A0 f2 96 bytes A1 F6 F7 128 bytes F8 Given virtual address 24, do the virtual to physical translation A3 F9 • Page p=1, o set d=8 A2 F11 160 bytes F10 192 bytes F12 • Frame f=6, o ser d=8 F13 224 bytes F14 Operativsystem: Memory Management ff Mohammad Rajiullah ff • Page table A0 Memory size = 256 bytes 4 bits for page and 4 for o set ff • What is p, and d? Virtual memory 0 F15 kau.se/cs 256 bytes Virtual address to physical address Given • • • • • • • 1 2 Virtual address = 4GB Physical address = 64 MB Page size= 4 KB No of pages=? No of frames=? No of entries in page table=? Size of page table=? Mohammad Rajiullah Operativsystem: Memory Management =2 220 = 1 M 22 = 4 230 = 1 G 3 2 =8 240 = 1 T 24 = 16 5 2 = 32 26 = 64 7 2 = 128 28 = 256 9 2 = 512 210 = 1024 or 1K kau.se/cs Making Paging Efficient • How should we store the page table? • Registers: Advantages? Disadvantages? • Memory: Advantages? Disadvantages? • Translation look-aside bu er (TLB): a fast fully associative memory that stores page numbers (key) and the frame (value) in which they are stored. • if memory accesses have locality, address translation has locality too. • typical TLB sizes range from 8 to 2048 entries. Operativsystem: Memory Management ff Mohammad Rajiullah kau.se/cs Paging Hardware with TLB Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Effective Access Time • Hit ratio percentage of times that a page number is found in the TLB • An 80% hit ratio means that we nd the desired page number in the TLB 80% of the time. • Suppose that 10 nanoseconds to access memory. • If we nd the desired page in TLB then a mapped-memory access take 10 ns • Otherwise we need two memory access so it is 20 ns •E ective Access Time (EAT) • EAT = 0.80 x 10 + 0.20 x 20 = 12 nanoseconds • implying 20% slowdown in access time • Consider amore realistic hit ratio of 99%, • EAT = 0.99 x 10 + 0.01 x 20 = 10.1ns • implying only 1% slowdown in access time. Operativsystem: Memory Management fi fi ff Mohammad Rajiullah kau.se/cs Memory Protection v: valid bit that says the entry is up-to-date Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Sharing • Paging allows sharing of memory across processes since memory used by a process no longer needs to be contiguous. • Shared code must be reentrant, which means the processes that are using it cannot change it (e.g., no data in reentrant code). • • Sharing of pages is similar to the way threads share text and memory with each other. • The user program (e.g., emacs) marks text segment of a program as reentrant with a system call. • The OS keeps track of available reentrant code in memory and reuses them if a new process requests the same program. • Can greatly reduce overall memory requirements for commonly used applications. A shared page may exist in di erent parts of the virtual address space of each process, but the virtual addresses map to the same physical address. Operativsystem: Memory Management ff Mohammad Rajiullah kau.se/cs Sharing Example Sharing of standard C library in a paging environment. Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Structure of the Page Table • • • • • • • Memory structures for paging can get huge using straight-forward methods Consider a 32-bit logical address space as on modern computers Page size of 4 KB (212) Page table would have 1 million entries (232 / 212) If each entry is 4 bytes each process 4 MB of physical address space for the page table alone Don’t want to allocate that contiguously in main memory One simple solution is to divide the page table into smaller units • • • Hierarchical Paging Hashed Page Tables Inverted Page Tables Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Hierarchical Page Table • Break up the logical address space into multiple page tables • A simple technique is a two-level page table • We then page the page table Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Swapping • • Moving processes between primary memory and disk High context-switch time Standard swapping of two processes using a disk as a backing store. Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Swapping with Paging Mohammad Rajiullah Operativsystem: Memory Management kau.se/cs Paging Summary • Paging is a big improvement over relocation: • They eliminate the problem of external fragmentation and therefore the need for compaction. • They allow sharing of code pages among processes, reducing overall memory requirements. • They enable processes to run when they are only partially loaded in main memory. • However, paging has its costs: • Translating from a virtual address to a physical address is more timeconsuming. • Paging requires hardware support in the form of a TLB to be e cient enough. • Paging requires more complex OS to maintain the page table. Operativsystem: Memory Management ffi Mohammad Rajiullah kau.se/cs