Chapter 4: Memory Management Part 1: Mechanisms for Managing Memory
Memory management n Basic memory management n Swapping n Virtual memory n Page replacement algorithms n Modeling page replacement algorithms n Design issues for paging systems n Implementation issues n Segmentation Chapter 4 CS 1550, cs.pitt.edu 2 (originaly modified by Ethan
In an ideal world… n The ideal world has memory that is n Very large n Very fast n Non-volatile (doesn’t go away when power is turned off) n The real world has memory that is: n Very large n Very fast n Affordable! Þ Pick any two… n Memory management goal: make the real world look as much like the ideal world as possible Chapter 4 CS 1550, cs.pitt.edu 3 (originaly modified by Ethan
Memory hierarchy n What is the memory hierarchy ? n Different levels of memory n Some are small & fast n Others are large & slow n What levels are usually included? n Cache: small amount of fast, expensive memory n L1 (level 1) cache: usually on the CPU chip n L2 & L3 cache: off-chip, made of SRAM n Main memory: medium-speed, medium price memory (DRAM) n Disk: many gigabytes of slow, cheap, non-volatile storage n Memory manager handles the memory hierarchy Chapter 4 CS 1550, cs.pitt.edu 4 (originaly modified by Ethan
Basic memory management n Components include n Operating system (perhaps with device drivers) n Single process n Goal: lay these out in memory n Memory protection may not be an issue (only one program) n Flexibility may still be useful (allow OS changes, etc.) n No swapping or paging 0xFFFF 0xFFFF Device drivers Operating system (ROM) User program (ROM) (RAM) User program (RAM) User program Operating system (RAM) Operating system (RAM) (RAM) 0 0 Chapter 4 CS 1550, cs.pitt.edu 5 (originaly modified by Ethan
Fixed partitions: multiple programs n Fixed memory partitions n Divide memory into fixed spaces n Assign a process to a space when it’s free n Mechanisms n Separate input queues for each partition n Single input queue: better ability to optimize CPU usage 900K 900K Partition 4 Partition 4 700K 700K Partition 3 Partition 3 600K 600K Partition 2 Partition 2 500K 500K Partition 1 Partition 1 100K 100K OS OS 0 0 Chapter 4 CS 1550, cs.pitt.edu 6 (originaly modified by Ethan
How many programs is enough? n Several memory partitions (fixed or variable size) n Lots of processes wanting to use the CPU n Tradeoff n More processes utilize the CPU better n Fewer processes use less memory (cheaper!) n How many processes do we need to keep the CPU fully utilized? n This will help determine how much memory we need n Is this still relevant with memory costing less than $1/GB? Chapter 4 CS 1550, cs.pitt.edu 7 (originaly modified by Ethan
Modeling multiprogramming n More I/O wait means less 1 processor utilization 0.9 n At 20% I/O wait, 3–4 0.8 processes fully utilize CPU 0.7 n At 80% I/O wait, even 10 CPU Utilization 0.6 processes aren’t enough 0.5 n This means that the OS 0.4 should have more 0.3 processes if they’re I/O 0.2 0.1 bound 0 n More processes => 0 1 2 3 4 5 6 7 8 9 10 Degree of Multiprogramming memory management & 80% I/O Wait 50% I/O Wait 20% I/O Wait protection more important! Chapter 4 CS 1550, cs.pitt.edu 8 (originaly modified by Ethan
Multiprogrammed system performance n Arrival and work requirements of 4 jobs n CPU utilization for 1–4 jobs with 80% I/O wait n Sequence of events as jobs arrive and finish n Numbers show amount of CPU time jobs get in each interval n More processes => better utilization, less time per process Job Arrival CPU time needed 1 2 3 4 1 10:00 4 CPU idle 0.80 0.64 0.51 0.41 2 10:10 3 CPU busy 0.20 0.36 0.49 0.59 3 10:15 2 CPU/process 0.20 0.18 0.16 0.15 4 10:20 2 0 Time 10 15 20 22 27.6 28.2 31.7 1 2 3 4 Chapter 4 CS 1550, cs.pitt.edu 9 (originaly modified by Ethan
Memory and multiprogramming n Memory needs two things for multiprogramming n Relocation n Protection n The OS cannot be certain where a program will be loaded in memory n Variables and procedures can’t use absolute locations in memory n Several ways to guarantee this n The OS must keep processes’ memory separate n Protect a process from other processes reading or modifying its own memory n Protect a process from modifying its own memory in undesirable ways (such as writing to program code) Chapter 4 CS 1550, cs.pitt.edu 10 (originaly modified by Ethan
Base and limit registers 0xFFFF Special CPU registers: base & n 0x2000 limit Limit n Access to the registers limited to Process system mode partition n Registers contain n Base: start of the process’s Base memory partition 0x9000 n Limit: length of the process’s memory partition Address generation n n Physical address: location in OS actual memory 0 n Logical address: location from the process’s point of view n Physical address = base + logical Logical address: 0x1204 address Physical address: n Logical address larger than limit 0x1204+0x9000 = 0xa204 => error Chapter 4 CS 1550, cs.pitt.edu 11 (originaly modified by Ethan
Swapping C C C C C B B B B A A A A D D D OS OS OS OS OS OS OS n Memory allocation changes as n Processes come into memory n Processes leave memory n Swapped to disk n Complete execution n Gray regions are unused memory Chapter 4 CS 1550, cs.pitt.edu 12 (originaly modified by Ethan
Swapping: leaving room to grow n Need to allow for programs Stack to grow Room for n Allocate more memory for Process B to grow data B n Larger stack Data n Handled by allocating more Code space than is necessary at Stack the start Room for n Inefficient: wastes memory Process A to grow that’s not currently in use A Data n What if the process requests too much memory? Code OS Chapter 4 CS 1550, cs.pitt.edu 13 (originaly modified by Ethan
Tracking memory usage: bitmaps Keep track of free / allocated memory regions with a bitmap n n One bit in map corresponds to a fixed-size region of memory n Bitmap is a constant size for a given amount of memory regardless of how much is allocated at a particular time Chunk size determines efficiency n n At 1 bit per 4KB chunk, we need just 256 bits (32 bytes) per MB of memory n For smaller chunks, we need more memory for the bitmap n Can be difficult to find large contiguous free areas in bitmap A B C D 8 16 24 32 Memory regions 11111100 00111000 01111111 Bitmap 11111000 Chapter 4 CS 1550, cs.pitt.edu 14 (originaly modified by Ethan
Tracking memory usage: linked lists Keep track of free / allocated memory regions with a linked list n n Each entry in the list corresponds to a contiguous region of memory n Entry can indicate either allocated or free (and, optionally, owning process) n May have separate lists for free and allocated areas Efficient if chunks are large n n Fixed-size representation for each region n More regions => more space needed for free lists A B C D 8 16 24 32 Memory regions A 0 6 - 6 4 B 10 3 - 13 4 C 17 9 D 26 3 - 29 3 Chapter 4 CS 1550, cs.pitt.edu 15 (originaly modified by Ethan
Allocating memory Search through region list to find a large enough space n Suppose there are several choices: which one to use? n n First fit: the first suitable hole on the list n Next fit: the first suitable after the previously allocated hole n Best fit: the smallest hole that is larger than the desired region (wastes least space?) n Worst fit: the largest available hole (leaves largest fragment) Option: maintain separate queues for different-size holes n Allocate 20 blocks first fit Allocate 13 blocks best fit Allocate 12 blocks next fit Allocate 15 blocks worst fit 1 5 18 - 6 5 - 19 14 - 52 25 - 102 30 - 135 16 - 202 10 - 302 20 - 350 30 - 411 19 - 510 3 15 Chapter 4 CS 1550, cs.pitt.edu 16 (originaly modified by Ethan
Freeing memory n Allocation structures must be updated when memory is freed n Easy with bitmaps: just set the appropriate bits in the bitmap n Linked lists: modify adjacent elements as needed n Merge adjacent free regions into a single region n May involve merging two regions with the just-freed area A X B A B A X A X B B X Chapter 4 CS 1550, cs.pitt.edu 17 (originaly modified by Ethan
Limitations of swapping n Problems with swapping n Process must fit into physical memory (impossible to run larger processes) n Memory becomes fragmented n External fragmentation: lots of small free areas n Compaction needed to reassemble larger free areas n Processes are either in memory or on disk: half and half doesn’t do any good n Overlays solved the first problem n Bring in pieces of the process over time (typically data) n Still doesn’t solve the problem of fragmentation or partially resident processes Chapter 4 CS 1550, cs.pitt.edu 18 (originaly modified by Ethan
Virtual memory n Basic idea: allow the OS to hand out more memory than exists on the system n Keep recently used stuff in physical memory n Move less recently used stuff to disk n Keep all of this hidden from processes n Processes still see an address space from 0 – max address n Movement of information to and from disk handled by the OS without process help n Virtual memory (VM) especially helpful in multiprogrammed system n CPU schedules process B while process A waits for its memory to be retrieved from disk Chapter 4 CS 1550, cs.pitt.edu 19 (originaly modified by Ethan
Recommend
More recommend