Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered.

Similar presentations


Presentation on theme: "1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered."— Presentation transcript:

1 1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered in class) Chapter 10 to page 328, 330-343, 344-353

2 2 Background Virtual memory – separation of user logical memory from physical memory. –Only part of the program needs to be in memory for execution. –Logical address space can therefore be much larger than physical address space. –Allows address spaces to be shared by several processes. –Allows for more efficient process creation. Virtual memory can be implemented via: –Demand paging –Demand segmentation

3 3 Virtual Memory That is Larger Than Physical Memory

4 4 Demand Paging Bring a page into memory only when it is needed. –Less I/O needed –Less memory needed –Faster response –More users Page is needed implies that “a reference to this page is generated by the CPU” –invalid reference  abort –not-in-memory  a trap to the OS called a “page fault” occurs which will result in bringing the page to memory

5 5 Transfer of a Paged Memory to Contiguous Disk Space

6 6 Valid-Invalid Bit With each page table entry a valid–invalid bit is associated{1  in-memory, 0  not-in-memory OR page is not valid(i.e. not in the address space of the process)} Initially valid–invalid but is set to 0 on all entries. Example of a page table snapshot During address translation, if valid–invalid bit in page table entry is 0 and the address is legal then we have a “page fault”. 1 1 1 1 0 0 0  Frame # valid-invalid bit page table

7 7 Page Table When Some Pages Are Not in Main Memory

8 8 Page Fault If there is ever a reference to a page, first reference will trap to OS  page fault ; stop executing the instruction that generated the page fault OS looks at another table to decide: –Invalid reference  abort. –Just not in memory. Get empty frame. Swap page into frame. Reset table’s entry validation bit = 1. Restart instruction

9 9 Steps in Handling a Page Fault

10 10 What happens if there is no free frame? Page replacement – find some page residing in memory, but not really in use, swap it out. –Choose a page replacement algorithm –performance – want an algorithm which will result in minimum number of page faults. Same page may be brought into memory several times.

11 11 Performance of Demand Paging Page Fault Rate, p :0  p  1.0 –percentage of memory references that generate page faults, –if p = 0 no page faults –if p = 1, every reference is a fault Effective Access Time, EAT = (1 – p) x memory access + p (page fault overhead + [swap page out ] + swap page in + restart overhead)

12 12 Demand Paging Example Memory access time = 1 microsecond 50% of the time the page that is being replaced has been modified and therefore needs to be swapped out. Swap Page Time= 10 msec =10,000 microsec EAT = (1 – p) x 1 + p (.5 x 10000 +.5 x 20000) ≈ 1 + 15000P (in microsecond) EAT is directly proportional to p

13 13 Process Creation Virtual memory allows other benefits during process creation: - Copy-on-Write - Memory-Mapped Files

14 14 Copy-on-Write Copy-on-Write (COW) allows both parent and child processes to initially share the same pages in memory. If either process modifies a shared page, only then is the page copied. COW allows more efficient process creation as only modified pages are copied. Free pages are allocated from a pool of zeroed- out pages.

15 15 Memory-Mapped Files Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory. A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than read() write() system calls. Also allows several processes to map the same file allowing the pages in memory to be shared.

16 16 Memory Mapped Files

17 17 Page Replacement Page-fault service routine handles page replacement In the page table, use modify (dirty) bit to reduce overhead of page transfers – only modified pages are written to disk. By allowing page replacement, large virtual memory can be implemented even with a smaller physical memory.

18 18 Illustrating the Need For Page Replacement

19 19 Handling a Page Fault 1.Find the location of the desired page on disk. 2.Find a free frame 3.Read the desired page into the free frame. Update the process page table and system frame table. 4.Restart the process.

20 20 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. 3.Read the desired page into the (newly) free frame. Update the page and frame tables. 4.Restart the process.

21 21 Page Replacement

22 22 Page Replacement Algorithms 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.

23 23 Graph of Page Faults Versus The Number of Frames

24 24 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) 4 frames FIFO Replacement – Belady’s Anomaly –more frames  less page faults 1 2 3 1 2 3 4 1 2 5 3 4 9 page faults 1 2 3 1 2 3 5 1 2 4 5 10 page faults 4 43

25 25 FIFO Page Replacement

26 26 FIFO Illustrating Belady’s Anamoly

27 27 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 How do you know this? Used for measuring how well your algorithm performs. 1 2 3 4 6 page faults 4 5

28 28 Optimal Page Replacement

29 29 Least Recently Used (LRU) Algorithm Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 Counter implementation –Every page entry has a counter; every time page is referenced through this entry, copy the clock into the counter. –When a page needs to be replaced, look at the counters to determine which page has not been referenced for the longest time 1 2 3 5 4 4 3 5

30 30 LRU Page Replacement

31 31 LRU Algorithm (Cont.) Stack implementation – keep a stack of page numbers in a doubly linked list with head and tail pointers: –move referenced page to the top of the stack  requires 6 pointers to be changed –Tail pointer points to the bottom of the stack, which is the LRU page –Top of the stack is always the most recently used page –No search for replacement

32 32 Use Of A Stack to Record The Most Recent Page References

33 33 Stack Page Replacement Algorithms Optimal, LRU and all other algorithms in the class called “Stack Algorithms” do not suffer from Belady’s anomaly. A stack algorithm is an algorithm for which it can be shown that the set of pages in memory for n frames is always a subset of the set of pages that would be in memory with n + 1 frames.

34 34 LRU Approximation Algorithms Reference bit –With each page associate a bit, initially = 0 –When page is referenced the bit set to 1. –Replace a page with a 0 reference bit (if one exists). We do not know the order, however. Second chance replacement –Use FIFO with a reference bit. –If page to be replaced has a 1 reference bit, give it another chance.  Clear its reference bit & set arrival time to current time.  Replace next FIFO page.  Page given another chance will not be replaced until all other pages are replaced.  Page referenced often (i.e. reference bit kept set), will never be replaced –This is also called “Clock replacement”.


Download ppt "1 Chapter 10: Virtual Memory Background Demand Paging Process Creation Page Replacement Allocation of Frames Thrashing Operating System Examples (not covered."

Similar presentations


Ads by Google