Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware MAXsys Ø Starting at address 0, going to address MAXsys Logical/virtual address space — A process’s view of its own memory Memory Management Basics MAXprog Program P Ø Starting at address 0, going to address MAXprog 0 But where do addresses come from? MOV r0, @0xfffa620e 0 1 2 Basic Concepts Address generation The compilation pipeline Which is bigger, physical or virtual address space? 0 Ø A. Physical address space Ø B. Virtual address space Ø C. It depends on the system. 1000 Library Routines Library Routines prog P : : foo() : : end P 0 P: : push ... inc SP, 4 jmp 75 : ... 75 : push ... inc SP, x jmp _foo : foo: ... Compilation 100 Assembly 175 1100 : : : jmp 175 : ... Linking 1175 : : : jmp 1175 : ... Loading 3 4 Basic Concepts (Cont’d.) Program Relocation Address Translation Program issues virtual addresses Machine has physical addresses. If virtual == physical, then how can we have multiple programs resident concurrently? Instead, relocate virtual addresses to physical at run time. MAXsys MEMORY EXCEPTION CPU Ø While we are relocating, also bounds check addresses for safety. Logical Addresses Instructions MAXprog I can relocate that program (safely) in two registers… Program P’s logical address space 0 5 no ≤ 500 yes + Physical Addresses 1000 1500 Program P’s physical address 1000 space Limit Base Register Register 0 6 Evaluating Dynamic Allocation Techniques The fragmentation problem External fragmentation With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. Ø Unused memory between units of allocation Ø E.g, two fixed tables for 2, but a party of 4 Ø A. True Ø B. False MAX Internal fragmentation Ø Unused memory within a unit of allocation Ø E.g., a party of 3 at a table for 4 Program Code (“text”) Program Q’s PAS Execution Data Stack Execution Stack Program 0 R’s PAS 7 Simple Memory Management Schemes First Fit Allocation Dynamic allocation of partitions Simple approach: Ø Allocate a partition when a process is admitted into the system Ø Allocate a contiguous memory partition to the process OS keeps track of... Full-blocks Empty-blocks (“holes”) 8 MAX Program P1 To allocate n bytes, use the first available free block such that the block size is larger than n. Program P2 P5 1K bytes 2K bytes 2K bytes 500 bytes 500 bytes Program P3 Allocation strategies First-fit Best-fit Worst-fit To allocate 400 bytes, we use the 1st free block available Program P4 0 9 Rationale & Implementation Best Fit Allocation Simplicity of implementation To allocate n bytes, use the smallest available free block such that the block size is larger than n. Requires: Ø Free block list sorted by address Ø Allocation requires a search for a suitable partition Ø De-allocation requires a check to see if the freed partition could be merged with adjacent free partitions (if any) Advantages ◆ ◆ Simple Tends to produce larger free blocks toward the end of the address space 10 1K bytes 1K bytes 2K bytes 2K bytes Disadvantages ◆ ◆ Slow allocation External fragmentation To allocate 400 bytes, we use the 3rd free block available (smallest) 11 500 bytes 12 Rationale & Implementation Worst Fit Allocation To avoid fragmenting big free blocks To allocate n bytes, use the largest available free block such that the block size is larger than n. To minimize the size of external fragments produced Requires: Ø Free block list sorted by size Ø Allocation requires search for a suitable partition Ø De-allocation requires search + merge with adjacent free partitions, if any ◆ 1K bytes 2K bytes Disadvantages Advantages ◆ 1K bytes Works well when most allocations are of small size Relatively simple ◆ ◆ ◆ External fragmentation Slow de-allocation Tends to produce many useless tiny fragments (not really great) To allocate 400 bytes, we use the 2nd free block available (largest) 500 bytes Doug Lea’s malloc “In most ways this malloc is a best-fit allocator” 13 Rationale & Implementation Allocation strategies To avoid having too many tiny fragments First fit, best fit and worst fit all suffer from external fragmentation. Requires: Ø A. True Ø B. False Ø Free block list sorted by size Ø Allocation is fast (get the largest partition) Ø De-allocation requires merge with adjacent free partitions, if any, and then adjusting the free block list Advantages ◆ 14 Disadvantages Works best if allocations are of medium sizes ◆ ◆ ◆ Slow de-allocation External fragmentation Tends to break large free blocks such that large partitions cannot be allocated 15 Dynamic Allocation of Partitions Memory Management Eliminating Fragmentation Sharing Between Processes Compaction Ø Relocate programs to coalesce holes MAX Program P1 Swapping Ø Preempt processes & reclaim their memory Ready Suspended suspended queue ready queue ? 16 Ø A single name space per process Ø No sharing Run-Time Program Stack P’s VAS Program Data How can one share code and data between programs without paging? Program P3 Waiting Heap Schemes so far have considered only a single address space per process Program P2 Running semaphore/condition queues 2n-1 Program Text Program P4 0 0 17 Program P’s VAS 18 Supporting Multiple Name Spaces Multiple Name Spaces Segmentation Example — Protection/Fault isolation & sharing 2n-1 New concept: A segment — a memory “object” 2n -1 4 Heap 2n -1 Run-Time Stack A process now addresses objects —a pair (s, addr) 0 3 Ø s — segment number Ø addr — an offset within an object Run-Time Stack 2n -1 Program Data 2n -1 Program Program 0 Text Text ❖ Don’t know size of object, so 32 bits for offset? 0 2 2n -1 Program Data 6 0 1 0 Ø A virtual address space Heap Libraries n2 2 -1 0 0 0 n1 s n5 User Code 0 n addr Segment + Address register scheme 0 s addr Single address scheme 19 Implementing Segmentation Memory Management Basics Physical Memory Base + Limit register scheme Are We Done? Add a segment table containing base & limit register values Program P 20 Segmentation allows sharing … but leads to poor memory utilization CPU s n o 0 32 no 0 ≤ Logical Addresses base Limit Register 500 yes + Ø We might not use much of a large segment, but we must keep the whole thing in memory (bad memory utilization). Ø Suffers from external fragmentation Ø Allocation/deallocation of arbitrary size segments is complex Program P’s Segment How can we improve memory management? 1000 Ø Paging Base 1000 Register limit s STBR 1500 MEMORY EXCEPTION 0 Segment Table 21 22
© Copyright 2025 Paperzz