Chapter 8 9 Main Memory and Virtual Memory

  • Slides: 29
Download presentation
Chapter 8 & 9 Main Memory and Virtual Memory Operating System Concepts – 9

Chapter 8 & 9 Main Memory and Virtual Memory Operating System Concepts – 9 th Edition Silberschatz, Galvin and Gagne © 2013

Background n Program must be brought (from disk) into memory and placed within a

Background n Program must be brought (from disk) into memory and placed within a process for it to be run n Main memory and registers are only storage CPU can access directly Operating System Concepts – 9 th Edition 8. 2 Silberschatz, Galvin and Gagne © 2013

Base and Limit Registers n A pair of base and limit registers define the

Base and Limit Registers n A pair of base and limit registers define the logical address space n CPU must check every memory access generated in user mode to be sure it is between base and limit for that user Operating System Concepts – 9 th Edition 8. 3 Silberschatz, Galvin and Gagne © 2013

Binding of Instructions and Data to Memory n Address binding of instructions and data

Binding of Instructions and Data to Memory n Address binding of instructions and data to memory addresses can happen at three different stages l Compile time: If memory location known a priori, absolute code can be generated; must recompile code if starting location changes l Load time: Must generate relocatable code if memory location is not known at compile time l Execution time: Binding delayed until run time if the process can be moved during its execution from one memory segment to another 4 Need hardware support for address maps (e. g. , base and limit registers) Operating System Concepts – 9 th Edition 8. 4 Silberschatz, Galvin and Gagne © 2013

Logical vs. Physical Address Space n The concept of a logical address space that

Logical vs. Physical Address Space n The concept of a logical address space that is bound to a separate physical address space is central to proper memory management l Logical address – generated by the CPU; also referred to as virtual address l Physical address – address seen by the memory unit n Logical and physical addresses are the same in compile-time and load-time address-binding schemes; logical (virtual) and physical addresses differ in execution-time address-binding scheme n Logical address space is the set of all logical addresses generated by a program n Physical address space is the set of all physical addresses generated by a program Operating System Concepts – 9 th Edition 8. 5 Silberschatz, Galvin and Gagne © 2013

Memory-Management Unit (MMU) n Hardware device that at run time maps virtual to physical

Memory-Management Unit (MMU) n Hardware device that at run time maps virtual to physical address n The user program deals with logical addresses; it never sees the real physical addresses l Execution-time binding occurs when reference is made to location in memory l Logical address bound to physical addresses Operating System Concepts – 9 th Edition 8. 6 Silberschatz, Galvin and Gagne © 2013

Paging n Physical address space of a process can be noncontiguous; process is allocated

Paging n Physical address space of a process can be noncontiguous; process is allocated physical memory whenever the latter is available l Avoids external fragmentation l Avoids problem of varying sized memory chunks n Divide physical memory into fixed-sized blocks called frames l Size is power of 2, between 512 bytes and 16 Mbytes n Divide logical memory into blocks of same size called pages n Keep track of all free frames n To run a program of size N pages, need to find N free frames and load program n Set up a page table to translate logical to physical addresses n Backing store likewise split into pages n Still have Internal fragmentation Operating System Concepts – 9 th Edition 8. 7 Silberschatz, Galvin and Gagne © 2013

Address Translation Scheme n Address generated by CPU is divided into: l Page number

Address Translation Scheme n Address generated by CPU is divided into: l Page number (p) – used as an index into a page table which contains base address of each page in physical memory l Page offset (d) – combined with base address to define the physical memory address that is sent to the memory unit l For given logical address space 2 m and page size 2 n Operating System Concepts – 9 th Edition 8. 8 Silberschatz, Galvin and Gagne © 2013

Paging Model of Logical and Physical Memory Operating System Concepts – 9 th Edition

Paging Model of Logical and Physical Memory Operating System Concepts – 9 th Edition 8. 9 Silberschatz, Galvin and Gagne © 2013

Paging (Cont. ) n Calculating internal fragmentation l Page size = 2, 048 bytes

Paging (Cont. ) n Calculating internal fragmentation l Page size = 2, 048 bytes l Process size = 72, 766 bytes l 35 pages + 1, 086 bytes l Internal fragmentation of 2, 048 - 1, 086 = 962 bytes l Worst case fragmentation = 1 frame – 1 byte l On average fragmentation = 1 / 2 frame size l So small frame sizes desirable? l But each page table entry takes memory to track l Page sizes growing over time 4 Solaris supports two page sizes – 8 KB and 4 MB n Process view and physical memory now very different n By implementation process can only access its own memory Operating System Concepts – 9 th Edition 8. 10 Silberschatz, Galvin and Gagne © 2013

Shared Pages n Shared code l One copy of read-only (reentrant) code shared among

Shared Pages n Shared code l One copy of read-only (reentrant) code shared among processes (i. e. , text editors, compilers, window systems) l Similar to multiple threads sharing the same process space l Also useful for interprocess communication if sharing of read-write pages is allowed n Private code and data l Each process keeps a separate copy of the code and data l The pages for the private code and data can appear anywhere in the logical address space Operating System Concepts – 9 th Edition 8. 11 Silberschatz, Galvin and Gagne © 2013

Background (Cont. ) n Virtual memory – separation of user logical memory from physical

Background (Cont. ) n Virtual memory – separation of user logical memory from physical memory l Only part of the program needs to be in memory for execution l Logical address space can therefore be much larger than physical address space l Allows address spaces to be shared by several processes l Allows for more efficient process creation l More programs running concurrently l Less I/O needed to load or swap processes Operating System Concepts – 9 th Edition 8. 12 Silberschatz, Galvin and Gagne © 2013

Background (Cont. ) n Virtual address space – logical view of how process is

Background (Cont. ) n Virtual address space – logical view of how process is stored in memory l Usually start at address 0, contiguous addresses until end of space l Meanwhile, physical memory organized in page frames l MMU must map logical to physical n Virtual memory can be implemented via: l Demand paging l Demand segmentation Operating System Concepts – 9 th Edition 8. 13 Silberschatz, Galvin and Gagne © 2013

Virtual-address Space n Usually design logical address space for stack to start at Max

Virtual-address Space n Usually design logical address space for stack to start at Max logical address and grow “down” while heap grows “up” l Maximizes address space use l Unused address space between the two is hole 4 No physical memory needed until heap or stack grows to a given new page n Enables sparse address spaces with holes left for growth, dynamically linked libraries, etc n System libraries shared via mapping into virtual address space n Shared memory by mapping pages readwrite into virtual address space n Pages can be shared during fork(), speeding process creation Operating System Concepts – 9 th Edition 8. 14 Silberschatz, Galvin and Gagne © 2013

Demand Paging n Could bring entire process into memory at load time n Or

Demand Paging n Could bring entire process into memory at load time n Or bring a page into memory only when it is needed l Less I/O needed, no unnecessary I/O Less memory needed l Faster response l More users n Similar to paging system with swapping (diagram on right) n Page is needed reference to it l invalid reference abort l not-in-memory bring to memory n Lazy swapper – never swaps a page into memory unless page will be needed l Swapper that deals with pages is a pager l Operating System Concepts – 9 th Edition 8. 15 Silberschatz, Galvin and Gagne © 2013

Basic Concepts n With swapping, pager guesses which pages will be used before swapping

Basic Concepts n With swapping, pager guesses which pages will be used before swapping out again n Instead, pager brings in only those pages into memory n How to determine that set of pages? l Need new MMU functionality to implement demand paging n If pages needed are already memory resident l No difference from non demand-paging n If page needed and not memory resident l Need to detect and load the page into memory from storage 4 Without changing program behavior 4 Without programmer needing to change code Operating System Concepts – 9 th Edition 8. 16 Silberschatz, Galvin and Gagne © 2013

Page Table When Some Pages Are Not in Main Memory Operating System Concepts –

Page Table When Some Pages Are Not in Main Memory Operating System Concepts – 9 th Edition 8. 17 Silberschatz, Galvin and Gagne © 2013

Page Fault n If there is a reference to a page, first reference to

Page Fault n If there is a reference to a page, first reference to that page will 1. 2. 3. 4. 5. trap to operating system: page fault Operating system looks at another table to decide: l Invalid reference abort l Just not in memory Find free frame Swap page into frame via scheduled disk operation Reset tables to indicate page now in memory Set validation bit = v Restart the instruction that caused the page fault Operating System Concepts – 9 th Edition 8. 18 Silberschatz, Galvin and Gagne © 2013

Page Replacement n Prevent over-allocation of memory by modifying page- fault service routine to

Page Replacement n Prevent over-allocation of memory by modifying page- fault service routine to include page replacement n Use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk n Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory Operating System Concepts – 9 th Edition 8. 19 Silberschatz, Galvin and Gagne © 2013

Need For Page Replacement Operating System Concepts – 9 th Edition 8. 20 Silberschatz,

Need For Page Replacement Operating System Concepts – 9 th Edition 8. 20 Silberschatz, Galvin and Gagne © 2013

Basic Page Replacement 1. Find the location of the desired page on disk 2.

Basic Page Replacement 1. Find the location of the desired page on disk 2. Find a free frame: - If there is a free frame, use it - If there is no free frame, use a page replacement algorithm to select a victim frame - Write victim frame to disk if dirty 3. Bring the desired page into the (newly) free frame; update the page and frame tables 4. Continue the process by restarting the instruction that caused the trap Note now potentially 2 page transfers for page fault – increasing EAT Operating System Concepts – 9 th Edition 8. 21 Silberschatz, Galvin and Gagne © 2013

Page Replacement Operating System Concepts – 9 th Edition 8. 22 Silberschatz, Galvin and

Page Replacement Operating System Concepts – 9 th Edition 8. 22 Silberschatz, Galvin and Gagne © 2013

First-In-First-Out (FIFO) Algorithm n Reference string: 7, 0, 1, 2, 0, 3, 0, 4,

First-In-First-Out (FIFO) Algorithm n Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3, 2, 1, 2, 0, 1, 7, 0, 1 n 3 frames (3 pages can be in memory at a time per process) 15 page faults n Can vary by reference string: consider 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 l Adding more frames can cause more page faults! 4 Belady’s Anomaly n How to track ages of pages? l Just use a FIFO queue Operating System Concepts – 9 th Edition 8. 23 Silberschatz, Galvin and Gagne © 2013

Optimal Algorithm n Replace page that will not be used for longest period of

Optimal Algorithm n Replace page that will not be used for longest period of time l 9 is optimal for the example n How do you know this? l Can’t read the future n Used for measuring how well your algorithm performs Operating System Concepts – 9 th Edition 8. 24 Silberschatz, Galvin and Gagne © 2013

Least Recently Used (LRU) Algorithm n Use past knowledge rather than future n Replace

Least Recently Used (LRU) Algorithm n Use past knowledge rather than future n Replace page that has not been used in the most amount of time n Associate time of last use with each page n 12 faults – better than FIFO but worse than OPT n Generally good algorithm and frequently used n But how to implement? Operating System Concepts – 9 th Edition 8. 25 Silberschatz, Galvin and Gagne © 2013

LRU Algorithm (Cont. ) n Counter implementation l Every page entry has a counter;

LRU Algorithm (Cont. ) n Counter implementation l Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter l When a page needs to be changed, look at the counters to find smallest value 4 Search through table needed n Stack implementation l Keep a stack of page numbers in a double link form: l Page referenced: 4 move it to the top 4 requires 6 pointers to be changed l But each update more expensive l No search for replacement n LRU and OPT are cases of stack algorithms that don’t have Belady’s Anomaly Operating System Concepts – 9 th Edition 8. 26 Silberschatz, Galvin and Gagne © 2013

Use Of A Stack to Record Most Recent Page References Operating System Concepts –

Use Of A Stack to Record Most Recent Page References Operating System Concepts – 9 th Edition 8. 27 Silberschatz, Galvin and Gagne © 2013

LRU Approximation Algorithms n LRU needs special hardware and still slow n Reference bit

LRU Approximation Algorithms n LRU needs special hardware and still slow n Reference bit l With each page associate a bit, initially = 0 l When page is referenced bit set to 1 l Replace any with reference bit = 0 (if one exists) 4 We do not know the order, however n Second-chance algorithm l Generally FIFO, plus hardware-provided reference bit l Clock replacement l If page to be replaced has 4 Reference 4 reference bit = 0 -> replace it bit = 1 then: – set reference bit 0, leave page in memory – replace next page, subject to same rules Operating System Concepts – 9 th Edition 8. 28 Silberschatz, Galvin and Gagne © 2013

Second-Chance (clock) Page-Replacement Algorithm Operating System Concepts – 9 th Edition 8. 29 Silberschatz,

Second-Chance (clock) Page-Replacement Algorithm Operating System Concepts – 9 th Edition 8. 29 Silberschatz, Galvin and Gagne © 2013