330 likes | 503 Views
CS423UG Operating Systems. Memory Management - V. Indranil Gupta Lecture 17 Oct 3, 2005. Content. Page replacement algorithms Multi-program memory management Segmentation. Review. Page Replacement algorithms Optimal FIFO LRU NRU Second chance. Ad Hoc Techniques.
E N D
CS423UG Operating Systems Memory Management - V Indranil Gupta Lecture 17 Oct 3, 2005
Content • Page replacement algorithms • Multi-program memory management • Segmentation CS 423UG - Operating Systems, Indranil Gupta
Review • Page Replacement algorithms • Optimal • FIFO • LRU • NRU • Second chance CS 423UG - Operating Systems, Indranil Gupta
Ad Hoc Techniques • Separate Page Out from Page In. • Keep a pool of free frames. • When a page is to be paged in, use a free frame. • Read the faulting page and restart the faulting process while page out is occurring. • Write dirty pages to disk whenever the paging device is free and reset the dirty bit. This allows page replacement algorithms to replace clean pages. • Cache paged-out pages in primary memory. • Page out dirty pages as before. • Return pages to a free pool but remember which page frame they are. • If system needs to map page in again, reuse page. • If system needs to page in data, choose any page in pool. • System V implements this strategy CS 423UG - Operating Systems, Indranil Gupta
Frame Allocation: Minimum • How are the page frames allocated to individual virtual memories in a multi- programmed environment? • The simple case would be to allocate a minimum number of frames per process. • most instructions require two operands • include an extra frame for paging out and one for paging in • moves and indirection instructions might require more CS 423UG - Operating Systems, Indranil Gupta
Equal Allocation • Allocate an equal number of frames per job • but jobs use memory unequally • high priority jobs have same number of page frames and low priority jobs • degree of multiprogramming might vary CS 423UG - Operating Systems, Indranil Gupta
Proportional Allocation • Allocate a number of frames per job proportional to job size • Challenge: how do you determine job size: by command parameters, user etc. or dynamically? CS 423UG - Operating Systems, Indranil Gupta
Page Fault Rate Curve (Typical) As page frames per VM space decrease, the page fault rate increases. Exception…? CS 423UG - Operating Systems, Indranil Gupta
Thrashing • Computations have locality (temporal locality and spatial locality). • As page frames decrease, the page frames available are not large enough to contain the locality of the process. • Working set of a process=set of pages it needs to operate without (too many) page faults • The processes start faulting heavily (high page fault rate). • Pages are read in, are used and immediately paged out. CS 423UG - Operating Systems, Indranil Gupta
Thrashing and CPU Utilization • As the page rate goes up, processes get suspended on page-out and page-in queues for the disk. • The system may try to optimize performance by starting new jobs. • Starting new jobs will reduce the number of page frames available to each process, increasing the page fault requests. • System throughput plunges. CS 423UG - Operating Systems, Indranil Gupta
Solution: “Working Set” Approach • The working set model assumes locality. • The Principle of Locality states that a program clusters its access to data and text temporally and spatially. • As the number of page frames increases above some threshold, the page fault rate will drop dramatically. CS 423UG - Operating Systems, Indranil Gupta
Working Set in Action • Algorithm: • if # free frames > working set of some suspended process, then activate process • if working set size of some process increases and no frame is free, suspend process and release all its frames • Keep track of working set: Moving window over reference string used for determination, e.g., last k accesses by this process CS 423UG - Operating Systems, Indranil Gupta
Working Set Example Window size is 12 references, 8 faults CS 423UG - Operating Systems, Indranil Gupta
Working Set Solution • Parameter: time units= locality of reference (typically several clock ticks) • Each page (PTE) has an R bit, and a time of last use (TOLU) byte • Every clock int. clears R bit for each frame • Every page access updates TOLU field and sets R bit for page • On page fault, find a victim: Scan all frames examining R bit • If (R==1) set TOLU to now (current time) • If (R==0 and age=now-TOLU > ) remove this page • If (R==0 and age=now-TOLU <= ) remember the smallest TOLU • If can’t find another page to evict, evict this “smallest one” • If all pages have R==1, evict a random page CS 423UG - Operating Systems, Indranil Gupta
Page Fault FrequencyVersion of Working Set • Assume that if the working set is correct there will not be many page faults. • If page fault rate increases beyond assumed knee of curve, then increase number of page frames available to process. • If page fault rate decreases below foot of knee of curve, then decrease number of page frames available to process. CS 423UG - Operating Systems, Indranil Gupta
Page Fault Frequency Version of Working Set CS 423UG - Operating Systems, Indranil Gupta
WSClock Page Replacement Algorithm • Carr and Hennessey, 1981 • Used very widely (Linux) • Circular list of pages, with a clock hand (pointer) pointing to one of these pages • Initially, list is empty. • As pages are loaded into memory, pages are added to the list • Each entry contains Time of last use as well as reference and dirty bits. • Clock hand updated according to the following algorithm… CS 423UG - Operating Systems, Indranil Gupta
WSClock Page Replacement Algorithm (Contd.) • Algorithm: on a page fault, • (Step 1) Examine the page pointed to by the clock hand • If R bit is 1, then the page has been referenced during the current tick, so it is in working set and not a candidate for removal • If R bit is 0, then check if it is in working set window (i.e., if now-TOLU is less than the working set window size time) • If page is not in the working set and the page is clean, then replace it • If the page is not in working set and page is dirty, request write to disk, and move on to the next page in the circular list. • If page is in Working Set, advance Clock Hand by 1 element and Repeat (Step 1) • Clock hand may cycle around multiple times until some scheduled page write has completed (then that page is chosen as the victim) CS 423UG - Operating Systems, Indranil Gupta
Page Size Considerations • Small pages • Reason • Locality of reference tends to be small (256B) • Less fragmentation (what kind – internal or external?) • Problem • Require large page tables • Large pages • Reason • Small page table • I/O transfers have high seek time, so better to transfer more data per seek • Problem • Internal fragmentation CS 423UG - Operating Systems, Indranil Gupta
Segmentation • Another Virtual Memory scheme • Different from paging • Provide a user with a two dimensional virtual memory • dimension 1: an address space or segment • dimension 2: a collection of segments • addresses look like (s,d) • s=segment number • d=offset within segment • Segment sizes are decided by user CS 423UG - Operating Systems, Indranil Gupta
(4,r) refers to segment 4, location r (1,x) doesn’t exist if x is larger than segment size x r Array A Funct B Stack C Array D Stack E Seg 5 Seg 1 Seg 2 Seg 3 Seg 4 Segmentation CS 423UG - Operating Systems, Indranil Gupta
Use of Segments • Segment for each user entity: stack, code, data, array • Protect each segment independently: stack, code, data, array • Allow each segment to grow independently • Share each segment independently • Cache parts of virtual memory based on execution pattern • Avoid paging inefficiency: avoid fetching pages from disk that may not be used CS 423UG - Operating Systems, Indranil Gupta
Segment Table 2 3 1 1 Cache 2 3 Disk 1 2 3 4 5 Virtual Memory Stored on Disk Segment Mapping Real Memory CS 423UG - Operating Systems, Indranil Gupta
Virtual Memory Virtual Memory Address s d d (s,d) s < 1 Physical Memory 1 Base Limit 1 + 0 Segment Table d (s,d) p Physical Addr Segment Mapping Hardware STBR=segment table base register CS 423UG - Operating Systems, Indranil Gupta
Implementation Issues • Can cache segment table in • registers (if few segments) • or keep segment table in memory at per-process location given by STBR • Length of segment table given by segment table length register (STLR) • STBR, STLR changed at context switch time • Caching segment table entries (similar to TLB for paging) • associative registers, • look-aside memory • Segment hit ratio: percentage of time segment found in associative memory • If not found in associative memory, must load from segment tables: • requires additional memory reference • Suffers external fragmentation - use compaction? CS 423UG - Operating Systems, Indranil Gupta
Sharing Segments CS 423UG - Operating Systems, Indranil Gupta
Sharing and Protection • Code and data must not contain segment references if the segment VM mappings for them are different • Can add protection to segment table to protect memory • Check is done by hardware during access • Shared segments can have different protections from different processes by having different segment table protection access bits CS 423UG - Operating Systems, Indranil Gupta
Segment Protection Legend: reference – seg. has been accessed valid – seg. exists resident – seg. is in primary memory dirty – seg. has been changed since page-in from disk CS 423UG - Operating Systems, Indranil Gupta
Segmented Paged Virtual Memory • Implement segmentation by using paging underneath • Addresses fragmentation problem of segmented memory • On average, half a page per segment is wasted by internal fragmentation • Sharing and Protection can still be provided • Can have variable segment sizes • Segment consists of multiple pages • Each page has a fixed size (some allow variable sizes too) CS 423UG - Operating Systems, Indranil Gupta
Segmented Paged Virtual Memory CS 423UG - Operating Systems, Indranil Gupta
Sun: Example of VM Scheme Other examples in text: Pentium, Multics (read yourself) CS 423UG - Operating Systems, Indranil Gupta
Segmentation with Paging: Pentium Protection on the Pentium CS 423UG - Operating Systems, Indranil Gupta
Reminders • Virtual Memory • Segmentation • Segmentation and paging • Reading for this lecture: Sections 4.0-4.4, relevant parts of 4.5-4.7 (lecture notes), 4.8 • Reading for next lecture: Sections 5.1, 5.2 • Midterm on Oct 10 (next Monday) CS 423UG - Operating Systems, Indranil Gupta