Operating Systems - Lecture 6.1 - Memory management...
Transcript of Operating Systems - Lecture 6.1 - Memory management...
Operating SystemsLecture 6.1 - Memory management 1
Adrien KrahenbuhlMaster of Computer Science
PUF - Hồ Chí Minh
2016/2017
Linking
Allocating memory
Paging overview
Linking Allocating memory Paging overview
CPU access to memory
The CPU reads instructions and reads/write data from/to memory.
Functional interface:value = read( address )write(address , value)
Operating Systems 6.1 - Memory management 1 2/28
Linking Allocating memory Paging overview
Programs have references to memory
Programs make use of memory addressesInstruction execution addresses for branching
Data access addresses for reading/writing data
Operating Systems 6.1 - Memory management 1 3/28
Linking Allocating memory Paging overview
Mono programming
X Run one program at a timeX Share memory between the program and the OS
Absolute memory addressesare no problem
This was the model in old MS-DOS(and other) systems.
Operating Systems 6.1 - Memory management 1 4/28
Linking Allocating memory Paging overview
Multiprogramming
X Keep more than one process in memoryX More processes in memory improves CPU utilization
Absolute memory addressesare a problem!!
Operating Systems 6.1 - Memory management 1 5/28
Linking Allocating memory Paging overview
Justifying Multiprogramming: CPU Utilization
X Keep more than one process in memoryX More processes in memory improves CPU utilization
X If a process spends 20% of its timecomputing, then would switching among 5processes give us 100% CPU utilization?
X Not quite. For n processes, if p = % timea process is blocked on I/O then:
I probability all are blocked = pn
X CPU is not idle for (1− pn) of the timeX 5 processes: 67% utilization
Operating Systems 6.1 - Memory management 1 6/28
Linking Allocating memory Paging overview
How do programs specify memory access?
Absolute codeIf you know where the program gets loaded (any relocation isdone at link time)
Position independent codeAll addresses are relative (e.g., gcc -fPIC option)
Dynamically relocatable codeRelocated at load time
Or ... use logical addresses
X Absolute code with with addresses translated at run timeX Need special memory translation hardware
Operating Systems 6.1 - Memory management 1 7/28
Linking Allocating memory Paging overview
Dynamic Linking
Operating Systems 6.1 - Memory management 1 8/28
Linking Allocating memory Paging overview
Dynamic linking
X A process loads libraries at load timeI Symbol references are resolved at load time
X OS loader finds the dynamic libraries and brings them into theprocess’ memory address space
Operating Systems 6.1 - Memory management 1 9/28
Linking Allocating memory Paging overview
Dynamic loading
X A process can load a module at runtime on requestI Similar to dynamic linkingI Program is written to load a specific libraryI Resolve symbols to get pointers to data & functions
X The library can be unloaded when not needed
Operating Systems 6.1 - Memory management 1 10/28
Linking Allocating memory Paging overview
Shared libraries
X Dynamic linking + sharingX Libraries that are loaded by programs when they start
I All programs that start later use the shared libraryI Program loader searches for needed shared libraries
X Object code is linked with a stubI Stub checks whether the needed library is in memoryI If not, the stub loads itI Stub is then replaced with the address of the library
X Operating system:I Checks if the shared library is already in another process’
memoryI Shares memory region among processes
X Need position independent code or pre-mapped code(reserved regions of memory that processes share)
Operating Systems 6.1 - Memory management 1 11/28
Linking Allocating memory Paging overview
Logical addressing
Memory management unit (MMU)Real-time, on-demand translation between logical (virtual) andphysical addresses
Operating Systems 6.1 - Memory management 1 12/28
Linking Allocating memory Paging overview
Relocatable addressing
Base & limit
X Physical address = logical address + base registerX But first check that: logical address < limit
Operating Systems 6.1 - Memory management 1 13/28
Linking
Allocating memory
Paging overview
Linking Allocating memory Paging overview
Multiple Fixed Partitions
X Divide memory into predefined partitions (segments)I Partitions don’t have to be the same sizeI For example: a few big partitions and many small ones
X New process gets queued for a partition that can hold itX Unused memory in a partition is wasted
I Internal fragmentationI Unused partitions: external fragmentation
Contiguous allocationProcess takes up a contiguous region of memory
Operating Systems 6.1 - Memory management 1 14/28
Linking Allocating memory Paging overview
Variable partition multiprogrammingX Create partitions as neededX New process gets queuedX OS tries to find a hole for it
Operating Systems 6.1 - Memory management 1 15/28
Linking Allocating memory Paging overview
Variable partition multiprogrammingX Create partitions as neededX New process gets queuedX OS tries to find a hole for it
Operating Systems 6.1 - Memory management 1 15/28
Linking Allocating memory Paging overview
Allocation algorithms
First fitFind the first hole that fits
Best fitFind the hole that best fits the process
Worst fitFind the largest available holeX Why? Maybe the remaining space will be big enough for
another process. In practice, this algorithm does not workwell.
Operating Systems 6.1 - Memory management 1 16/28
Linking Allocating memory Paging overview
Variable partition multiprogramming
What if a process needs more memory?
X Always allocate some extra memory just in caseX Find a hole big enough to relocate the process
Combining holes (fragments)
X Memory compactionX Usually not done because of CPU time to move a lot of
memory
Operating Systems 6.1 - Memory management 1 17/28
Linking Allocating memory Paging overview
Segmentation hardware
X Divide a process into segments and place each segment into apartition of memory
I Code segment, data segment, stack segment, etc.X Discontiguous memory allocation
Operating Systems 6.1 - Memory management 1 18/28
Linking
Allocating memory
Paging overview
Linking Allocating memory Paging overview
Paging
X Memory management schemeI Physical space can be non-contiguousI No fragmentation problemsI No need for compaction
X Paging is implemented by the Memory Management Unit(MMU) in the processor
Operating Systems 6.1 - Memory management 1 19/28
Linking Allocating memory Paging overview
Paging
X TranslationI Divide physical memory into fixed-size blocks: page framesI A logical address is divided into blocks of the same size: pagesI All memory accesses are translated: page → page frameI A page table maps pages to frames
X Ex: 32-bit address, 4 KB page sizeI Top 20 bits identify the page numberI Bottom 12 bits identify offset within the page/frame
Operating Systems 6.1 - Memory management 1 20/28
Linking Allocating memory Paging overview
Page translation
Operating Systems 6.1 - Memory management 1 21/28
Linking Allocating memory Paging overview
Logical vs. physical views of memory
Operating Systems 6.1 - Memory management 1 22/28
Linking Allocating memory Paging overview
Hardware implementation
X Where do you keep the page table?I In memory
X Each process gets its own virtual address spaceI Each process has its own page tableI Change the page table by changing a page table baseregister→ CR3 register on Intel IA-32 and x86-64 architectures
X Memory translation is now slow!I To read a byte of memory, we need to read the page table firstI Each memory access is now 2x slower!
Operating Systems 6.1 - Memory management 1 23/28
Linking Allocating memory Paging overview
Hardware Implementation: TLB
X Cache frequently-accessed pagesI Translation lookaside buffer (TLB)I Associative memory: key (page #) and value (frame #)
X TLB is on-chip & fast . . . but small (64-1,024 entries)I Locality in the program ensures lots of repeated lookups
X TLB miss = page # not cached in the TLBI Need to do page table lookup in memory
X Hit ratio = % of lookups that come from the TLB
Operating Systems 6.1 - Memory management 1 24/28
Linking Allocating memory Paging overview
Address Space Identifiers: Tagged TLB
X There is only one TLB per systemX When we context switch, we switch address spaces
I New page tableI But . . . TLB entries belong to the old address space
X Either:I Flush (invalidate) the entire TLBI Have a Tagged TLB with an Address Space Identifier
(ASID)
Operating Systems 6.1 - Memory management 1 25/28
Linking Allocating memory Paging overview
Protection
X An MMU can enforce memory protectionX Page table stores status & protection bits per frame
I Valid/invalid: is there a frame mapped to this page?I Read-onlyI No executeI Kernel only accessI Dirty: the page has been modified since the flag was clearedI Accessed: the page has been accessed since the flag was cleared
Operating Systems 6.1 - Memory management 1 26/28
Linking Allocating memory Paging overview
Multilevel (Hierarchical) page tables
X Most processes use only a small part of their address spaceX Keeping an entire page table is wasteful
I Example: 32-bit system with 4KB pages: 20-bit page table→ 220 = 1,048,576 entries in the page table
Operating Systems 6.1 - Memory management 1 27/28
Linking Allocating memory Paging overview
Multilevel page table
Operating Systems 6.1 - Memory management 1 28/28