Presentation is loading. Please wait.

Presentation is loading. Please wait.

Logistics Homework 5 will be out this evening, due 3:09pm 4/14

Similar presentations


Presentation on theme: "Logistics Homework 5 will be out this evening, due 3:09pm 4/14"— Presentation transcript:

0 Instructor: Junfeng Yang
W4118 Operating Systems Instructor: Junfeng Yang

1 Logistics Homework 5 will be out this evening, due 3:09pm 4/14
You will implement a new page replacement algorithm: Most Recently Used (MRU) Will be clear at end of this lecture 1

2 Last lecture Segmentation: divide address space into logical segments; each has separate base and limit Advantages Disadvantages Combine paging and segmentation X86 example Intro to Virtual Memory Motivation: want to run many processes or a process with large address space using limited physical memory Observation: locality of reference Process only needs a small number of pages at any moment Virtual memory: OS + hardware work together to provide processes the illusion of having fast memory as large as disk Segmentation Advantages Sharing of segments Easier to relocate segment than entire program Avoids allocating unused memory Flexible protection Efficient translation Segment table small  fit in MMU Segmentation Disadvantages Segments have variable lengths  dynamic allocation overhead (Best fit? First fit?) External fragmentation: wasted memory Segments can be large

3 Today Virtual memory implementation
What are the operations virtual memory must provide? When to bring a page from disk into memory? What page in memory should be replace? Page replacement algorithms How to approximate LRU efficiently? 3

4 Virtual Address Space Virtual address maps to one of three locations
Physical memory: small, fast, expensive Disk: large, slow, cheap Nothing

5 Virtual Memory Operation
What happens when reference a page in backing store? Recognize location of page Choose a free page Bring page from disk into memory Above steps need hardware and software cooperation How to detect if a page is in memory? Extend page table entries with present bits Page fault: if bit is cleared then referencing resulting in a trap into OS

6 Handling a Page Fault OS selects a free page
OS brings faulting page from disk into memory Page table is updated, present bit is set Process continues execution

7 Steps in Handling a Page Fault
Consider TLB as well

8 Continuing Process Continuing process is tricky Options
Page fault may have occurred in middle of instruction Want page fault to be transparent to user processes Options Skip faulting instruction? Restart instruction from beginning? What about instruction like: mov ++(sp), p Requires hardware support to restart instructions Move ++(sp), p Take the content of SP Increment SP Put the content of SP to p  suppose there is a page fault for doing the store Should we re-execute the instruction? Not correct, because SP has changed (side effects)

9 OS Decisions Page selection Page replacement
When to bring pages from disk to memory? Page replacement When no free pages available, must select victim page in memory and throw it out to disk

10 Page Selection Algorithms
Demand paging: load page on page fault Start up process with no pages loaded Wait until a page absolutely must be in memory Request paging: user specifies which pages are needed Requires users to manage memory by hand Users do not always know best OS trusts users (e.g., one user can use up all memory) Prepaging: load page before it is referenced When one page is referenced, bring in next one Do not work well for all workloads Difficult to predict future

11 Page Replacement Algorithms
Optimal: throw out page that won’t be used for longest time in future Best algorithm if we can predict future Good for comparison, but not practical Random: throw out a random page Easy to implement Works surprisingly well. Why? Avoid worst case FIFO: throw out page that loaded in first Fair: all pages receive equal residency LRU: throw out page that hasn’t been used in longest time Past predicts future With locality: approximates Optimal

12 Page Replacement Algorithms
Goal: want lowest page-fault rate Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults on that string In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

13 Optimal Algorithm Replace page that will not be used for longest period of time 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Problem: how do you know the future? Not practical, but can be used for measuring how well your algorithm performs 1 4 6 page faults 2 3 4 5

14 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process): 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 4 frames: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1 1 5 4 2 2 1 5 10 page faults 3 3 2 4 4 3 Advantages: fair, easy to implement Problem: ignores access patterns Belady’s anomaly: more frames  more page faults!

15 First-In-First-Out (FIFO) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frames (3 pages can be in memory at a time per process): 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 4 frames: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Belady’s Anomaly: more frames  more page faults 1 1 4 5 2 2 1 3 9 page faults 3 3 2 4 1 1 5 4 2 2 1 5 10 page faults 3 3 2 4 4 3

16 Graph of Page Faults Versus The Number of Frames

17 FIFO Illustrating Belady’s Anomaly
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

18 Least Recently Used (LRU) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 With locality, LRU approximates Optimal algorithm 1 1 1 1 5 2 2 2 2 2 3 5 5 4 4 4 4 3 3 3

19 Implementing LRU: hardware
A counter for each page Every time page is referenced, save system clock into the counter of the page Page replacement: scan through pages to find the one with the oldest clock Problem: have to search all pages/counters!

20 Implementing LRU: software
A doubly linked list of pages Every time page is referenced, move it to the front of the list Page replacement: remove the page from back of list Avoid scanning of all pages Problem: too expensive Requires 6 pointer updates for each page reference High contention on multiprocessor

21 Example software LRU implementation

22 LRU: Concept vs. Reality
LRU is considered to be a reasonably good algorithm Problem is in implementing it efficiently Hardware implementation: counter per page, copied per memory reference, have to search pages on page replacement to find oldest Software implementation: no search, but pointer swap on each memory reference, high contention In practice, settle for efficient approximate LRU Find an old page, but not necessarily the oldest LRU is approximation anyway, so approximate more

23 Clock (second-chance) Algorithm
Goal: remove a page that has not been referenced recently good LRU-approximate algorithm Idea A reference bit per page Memory reference: hardware sets bit to 1 Page replacement: OS finds a page with reference bit cleared OS traverses all pages, clearing bits over time Access memory twice, why okay?

24 Clock Algorithm Implementation
OS circulates through pages, clearing reference bits and finding a page with reference bit set to 0 Keep pages in a circular list = clock Pointer to next victim = clock hand

25 Second-Chance (clock) Page-Replacement Algorithm

26 Clock Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 1
1 1 1 1 3 1 3 3 2 1 4 1 4 4 4 5 1 4 1 4 1 1 1 1 5 1 2 1 2 2 3 1 3 3

27 Clock Algorithm Extension
Problem of clock algorithm: does not differentiate dirty v.s. clean pages Dirty page: pages that have been modified and need to be written back to disk More expensive to replace dirty pages than clean pages One extra disk write (5 ms)

28 Clock Algorithm Extension
Use dirty bit to give preference to dirty pages On page reference Read: hardware sets reference bit Write: hardware sets dirty bit Page replacement reference = 0, dirty = 0  victim page reference = 0, dirty = 1  skip (don’t change) reference = 1, dirty = 0  reference = 0, dirty = 0 reference = 1, dirty = 1  reference = 0, dirty = 1 advance hand, repeat If no victim page found, run swap daemon to flush unreferenced dirty pages to the disk, repeat

29 Problem with LRU-based Algorithms
LRU ignores frequency Intuition: a frequently accessed page is more likely to be accessed in the future than a page accessed just once Problematic workload: scanning large data set … (pages frequently used) … (pages used just once) Solution: track access frequency Least Frequently Used (LFU) algorithm Expensive Approximate LFU: LRU-2Q

30 Problem with LRU-based Algorithms (cont.)
LRU does handle repeated scan well when data set is bigger than memory 5-frame memory with Solution: Most Recently Used (MRU) algorithm Replace most recently used pages Best for repeated scans

31 Memory Management Summary
Different memory management techniques Contiguous allocation Paging Segmentation Paging + segmentation In practice, hierarchical paging is most widely used; segmentation loses is popularity Some RISC architectures do not even support segmentation Virtual memory OS and hardware exploit locality to provide illusion of fast memory as large as disk Similar technique used throughout entire memory hierarchy Page replacement algorithms LRU: Past predicts future No silver bullet: choose algorithm based on workload

32 Current Trends Less critical now
Personal computer v.s. time-sharing machines Memory is cheap  Larger physical memory Larger page sizes (even multiple page sizes) Better TLB coverage Smaller page tables, less page to manage Internal fragmentation Larger virtual address space 64-bit address space Sparse address spaces File I/O using the virtual memory system Memory mapped I/O: mmap() Use VM for file caching Access file data as if access memory Page fault handling for file reads/writes madvise(): OS can ignore your hints


Download ppt "Logistics Homework 5 will be out this evening, due 3:09pm 4/14"

Similar presentations


Ads by Google