340 likes | 389 Views
Memory Management Techniques. Single Contiguous Overlays Fixed (static) partitions Relocation (Dynamic) Partitions Paging Demand Paging Segmented Segmented/ Demand Paging For each technique, observe: Algorithms, Advantages, Disadvantages and Special Requirements. 1. Single Contiguous.
E N D
Memory Management Techniques • Single Contiguous • Overlays • Fixed (static) partitions • Relocation (Dynamic) Partitions • Paging • Demand Paging • Segmented • Segmented/ Demand Paging • For each technique, observe: Algorithms, Advantages, Disadvantages and Special Requirements
1. Single Contiguous While (job is ready) Do If (JobSize <= MemorySize) Then Begin Allocate Memory Load and Execute Job Deallocate Memory End Else Error Advantages: Simplicity Disadvantages CPU wasted, Main memory not fully used, Limited job size
II Overlays Programs can be sectioned into modules Not all modules need to be in main memory at the same time A B E C D Programmer specifies which modules can overlay each other Linker inserts commands to invoke the loader when the modules are referenced The “parent” must stay in memory
Overlays(Cont) Advantages: Reduced memory usage Disadvantages: Overlap map must be specified by programmer Programmer must know memory requirements Overlapped modules must be completely disjoint
Fixed Partition(1) Must ensure that a process does not access memory space dedicated to another process Base Register – Holds the address of the beginning of the partition Bounds Register – Hold the size of the job Each relative address is compared to the bounds register and if in range it is added to the base register to produce a physical address
Fixed Partition(2) Partition A A Virtual Address + A + L Bounds Reg L
Dynamic Partitions(1) • Consider the following scenario (100K memory): • Job 1 arrives; size = 22 K • Job 2 arrives; size = 24 K • Job 3 arrives; size = 30 K • Job 4 arrives; size = 10 K • Job 1 terminates • Job 3 terminates • Job 5 terminates
Dynamic Partitions(2) Lets trace our what happens: 0 100 Where should job 5 be put? What are pros/cons of each placement?
Table Sort Illustration(1) FREE – 22K a IN USE – 24K b FREE - 30K c IN USE – 10K d FREE 14K e
Table Sort Illustration(2) First fit Start addr Length A 22 C 30 E 14 Best fit Start addr Length E 14 A 22 C 30 Worst fit Start addr Length C 30 A 22 E 14
Partition Selection Algorithm • Implementation requires a free block table • Sorting table in a particular manner results in a specific selection algorithm • First FitSort by location • Best FitSort by size (ascending)[don’t break up big blocks) • Worst FitSort by size (descending)[break up big blocks]
What if we cannot find a big enough hole for an arriving job? Suppose a 35K job arrives? Suppose a 90K job arrives? 22K Free 24K 2 30K Free 4 10K 14K Free
Compaction Shuffle jobs to create larger contiguous free memory Do the values of pointers need changing? Consider the arrival of a 40K job: Job A 15K Job A 15K Job B 20K Job B 20K Job C 7K Job C 7K
Dynamic Partitions • Requires two OS operations: • Allocation: • Form a partition from a free partition of ample size • Deallocation: • Return partition to free table and merge where possible
Pros/Cons of Dynamic Partitions Advantages: Efficient memory usage Disadvantages: Partition Management Compaction or external fragmentation
Demand Page Management • Page Map Table (PMT) • Maps page to block • Status: Main Memory • Secondary Memory • In Transit • 2) Memory Block Table (MBT) • Maps block to page • Reference Bit • Change Bit • 3) File Map Table (FMT) • Maps a job’s pages to secondary memory • PMT for the disk • 1 FMT/ job • 1 entry / page
Demand Paging Schematic(1) What does 3 mean? PMTAR OS 0 3 1 PMT 2 M 0 M 3 1 2 S 4 5 M = In Memory S = On secondary storage I = In transit 6 7
Demand Paging Schematic(2) FMT Disk 0 1 2
Demand Paging Schematic(3) OS 0 Referenced 1 2 J1 P0 R C 3 4 J1 P1 R C 5 6 7 Changed
Sharing Pages of Reentrant Code or Data Between Processes Q. What problem is caused if a shared page frame corresponds to two different page #s? (Hint: consider page replacement) A. Alias or synonym problem
Address Translation Steps: Look up p in TLB if not found look up p in PMT (must be found) Replace p by p’ to form PA
Page Replacement Local VS Global Page Replacement Local Requires that each process removes a page from its own set of allocated blocks Global A replacement page may be selected from the set of all blocks
Locality At any given time, the locality of a process is the set of pages that are actively being used together Spacial There is a high probability that once a location is referenced, the one after it will be accessed in the near future EX: Sequential code, array processing Temporal A referenced location is likely to be accessed again in the near future EX: Loops. Single data elements
Working Set Theory W (t,) = The set of pages referenced between times t and t+ The number of elements in W is called the working Set Size Working set size is an estimation of locality size A job should not be scheduled unless there is room for its entire working set
Page Replacement Algorithms • Optimal Replacement • Replace the page which will not be used for the longest period of time • Lowest page fault rate of all algorithms • Requires future knowledge • 2) FIFO • Replace the “oldest” page • A frequently used page may be swapped out • Belady’s Anomaly: • For some page replacement algorithms, the page fault rate may increase as the number of blocks increase
Page Trace: 4 3 2 1 4 3 5 4 3 2 1 5 4 3 2 1 4 3 5 4 3 2 1 5 0 1 2 4 3 2 1 4 3 5 4 3 2 1 5 0 1 2 3
Replacement Algorithms Continued 3) Least Recently Used (LRU) Uses the recent past as an approximation of the near future Statck algorithm Does NOT suffer from Belady’s Anomaly Hardware/ Overhead intensive 4) LRU Approximation Uses reference bits in the MBT and a static reference pointer(RP) The reference pointer is not reinitialized between calls to LRU Approximation
LRU Approximation Algorithm Initially: RP 0; Begin Done := FALSE; While (NOT Done) Do If (MBT[RP].ref = 1) Then MBT [RP].ref := 0 Else Begin Return(RP); Done :== TRUE; End; RP:=RP+1; End;
LRU Approximation Example Page Trace: 4 3 2 1 4 3 5 4 2 1 5 NOTE: Slightly different from previous example Page Reference Bit 0 1 2 RP = 0 Page Fault Rate =
Page Replacement Local VS Global page replacement Local Requires that each process remove a page from its own set of allocated blocks Global A replacement page may be selected from the set of all blocks
VIII Segmentation • An alternative to paging • Recall variable Size partitioning: • Program allocation contiguous • Use first fit, etc • Segmentation: • Put subroutines, arrays, etc. into separate segments • Each segment must occupy contiguous locations, but segments may be scattered throughout memory • Segmentation = Paging with variable size pages • Why do this? What problems arise?
VIII Segmentation Divide each process into logical segments (procedures, arrays, etc) Logical breakdown gives an intuitive structure to main memory Managing segments: Segment Map Table ( SMT ) Length Address 1 SMT/job 1 entry/segment