Operating Systems and Computer Networks Memory Management pascal.klein@uni-due.de Alexander Maxeiner, M.Sc. Faculty of Engineering
Agenda 1 Swapping 2 Segmentation Algorithms 3 Memory Allocation 4 Virtual Memory 5 Paging 6 Page Replacement Algorithms 2
Memory Management Goals of Memory Management Convenient abstraction for programming Allocation of scarce memory resources among competing processes maximize performance with minimal overhead Mechanisms Physical and virtual addressing Partitioning, Paging, Segmentation Page replacement algorithms 3
Memory Hierarchy (1) small size small capacity small size small capacity Power on Power on Immediate term processor registers very fast, very expensive processor cache very fast, very expensive medium size medium capacity Power on very short term Random access memory fast, affordable medium size large capacity Power off short term flash / USB memory slower, cheap Large size very large capacity Large size very large capacity Power off mid term Power off Long term hard drives slow, very cheap Tape backup very slow, affordable 4
Memory Hierarchy (2) small size small capacity small size small capacity medium size medium capacity Large size very large capacity processor registers very fast, very expensive Power on Power on Immediate term Power on very short term Power off mid term processor cache very fast, very expensive Random access memory fast, affordable hard drives slow, very cheap 5
Sharing of Memory Program 1 Free Space Program 3 Program 2 Free Space OS Allocation Protection from each other, Protecting OS Translating logical addresses to physical Swapping (if physical memory is too small for all processes) Virtual memory (memory abstraction) 6
Memory Strategies Program 1 Free Space Program 3 Program 2 Free Space OS Fetch strategy When AND how much to load at a time (e.g.: prefetching) Placement (or allocation) strategy Determination WHERE data is to be placed Replacement strategy Determination WHICH area can be removed Variations Fixed partitions Variable partitions Segmentation paging Early computers, AND embedded, small, etc... Modern PCs 7
No Memory Abstraction (1) MOV REG1, 1000 set of physical addresses from 0 to... one program only How to execute more than one program? OS Program Program OS 8
Assumption: Increased Size - no Memory Abstraction Divide all physical memory into a fixed set of contiguous partitions Place only one process at a time in any partition Partition boundaries limit the available memory for each process Proces either entirely in main memory or not No sharing between processes Memory wasting: partition internal fragmentation Free partitions too small Without virtual addressing protection problems! Without Swapping/virtual memory size problems! 12K 2K 6K OS: 2K 9
Swapping 10
Memory Abstraction Swapping Free Space Issue: memory (RAM) is too small swap data on slower but bigger memory Swapper decides which processes should be in main memory OS 11
Swapping Example Assumption: 180K of Memory, OS: 20K 180K OS: 20K 12
Swapping Example Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in 150K Process A: 30K OS: 20K 13
Swapping Example 100K Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in Process B (50K) is created or swapped in Process B: 50K Process A: 30K OS: 20K 14
Swapping Example 80K Process C: 20K Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in Process B (50K) is created or swapped in Process C (20K) is created or swapped in Process B: 50K Process A: 30K OS: 20K 15
Swapping Example 60K Process D: 20K Process C: 20K Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in Process B (50K) is created or swapped in Process C (20K) is created or swapped in Process D (20K) is created or swapped in Process B: 50K Process A: 30K OS: 20K 16
Swapping Example 60K Process D: 20K Process C: 20K Process B: 50K Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in Process B (50K) is created or swapped in Process C (20K) is created or swapped in Process D (20K) is created or swapped in Process A is swapped out to disk 30K OS: 20K 17
Swapping Example 60K Process D: 20K 20K Process B: 50K 30K Assumption: 180K of Memory, OS: 20K Process A (30K) is created or swapped in Process B (50K) is created or swapped in Process C (20K) is created or swapped in Process D (20K) is created or swapped in Process A is swapped out to disk Process C is swapped out to disk What is the problem? OS: 20K 18
Swapping Issue Memory Fragmentation Memory Compaction Problem: CPU time Fixed size of partitions Problem: growing memory Bigger partitions with room for grow (here: heap) Variable size of partitions Management with Bit-Maps or Linked Lists Free Space Process C Free Space Process D OS Trade-off: Space requirements vs. time for allocation 19
Managing free space with Bit-Maps 60K Process D: 20K 20K Process B: 50K 30K OS: 20K Memory division into allocation units Corresponding bit for each allocation unit (0,1) telling whether it is free or not Example: Allocation units of 10KiB For current 20 bits: 00000011 00111110 0011 Trade-off: Bit-Map size depends on allocation unit searching for k consecutive 0-bits in map costly, fixed size 20
Managing free space with Linked Lists 60K Process D: 20K 20K Process B: 50K 30K Linked list of allocated and free memory segments Each record has Process ID/ Free (H: hole) Start address length pointer to next record H 2 3 B 5 5 H 10 2 OS: 20K D 12 2 H 14 6 21
Managing free space with Linked Lists 60K Process D: 20K 20K Update Process: When a process is swapped out, neighbouring blocks need to be examined doubly linked lists (containing a pointer to the next AND previous node Process B: 50K 30K H 2 3 P 5 5 H 10 2 OS: 20K P 12 2 H 14 6 22
Algorithms to allocate memory 60K Process D: 20K 20K Process B: 50K 30K Assuming a new process E requests 15K Which hole should it use? Available algorithms First fit Next fit Best fit Worst fit OS: 20K 23
Memory Allocation: First Fit 60K Process D: 20K 20K Process B: 50K simplest algorithm scanning along the list (from beginning) until it finds sufficient hole Breaking hole in two pieces: One for process One for unused memory (new hole) 30K OS: 20K 24
Memory Allocation: First Fit 60K Process D: 20K 20K Process B: 50K Free: 15K Process E:15K OS: 20K simplest algorithm scanning along the list (from beginning) until it finds sufficient hole Breaking hole in two pieces: stop start One for process One for unused memory (new hole) Fitting into 30K hole Very fast searches as little as possible 25
Memory Allocation: Next Fit 60K Process D: 20K 20K Process B: 50K 30K Variation of First Fit Keeps track of where it found a hole Next time: start at position where it left off last time Assumption that prospective fitting holes come after already found holes However, slight worse performance than first fit OS: 20K 26
Memory Allocation: Next Fit 60K Process D: 20K 20K Process B: 50K Free: 15K Process E:15K OS: 20K Variation of First Fit Keeps track of where it found a hole Next time: start at position where it left off last time Assumption that prospective fitting holes come after already found holes stop start However, slight worse performance than first fit Fitting into 30K hole 27
Memory Allocation: Next Fit 60K Process D: 20K Free: 5K Process F:15K Process B: 50K Free: 15K Process E:15K OS: 20K Variation of First Fit Keeps track of where it found a hole Next time: start at position where it left off last time stop Assumption that prospective fitting holes come after already found holes start However, slight worse performance than first fit Fitting into 30K hole Then, fitting into 20K hole 28
Memory Allocation: Best Fit 60K Process D: 20K 20K Searches entire list (from beginning to end) Takes smallest hole Assumption of best memory spacial performance Process B: 50K 30K OS: 20K 29
Memory Allocation: Best Fit 60K Process D: 20K Free: 5K Process E:15K Process B: 50K 30K OS: 20K stop Searches entire list (from beginning to end) Takes smallest hole Assumption of best memory spacial performance Fitting into 20K hole Takes much CPU time (for searching) Resulting in more memory wasting because of numerous tiny useless holes. start 30
Memory Allocation: Worst Fit 60K Process D: 20K 20K Variation of best fit Always takes the largest hole Assumption to get around splitting into tiny holes, to be big enough for other processes Process B: 50K 30K OS: 20K 31
Memory Allocation: Worst Fit 45K Process E:15K Process D: 20K 20K Process B: 50K 30K OS: 20K stop Variation of best fit Always takes the largest hole Assumption to get around splitting into tiny holes, to be big enough for other processes start Fitting into 60K hole Takes much CPU time (for searching) Still memory wasting 32
Memory Allocation: Enhancing performance Maintaining separate lists for processes AND holes Algorithms devote energy to inspect holes NOT processes BUT higher effort for deallocating (changing both lists) Sorting of lists regarding size enhancing speed of Best Fit/Worst Fit In practice: FF is usually better NF is pointless when sorted lists are used 33
Buddy System Memory encapsulated in units of powers of 2 Process requests rounded up to fit into units Possible hole sizes:..., 4K, 8K, 16K, 32K, 64K, 128K,... 64K 32K 16K 8K 4K 4K 34
Buddy System Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) 128K 35
Buddy System 64K 128K 32K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) 16K 8K A: 8K 36
Buddy System 64K 32K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) Process C requests 24K rounded to 32K (8K wasted) 16K B: 8K A: 8K 37
Buddy System 64K C: 32K 16K B: 8K A: 8K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) Process C requests 24K rounded to 32K (8K wasted) Process D requests 3K rounded to 4K (1K wasted) 38
Buddy System 64K C: 32K 8K 4K D: 4K B: 8K A: 8K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) Process C requests 24K rounded to 32K (8K wasted) Process D requests 3K rounded to 4K (1K wasted) Process A exits 39
Buddy System 64K C: 32K 8K 4K D: 4K B: 8K 8K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) Process C requests 24K rounded to 32K (8K wasted) Process D requests 3K rounded to 4K (1K wasted) Process A exits Process B exits 40
Buddy System 64K C: 32K 8K 4K D: 4K 16K Example: Suppose a memory of 128K One hole of 128K Process A requests 6K rounded to 8K (2K wasted) Process B requests 5K rounded to 8K (3K wasted) Process C requests 24K rounded to 32K (8K wasted) Process D requests 3K rounded to 4K (1K wasted) Process A exits Process B exits 41
Virtual Memory 42
Virtual Addressing Program 1 Free Space Program 3 Program 2 Free Space OS address space address space address space Issue: By using physical addresses all executions are absolutely adressed NOT relatively! Better memory management with logical addresses Independancy of actual physical location of data in physical memory OS determines logical location of data CPU instructions using virtual addresses (don t care about location) Translated by hardware into physical address 43
Virtual Memory Software size much bigger than available RAM Each program s address space broken up into chunks Pages Page: contiguous range of addresses mapped onto physical memory Not all pages have to be in physical memory If OS notices, a program part is not in physical memory, it needs to be loaded and re-executed In Multiprogramming: While program is waiting for pieces of itself to be read in, CPU can execute another process 44
Paging Page: Fixed size unit in virtual memory Page frame: corresponding units in physical memory Same size (here: 4KB) Transparent to the program Protection a program cannot leave Virtual Address Space Example: 64KB virtual address space, 32KB physical space, 4KB page size How many pages/page frames? Answer: 16 virtual pages, 8 page frames 45
Relationship between virtual and physical memory virtual address space Addresses No. 4.096-8.191 60KB-64KB... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 28KB-32KB... 4KB-8KB 0KB-4KB page frame 46
Relationship between virtual and physical memory virtual address space Addresses No. 0 4.095 60KB-64KB... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 28KB-32KB... 4KB-8KB 0KB-4KB page frame MOV REG, 0 MMU checks VM page 0 corresponds to page frame 1 Address is changed to 4096 47
Relationship between virtual and physical memory virtual address space Addresses No. 8.192 12.287 60KB-64KB... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 28KB-32KB... 4KB-8KB 0KB-4KB page frame MOV REG, 8192 MMU checks VM page 8192 corresponds to page frame 3 Address is changed to 12288 48
Relationship between virtual and physical memory virtual address space Addresses No. 20.480 24.576 60KB-64KB... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 28KB-32KB... 4KB-8KB 0KB-4KB MOV REG, 20500 MMU checks VM page 20500 corresponds to page frame 4 with an offset of 20 Byte (20.480 + 20) Address is changed to 16404 (16.384 + 20) page frame 49
Memory Management Unit (MMU) p: page number d: offset r: page frame 50
Paging translating addresses Virtual address has two parts: virtual page number and offset Virtual page number is an index into a page table Page table determines page frame number Present/Absent bit if page is in physical memory What happens, if program references an unmapped address? (page fault) 51
Relationship between virtual and physical memory virtual address space Addresses No. 12.288 16.383 60KB-64KB page fault!... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 MMU checks VM page fault! 28KB-32KB... 4KB-8KB 0KB-4KB MOV REG, 12288 page frame 52
Relationship between virtual and physical memory virtual address space Addresses No. 12.288 16.383 page fault! 60KB-64KB e.g.... 4KB-8KB 0KB-4KB 7 6 2 4 5 3 0 1 MOV REG, 12288 MMU checks VM page fault! Choose a rarely used page frame (in phys. mem) Save content on hard drive...001010 100011.. 28KB-32KB... 4KB-8KB 0KB-4KB page frame 53
Relationship between virtual and physical memory virtual address space Addresses No. 12.288 16.383 page fault! 60KB-64KB e.g.... 4KB-8KB 0KB-4KB 7 2 4 5 6 3 0 1 MOV REG, 12288 MMU checks VM page fault Choose a rarely used page frame (in phys. mem) Save content on hard drive Load requested page into phys. Mem...111010 100100.. 28KB-32KB... 4KB-8KB 0KB-4KB page frame 54
Relationship between virtual and physical memory virtual address space Addresses No. 12.288 16.383 page fault! 60KB-64KB e.g.... 4KB-8KB 0KB-4KB 7 2 4 5 6 3 0 1 MOV REG, 12288 MMU checks VM page fault Choose a rarely used page frame (in phys. mem) Save content on hard drive Load requested page into phys. Mem...111010 100100.. 28KB-32KB... 4KB-8KB 0KB-4KB Restart trapped instruction page frame 55
Internal MMU operation 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 4 Bit page number 4 Bit 16 pages present bit 15 000 0 14 13 12 11 10 9 000 000 000 111 000 101 0 0 0 1 0 1 8 000 0 7 000 0 3 Bit page 6 000 0 frame 12 Bit Offset 5 4 3 2 1 0 011 100 000 110 001 010 1 1 1 1 1 1 110 12 Bit 4096 byte 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 56
Structure of a page table entry Strongly machine dependent M modified bit P present/absent bit M R P Prot Page frame number R reference bit Protection bits M/R bit protocol access to the page M-bit is set when a program writes to a page to know if it needs to be saved before physical space can be reused 57
Structure of a page table entry Strongly machine dependent M modified bit P present/absent bit M R P Prot Page frame number R reference bit Protection bits M/R bit protocol access to the page R-bit is set when read/write access occurs for decision if space should be used for overwriting 58
Structure of a page table entry Strongly machine dependent M modified bit P present/absent bit M R P Prot Page frame number R reference bit Protection bits P bit shows if page exists or not 59
Structure of a page table entry Strongly machine dependent M modified bit P present/absent bit M R P Prot Page frame number R reference bit Prot bits are setting rights which operations are allowed on a page E.g.: read, write, execute Protection bits 60
Paging advantages Easy to allocate memory Memory comes from a free list of fixed size chunks Allocating a page is just removing it from the list External fragmentation not a problem Easy to swap out chunks of a program All chunks are the same size Use present bit to detect references to swapped pages Pages are a convenient multiple of the disk block size 61
Paging disadvantages Memory reference overhead 2 references per address lookup (page table, then memory) Solution use a hardware cache of lookups Memory requirements to hold page tables can be significant 32 bit address space á 4KB pages = 2 20 4 bytes/pte = 4MB per page table 25 processes = 100MB just for page tables! Solution multilevel page tables 62
Translation Lookaside Buffer (TLB) Observation, programs have most access on just a few pages Hardware Implementation TLB Part of MMU Small list of most important pages of a process (not more than 64) e.g. variables used in a loop Virtual page number, M-bit, Protect-bit, page frame, Validity Access: check TLB, if needed page can be found If NOT: fetch entry from page table, write entry to TLB (overwrite older entries) 63
Multilevel Page Table To be used for big virtual address spaces Dividing a page table into mulitple tables... 1023...... P=1 2 1 0 P=0 P=0 P=1... Two 4MB tables vs. of 1024*1024 entries 64
Page Replacement Algorithms Page fault! Load page into physical memory! Which page schould be replaced? Answer: pages which are rarely used! Possible Algorithms: Not Recently Used (NRU) Least Recently Used (LRU) First In First Out (FIFO) Second Chance (SC) 65
Not Recently Used Algorithm (NRU) Using status bits referenced (R) AND modified (M) Page Index M R P Prot Page frame Offset 5 0 1 1 111 1001 10001010101 4 1 0 1 111 1011 00000100001 3 0 0 1 111 0111 00011000001 2 1 0 1 111 0001 01110001001 1 0 0 1 111 1010 00000001001 0 1 1 1 111 0101 00110000001 R-bit cleared periodically by OS M-bit cleared when hard disk updated Clearing random Entry of smallest availabe class: Best entry class 0. here: entry 1 or 3 is cleared! Class 0: R=0, M=0 Class 1: R=0, M=1 Class 2: R=1, M=0 Class 3: R=1, M=1 66
First In First Out Algorithm (FIFO) Oldest page entry (that loaded first) Page Index M R P Prot Page frame Offset 5 0 1 1 111 1001 10001010101 4 1 0 1 111 1011 00000100001 3 0 0 1 111 0111 00011000001 2 1 0 1 111 0001 01110001001 1 0 0 1 111 1010 00000001001 0 1 1 1 111 0101 00110000001 Sorted list ordered by loading time Clear oldest entry no matter if referenced or modified Loaded 12 256 124 116 261 45 here: entry 5 67
Second Chance Algorithm (SC) Enhancement of FIFO (by using R-Bit) Page Index M R P Prot Page frame Offset 5 0 1 1 111 1001 10001010101 4 1 0 1 111 1011 00000100001 3 0 0 1 111 0111 00011000001 2 1 0 1 111 0001 01110001001 1 0 0 1 111 1010 00000001001 0 1 1 1 111 0101 00110000001 Linked list ordered by loading time Clear oldest entry if not referenced Loaded 12 256 124 116 261 45 If entry is referenced clear R-bit and continue searching here: entry 5 and 0 will be unreferenced AND loading time reset, then, entry 2 will be cleared and used 68
Least Recently Used Algorithm (LRU) Clears page that has been unused for the longest time Page Index M R P Prot Page frame Offset 5 0 1 1 111 1001 10001010101 4 1 0 1 111 1011 00000100001 3 0 0 1 111 0111 00011000001 2 1 0 1 111 0001 01110001001 1 0 0 1 111 1010 00000001001 0 1 1 1 111 0101 00110000001 Linked list ordered by access time based on asusmption, that pages used in the last few instructions will be used in the next few future instructions Problem: Entries have to be updated every access linked-list must be reordered ;-( here: entry 2 is cleared. (updated on hard drive first) Loaded 12 256 124 116 261 45 Access 245 256 163 116 267 245 69
Questions? 70
Resources Tanenbaum, Andrew S., Modern Operating Systems, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2008. Tanenbaum, Andrew S., Moderne Bertriebssysteme, 3rd edition, Pearson Education Inc, Amsterdam, Netherlands, 2009. Lee, Insup, CsE 380 Computer Operating Systems, Lecture Notes, University of Pennsylvania, 2002. Snoeren, Alex C., Lecture 10: Memory Management, Lecture Notes, UC San Diego, 2010. 71