Download presentation
Presentation is loading. Please wait.
Published byStanley Sherman Modified over 9 years ago
1
1 Memory Management Basics
2
2 Program P Basic Memory Management Concepts Address spaces Physical address space — The address space supported by the hardware Starting at address 0, going to address MAX sys Logical/virtual address space — A process’s view of its own memory Starting at address 0, going to address MAX prog 0 MAX sys 0 MAX prog MOV r0, @0xfffa620e But where do addresses come from?
3
3 Which is bigger, physical or virtual address space? A. Physical address space B. Virtual address space C. It depends on the system.
4
4 Basic Concepts Address generation The compilation pipeline prog P : foo() : end P prog P : foo() : end P P: : push... inc SP, x jmp _foo : foo:... P: : push... inc SP, x jmp _foo : foo:... : push... inc SP, 4 jmp 75 :... : push... inc SP, 4 jmp 75 :... 0 75 1100 1175 Library Routines 1000 175 Library Routines Library Routines 0 100 CompilationAssemblyLinkingLoading : jmp 1175 :... : jmp 175 :... : jmp 175 :...
5
5 Program Relocation 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. While we are relocating, also bounds check addresses for safety. I can relocate that program (safely) in two registers…
6
6 Basic Concepts (Cont’d.) Address Translation 0 MAX sys Program P’s logical address space Program P’s logical address space 0 MAX prog 1000 1500 CPU + + 1000 Base Register Logical Addresses ≤ ≤ 500 Limit Register MEMORY EXCEPTION Physical Addresses yes no Instructions P’s physical address space
7
7 With base and bounds registers, the OS needs a hole in physical memory at least as big as the process. A. True B. False
8
8 Evaluating Dynamic Allocation Techniques The fragmentation problem External fragmentation Unused memory between units of allocation E.g, two fixed tables for 2, but a party of 4 Internal fragmentation Unused memory within a unit of allocation E.g., a party of 3 at a table for 4 0 MAX Program R’s PAS Program Q’s PAS Execution Stack Program Code (“text”) Data Execution Stack
9
9 Simple Memory Management Schemes 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 0 MAX Program P 2 Program P 3 Program P 1 P5P5 Program P 4 OS keeps track of... Full-blocks Empty-blocks (“holes”) Allocation strategies First-fit Best-fit Worst-fit
10
10 First Fit Allocation To allocate n bytes, use the first available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 1st free block available 2K bytes 500 bytes
11
11 Rationale & Implementation Simplicity of implementation 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 u Simple u Tends to produce larger free blocks toward the end of the address space Disadvantages u Slow allocation u External fragmentation
12
12 Best Fit Allocation To allocate n bytes, use the smallest available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 3rd free block available (smallest) 1K bytes 2K bytes
13
13 Rationale & Implementation To avoid fragmenting big free blocks 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 Advantages u Works well when most allocations are of small size u Relatively simple Disadvantages u External fragmentation u Slow de-allocation u Tends to produce many useless tiny fragments (not really great) Doug Lea’s malloc “In most ways this malloc is a best-fit allocator”
14
14 Worst Fit Allocation To allocate n bytes, use the largest available free block such that the block size is larger than n. 500 bytes 1K bytes 2K bytes To allocate 400 bytes, we use the 2nd free block available (largest) 1K bytes
15
15 Rationale & Implementation To avoid having too many tiny fragments Requires: 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 u Works best if allocations are of medium sizes Disadvantages u Slow de-allocation u External fragmentation u Tends to break large free blocks such that large partitions cannot be allocated
16
16 Allocation strategies First fit, best fit and worst fit all suffer from external fragmentation. A. True B. False
17
17 Dynamic Allocation of Partitions Eliminating Fragmentation Compaction Relocate programs to coalesce holes 0 MAX Program P 2 Program P 3 Program P 1 Program P 4 Suspended suspended queue ready queue semaphore/condition queues Waiting Running Ready ? Swapping Preempt processes & reclaim their memory
18
18 0 2 n -1 Program P’s VAS Memory Management Sharing Between Processes Schemes so far have considered only a single address space per process A single name space per process No sharing Program P’s VAS Program Data Program Text Heap Run-Time Stack How can one share code and data between programs without paging?
19
19 Multiple Name Spaces Example — Protection/Fault isolation & sharing 0 2 n -1 0 2 n 1 -1 0 0 0 2 n 2 -1 2 n 3 -1 2 n 4 -1 0 2 n 6 -1 Libraries 2 n 5 -1 0 Program Data Program Text Heap Run-Time Stack Program Text Program Data Run-Time Stack Heap User Code
20
20 Supporting Multiple Name Spaces Segmentation New concept: A segment — a memory “object” A virtual address space A process now addresses objects —a pair (s, addr) s— segment number addr — an offset within an object Don’t know size of object, so 32 bits for offset? Segment + Address register scheme saddr Single address scheme n1n1 00n2n2 0 s n addr
21
21 Implementing Segmentation Base + Limit register scheme 0 Program 1000 1500 + + 1000 Base Register Logical Addresses ≤ ≤ 500 Limit Register MEMORY EXCEPTION Physical Memory yes no P’s Segment Segment Table s CPU 0n320 so Program P Program P baselimit STBR Add a segment table containing base & limit register values
22
22 Memory Management Basics Are We Done? Segmentation allows sharing … but leads to poor memory utilization 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 How can we improve memory management? Paging
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.