Wednesday, October 27, 1993 Today: Virtual Memory page replacement algorithms (chapter 8) ------------------------------------------------------------------------------- Page replacement: decide what page to replace when there are none free - may augment page table with additional status bits for benefit of page replacement algorithm - dirty (modify) bit - use bit etc... - more global question is frame allocation algorithm (how many frames to allocate to a given process - page replacement algorithms: metric (good) is *lowest page-fault rate* - *reference string*: string of memory references (actually a string of page numbers representing references) - can generate artificially or can capture from running system (compress sequences like "rpppr" into "rpr" to make size more managable) ------------------------------------------------------------------------------- FIFO page replacement algorithm (first-in, first-out) - associate time with each page - replace oldest page - implementation: queue (oldest is at head of queue, newest at tail) Example: 3 frames/reference string 1 2 3 4 1 2 5 1 2 3 4 5/track question of "page fault?" --- Belady's anomaly --- try the same reference string for 4 frames --- Note that FIFO depends only on when the page is brought into memory ignoring how the page has been and will be used. ------------------------------------------------------------------------------- OPT aka MIN --- Optimal Page Replacement Algorithm "Replace the page that will not be used for the longest period of time" Obvious disadvantage---have to predict future Compare OPT to FIFO for reference string 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1 Main use of OPT is to provide a reference point for comparison of other page replacement algorithms (e.g., how close is the proposed algorithm to optimal in the worst case and on average). - does not suffer from Belady's anomaly ------------------------------------------------------------------------------- Since OPT requires knowledge of the future, we try approximations that use the past to predict the future. Possible assumptions include: - a page used recently is likely to be used in the near future - a page unused recently is unlikely to be used in the near future ------------------------------------------------------------------------------- LRU --- Least Recently Used Replace the page that has not been used for the longest period of time Implementation alternatives: - keep time-of-use register for each page, store system clock into that register on each memory reference. To discover least recently used frame, scan time registers and find the one with the oldest clock (expensive). - keep stack of page numbers. Reference to page causes entry to be moved to top of stack. Top is most recently used. Bottom is least recently used. Note that stack entries are removed from middle so doubly linked list implementation is best---six pointer changes at worst (internal neighbors 2 pointers, top of stack pointer, previous top's pointer, entry's 2 pointers) - does not suffer from Belady's anomaly ------------------------------------------------------------------------------- Implementing LRU can be expensive, so in practice many implementations only *approximate* LRU --- basic idea: use a reference bit - every entry in page table has reference bit - bit set (to 1) when page accessed - periodically OS resets all reference bits (to 0) - pages with bit reset (i.e., 0) have higher priority to be replaced Reference bits tell us what pages have been accessed but do not tell us the order of use. ------------------------------------------------------------------------------- Additional reference bits algorithm - Keep record of reference bits at regular intervals - associate 8 bit byte with each page table entry - at regular intervals (e.g., 100 milliseconds) OS shifts reference bit for each page into high-order bit of 8-bit byte, shifting other bits right one. - low order bit discarded - 00000000 not used in 8 time units - 11111111 used in each of past 8 time units - 00111111 not used in last 2 time units but used in each of 6 previous ones If view numbers as unsigned integers, the page(s) with the lowest number is the one least recently used. Note---not guaranteed to be a *single* page ------------------------------------------------------------------------------- Second-chance algorithm (also called the clock algorithm) - FIFO replacement but inspect reference bit before actually replacing - if reference bit==0, then replace it - if reference bit==1, then clear reference bit and move on to inspect the next FIFO page Implementation: circular queue. Advance pointer, clearing reference bits, until find one with reference bit==0 - worst case: cycle through all entries - degenerates to FIFO in this case - slowly sweeping clock hand is good---plenty of memory, not many page faults ------------------------------------------------------------------------------- Ad-hoc algorithm (as in VAX/VMS) - keep pool of free frames - when new frame needed, select frame to be freed - get frame from pool to receive new page, transfer page in - initiate transfer of freed page - when transfer finishes, move it to the free pool (overlaps page out and subsequent computation) - modification in VAX/VMS---keep track of what's in the free frames and if page is needed before overwritten, recover from free pool - consequently if you mistakenly selected active page for replacement this brings it back again before it gets wiped out. ------------------------------------------------------------------------------- Additional nuance: - keep "dirty" bit to give preference to dirty pages. It is more expensive to throw out dirty pages---clean pages do not have to be written to disk. ------------------------------------------------------------------------------- Next: allocation of frames, thrashing