ICS 143 PRINCIPLES OF OPERATING SYSTEMS Discussion Section Week 10 Today 2 Announcements Lecture Review Pop Quiz #3 6/1/2015 Announcements 3 Please fill out your EEE instructor evaluation. Schedule switch-up in Week 10: Regular class on Wed, 3 June 2015 Two classes on Fri, 5 June 2015 at 11:00am and 3:00pm in the usual rooms Homework #4: Programming Assignment Due Thu, 4 June 2015, 11:55pm via EEE Dropbox Due Wed, 10 June 2015, 11:55pm via EEE Dropbox Final Tue, 9 June 2015, 1:30-3:30pm in EH 1200 6/1/2015 Memory Segmentation 4 Separate user’s view of memory and the actual physical memory Division of a computer's primary memory into segments (or sections) subroutine segment 3 A reference to a memory location includes a value that identifies a segment and an offset within that segment A logical address space is a collection of segments A segment is a logical unit segment 0 e.g. main program, procedure, function, local variables, global variables, common block <segment-number, offset> stack Sqrt segment 1 segment 2 main program logical address space 6/1/2015 Segment Table 5 Maps two-dimensional user-defined addresses into one-dimensional physical addresses. Each table entry has: Segment-table base register (STBR): Base - contains the starting physical address where the segments reside in memory. Limit - specifies the length of the segment. points to the segment table’s location in memory. Segment-table length register (STLR): indicates the number of segments used by a program; segment number is legal if s < STLR. 6/1/2015 Segment Table (2) 6 limit offset segment 0 segment 2 segment number base 0 1000 1400 1 400 6300 2 500 3600 <segment,offset> A: <2,300> B: <0,1200> points to? segment 1 A: 3600+300=3900 B: invalid 6/1/2015 Segmentation Architecture Relocation is dynamic - by segment table Sharing Code sharing occurs at the segment level. Shared segments must have same segment number. Allocation - dynamic storage allocation problem use best fit/first fit, may cause external fragmentation Protection protection bits associated with segments read/write/execute privileges array in a separate segment - hardware can check for illegal array indexes. Virtual Memory 8 Goal of memory management: Virtual Memory: keep many processes in memory simultaneously to allow multiprogramming Problem: require entire process to be in memory before process can execute Only part of the program needs to be in memory for execution Programs can be larger than physical memory Separates user logical memory from physical memory Allows easy sharing of files and address spaces Virtual Memory can be implemented via demand paging or demand segmentation 6/1/2015 Demand Paging Similar to a paging system with swapping Bring a page into memory only when it is needed. Less I/O needed Less Memory needed Faster response More users The first reference to a page will trap to OS with a page fault. OS looks at another table to decide Invalid reference - abort Just not in memory paged main memory large secondary storage Valid-Invalid Bit With each page table entry a valid-invalid bit is associated (1 inmemory, 0 not in memory). Initially, valid-invalid bit is set to 0 on all entries. During address translation, if validinvalid bit in page table entry is 0 --- page fault occurs. Page Replacement 11 What happens if there is no free frame? Page replacement - find some page in memory that is not really in use and swap it. Handling a page fault: Find the location of the desired page on disk. 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 the victim page to disk; change the page and frame tables accordingly Read the desired page into the (newly) free frame; change the page and frame tables Restart the user process 6/1/2015 Dirty Bit 12 Note: If no frames are free, two page transfers (in and out) are required doubles the page-fault service time! Reduce overhead with dirty bit: set by hardware whenever any word or byte in the page is written into if bit is set, page has been modified and we need to write it to disk if bit is not set, no writing to disk required 6/1/2015 Page Replacement Strategies The Principle of Optimality Replace the page that will not be used again the farthest time into the future. Random Page Replacement Choose a page randomly Replace the page that has been in memory the longest. Replace the page that has not been used for the longest time. Replace the page that is used least often. An approximation to LRU Keep in memory those pages that the process is actively using FIFO - First In First Out LRU - Least Recently Used LFU - Least Frequently Used NUR - Not Used Recently Working Set First In First Out (FIFO) Replacement 14 Associate with each page the time when that page was brought into memory When a page must be replaced, choose the oldest page Bélády's anomaly: increasing the number of page frames results in more page faults 6/1/2015 Optimal Page Replacement 15 Replace the page that will not be used for the longest period of time How do you know this??? You don’t. Generally used to measure how well an algorithm performs; guarantees lowest possible page-fault rate 6/1/2015 LRU Page Replacement 16 Replace the page that has not been used for the longest period of time (least recently used; LRU) Approximation to optimal algorithm: assume recent past indicates the future Problem: Needs sufficient hardware support, often hard to implement! ? 6/1/2015 Second-Chance Replacement 17 FIFO replacement algorithm With each page, associate a reference bit: hardware sets the bit whenever page is referenced (either read or write to any byte in the page) When a page is selected, inspect reference bit If reference bit = 0, replace the page If reference bit = 1, set bit =0, continue 6/1/2015 Enhanced Second-Chance Replacement 18 Need a reference bit and a modify bit as an ordered pair. 4 situations are possible: (0,0) - neither recently used nor modified - best page to replace. (0,1) - not recently used, but modified - not quite as good, because the page will need to be written out before replacement. (1,0) - recently used but clean - probably will be used again soon. (1,1) - probably will be used again, will need to write out before replacement. Used in the Macintosh virtual memory management scheme 6/1/2015 Allocation of Frames 19 How do we allocate 𝑚 free frames among 𝑛 processes? Equal allocation: each process gets 𝑚/𝑛 frames Proportional number Priority allocation: of frames is proportional to process size allocation: number of frames is proportional to process priority 6/1/2015 Global vs. Local Allocation 20 Global allocation Process selects a replacement frame from the set of all frames Process can take a frame from another Process may not be able to control its page fault rate Local allocation Each process selects from only its own set of allocated frames Process slowed down even if other less used pages of memory are available Global replacement has better throughput 6/1/2015 Thrashing 21 If a process does not have enough pages, the page-fault rate is very high. This leads to: low CPU utilization. OS thinks that it needs to increase the degree of multiprogramming Another process is added to the system. System throughput plunges... Thrashing: High paging activity: Process is spending more time paging than executing 6/1/2015 Working-Set Model 22 Locality: Δ = working-set window set of pages that are actively used together process migrates from one locality to another examine the most recent Δ page references working set: set of pages in the most recent Δ page references If a page is in active use, it will be in the working set If a page is no longer being used, it will drop from the working set Δ time units after its last reference 𝐷 = working set size of a process 𝑚 = number of available frames If 𝐷 > 𝑚: thrashing => suspend one of the processes 6/1/2015 Page-Fault Frequency 23 Thrashing has a high page-fault rate Idea: Control thrashing by establishing an acceptable page-fault rate rate is too low, process loses frame If rate is too high, process needs and gains a frame increase number of frames If decrease number of frames 6/1/2015 24 Pop Quiz #3 6/1/2015