310 likes | 459 Views
CMT603. Lecture 9 Memory Management 2. Recap. Addressing an Address binding Logical and Physical Addressing Memory Management Unit Contiguous Addressing Fixed Partition Variable Partition Non-Contiguous Addressing Paging Segmentation. Contents. Segmentation Why ? How ?
E N D
CMT603 Lecture 9 Memory Management 2
Recap • Addressing an Address binding • Logical and Physical Addressing • Memory Management Unit • Contiguous Addressing • Fixed Partition • Variable Partition • Non-Contiguous Addressing • Paging • Segmentation
Contents • Segmentation • Why ? • How ? • Virtual Memory • Why? • Implementation • Page Faults
Segmentation • Paging separates user’s view of memory from the actual physical memory • The user views memory as a collection of variable-sized segments, with no necessary ordering among segments • Segmentation supports this user view of memory.
Users view of Memory Main Program Segment 0 Sub 1 Sub 2 Segment 1 Segment 2 Sub 3 Segment 3
Segmentation Hardware Physical memory Segmentation Table s Limit Base CPU s d < + Addressing Error
An Example Physical memory Segmentation Table s 256 3000 CPU 4 212 3000 3212 < + 3256 Addressing Error
A View of the Memory Physical memory 0 OS 1600 2000 Sub 1 Main Program 2400 Segmentation Table Segment 0 3000 Main Program Sub 1 Sub 2 Segment 1 3800 Segment 2 4100 Sub 3 Sub 3 4500 Segment 3 4800 Sub 2 5000
Fragmentation • Segmentation may cause external fragmentation. • When all blocks of free memory are too small to accommodate a segment, the process may simply have to wait until more memory become available. • Solution: • Memory compaction • Page the segments – Segmentation with paging
Segmentation With Paging • Segments are arranged across multiple pages. • A logical memory address is presented as an ordered triple (s, p, d), where s is the segment number, p is the page number, and d is the displacement within the page
Page size = Frame size = 32 bytes Segmentation With Paging Physical memory CPU s p d Segmentation Table s Limit Page Table Base b Page Table < + f d f b+p
Page size = Frame size = 32 bytes Example Physical memory CPU 4 6 28 Physical Address 20*32+28=668 Segmentation Table 4 Page Table 12 24 24 190 25 100 26 34 < + 20 28 27 20 b+p 28 100 29 34 30 20
Fragmentation • It may cause internal fragmentation
Virtual Memory • Previous memory allocation strategies have the same goal: • To keep many processes in memory simultaneously to support multiprogramming. • Requires the entire process to be in memory before its execution.
Virtual Memory • Benefits of the ability to execute a program that is only partially in memory: • A program is no longer constrained by the amount of physical memory that is available. • More programs can be run at the same time.
Virtual Memory • Virtual memory is a technique that allows the execution of processes that may not be completely in memory. • It allows a large virtual memory to be provided for programmers when only a smaller physical memory is available
Implementation:Demand paging scheme • A process is viewed as a set of sequential pages. It resides on secondary memory (disk). • A page is never brought into memory unless it will be needed. • Add valid-invalid bit into page table. • “valid” indicates that the associated page is both legal and in memory. • “invalid” indicates that the associated page is not in memory.
Diagram of Virtual Memory Page Table A B F C G D E Page-fault Trap Logical Memory Hard Disk Physical Memory
Handling Page Faults • Access to a page marked invalid causes a page-fault trap to the OS • 1. Find a free frame. • 2. Read the desired page into the frame. • 3. Reset the page table to indicate that the page is in memory. • 4. Restart the instruction that was interrupted by the page-fault trap. • The process now can access the page as though it had always been in memory
Handling Page Faults C i v 0 A B F C G D E Logical Memory
Need for Page Replacement • Suppose a page fault occurs, and a • desired page that is residing on the • disk is required by a process, but • there is no free frame
Page Replacement Page Table C P A P B F C G D E Logical Memory Hard Disk Physical Memory
Page Replacement • Reduce the overhead by using a “dirty” bit: • A dirty bit is associated with each page • The dirty bit is set whenever there is any modification in the page • When a page is selected for replacement, the page is written to the disk only if its dirty bit is set.
Page-Replacement Algorithms:Evaluation method • The lower the page-fault rate the better. • Evaluation: Run on a string of memory references and count the number of page faults. For example 7, 8, 0, 6, 5, 4, 6, 9, 2, 3, 3 • Each number is a reference to a page. Question What algorithms could we use
First-In First-Out (FIFO) Algorithm • Create a FIFO queue to hold all pages in memory. Always replace the page at the head of the queue, and insert a new page at the tail of the queue. • Assume there are 3 frames in memory. If run on the reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 using FIFO algorithm, how many page faults?
First-In First-Out (FIFO) Algorithm number of page faults = 15 • Bad replacement choices increase the page-fault rate: • The page replaced may be an active page in constant use. • After paging out this page to bring a new one, a page fault may occur immediately.
Optimal Algorithm • Replace the page that will not be used for the longest period of time. • This has the lowest page-fault rate. • Assume there are 3 frames in memory. If run on a reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 using optimal algorithm, how many page faults?
Optimal Algorithm number of page faults = 9 • Optimal algorithm is difficult to implement, because it requires future knowledge of the reference string.
Least Recently Used (LRU) Algorithm • Replace the page that has not been used for the longest period of time. • An approximation to the optimal algorithm. • Assume there are 3 frames in memory. If run on a reference string 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1 using LRU algorithm, how many page faults?
Least Recently Used (LRU) Algorithm number of page faults =12 • Better performance at the cost of system overhead
Summary • Segmentation • Why ? • How ? • Virtual Memory • Why? • Implementation • Page Faults