Download presentation
Presentation is loading. Please wait.
1
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H بسم الله الرحمن الرحيم Chapter 9: Virtual Memory CPCS361 – Operating Systems I
2
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 2 Chapter 9: Virtual Memory Background Demand Paging Copy-on-Write Page Replacement Allocation of Frames Thrashing Memory-Mapped Files Allocating Kernel Memory Other Considerations Operating-System Examples
3
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 3 Objectives To describe the benefits of a virtual memory system To explain the concepts of demand paging, page- replacement algorithms, and allocation of page frames To discuss the principle of the working-set model
4
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 4 Background The requirement that instructions must be in physical memory to be executed seems both necessary and reasonable but it is also unfortunate, since it limits the size of a program to the size of physical memory Examination of real programs shows that, in many cases, the entire program in not needed Programs often have code to handle unusual error conditions Arrays, lists, and tables are often allocated more memory than they actually need Certain options and features of a program may be used rarely
5
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 5 Background Even in those cases where the entire program is needed, it may not all needed at the same time The ability to execute a program that is only partially in memory would confer many benefits: A program would no longer be constrained by the amount of physical memory that is available Because each user program could take less physical memory, more programs could be run at the same time, with a corresponding increase in CPU utilization and throughput but with no increase in response time or turnaround time Less I/O would be needed to load or swap each user program into memory, so each user program would run faster Running a program that is not entirely in memory would benefit both the system and the user
6
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 6 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 Virtual memory can be implemented via: Demand paging Demand segmentation
7
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 7 Virtual Memory that is Larger than Physical Memory
8
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 8 Virtual-address Space
9
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 9 Shared Library Using Virtual Memory
10
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 10 Demand Paging Consider how an executable program might be loaded from disk into memory; two strategies: Load the entire program in physical memory at program execution time Problem: we may initially need the entire program in memory Initially load pages only as they are needed; called demand paging With demand-paged virtual memory, pages are only loaded when they are demanded during program execution; pages that are never accessed are thus never loaded into physical memory
11
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 11 Demand Paging A demand paging system is similar to a paging system with swapping where processes reside in secondary memory (usually a disk) Bring a page into memory only when it is needed Less I/O needed Less memory needed Faster response Decrease swap time (resulted from swapping out unnecessary pages) More users
12
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 12 Demand Paging Page is needed reference to it invalid reference abort not-in-memory bring to memory Lazy swapper – never swaps a page into memory unless page will be needed Swapper that deals with individual pages is a pager Use pager rather than swapper in connection with demand paging
13
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 13 Transfer of a Paged Memory to Contiguous Disk Space
14
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 14 Valid-Invalid Bit Hardware support to distinguish between the pages that are in memory and the pages that are on the disk is needed With each page table entry a valid–invalid bit is associated (v in-memory, i not-in-memory) Initially valid–invalid bit is set to i on all entries Example of a page table snapshot: Frame #valid-invalid bit v v v …. i i
15
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 15 Valid-Invalid Bit During address translation, if valid–invalid bit in page table entry is i page fault If we guess right and page in all and only those pages that are actually needed, the process will run exactly as though we had brought in all pages. While the process executes and accesses pages that are memory resident, execution proceeds normally
16
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 16 Page Table When Some Pages are Not in Main Memory
17
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 17 Page Fault If there is a reference to a page: first reference to that page will trap to operating system (access to a page marked invalid causes): page fault trap
18
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 18 Page Fault Procedure for handling the page fault: 1. Check the page table determine whether the reference was valid or invalid memory 2. If the reference: Invalid reference abort (terminate the process) Valid reference but just not in memory page it in 3. Find a free frame (e.g. from the free-frame list) 4. Schedule a disk operation to read (swap) the desired page into the allocated frame 5. Modify (reset) the internal table kept with the process and the page table (Set validation bit = v) 6. Restart the instruction that caused the page fault trap. The process can now access the page as though it had always been in memory
19
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 19 Steps in Handling a Page Fault
20
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 20 Page Fault (Cont.) Pure demand paging: start executing a process with no pages in memory (extreme case) Theoretically, some programs could access several new pages of memory with each instruction execution (one page for the instruction and many for data), possibly causing multiple page faults per instruction. This situation would result in unacceptable system performance Fortunately, analysis of running processes shows that this behavior is exceedingly unlikely. Programs tend to have locality of reference which results in reasonable performance from demand paging
21
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 21 Page Fault (Cont.) Hardware support is needed for demand paging: Page table: mark an entry invalid through a valid-invalid bit Secondary memory: holds pages that are not present in main memory A crucial requirement for demand paging is the need to be able to restart any instruction after a page fault A page fault may occur at any memory reference If the page fault occurs on the instruction fetch, then restart fetching the instruction again If a page fault occurs while fetching an operand, then fetch and decode the instruction again and then fetch the operand
22
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 22 Performance of Demand Paging Probability of Page Fault Rate: 0 p 1.0 if p = 0 no page faults if p = 1, every reference is a fault Effective Access Time (EAT) EAT = (1 – p) memory access + p page fault time page fault time = page fault overhead + swap page out + swap page in + restart overhead
23
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 23 Demand Paging Example Memory access time = 200 ns; Average page-fault service time = 8 ms EAT = (1 – p) 200 + p (8 ms) = (1 – p) 200 + p 8,000,000 = 200 + p 7,999,800 Page-fault service time (8 ms): 1.Service page-fault interrupt 2.Write out / Read in the page 3.Restart the process
24
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 24 Demand Paging Example If one access out of 1,000 causes a page fault, then, p = 0.001 EAT = 8199.8 ns = 8.2 s The computer will be slowed down by a factor of: 8 s /200 ns = 40 because of demand paging!! If we want performance degradation to be < 10%, we need 200 + 7,999,800 p < (200 10 / 100) 7,999,800 p < 20 p < 0.0000025 That is to keep the slowdown due to paging at a reasonable level, we can allow fewer than one memory access out of: 1/ 0.0000025 = 399,990 to page-fault.
25
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 25 What happens if there is no free frame? While a user process is executing, a page fault occurs OS determines where the desired page is residing on the disk but then finds that there is no free frames on the frame list; all memory is in use Page replacement – find some page in memory, but not really in use, swap it out Algorithm Performance – want an algorithm which will result in minimum number of page faults Same page may be brought into memory several times
26
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 26 Page Replacement Page replacement completes separation between logical memory and physical memory – large virtual memory can be provided on a smaller physical memory Degree of multiprogramming could be increased by running twice as many processes If a process of 10 pages actually uses only ½ of them, then demand paging saves the I/O necessary to load 5 pages that are never used
27
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 27 Page Replacement Degree of multiprogramming could be increased by running twice as many processes If 40 frames are available and processes are 10 pages in size 5 of which are never used: 8 processes rather than 4 could run Running 6 processes higher CPU utilization and throughput, 10 spare frames It is possible, however, that each of these processes, for a particular data set, may suddenly try to use all of its 10 pages, resulting in a need for 60 frames when only 40 are available!!
28
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 28 Need For Page Replacement
29
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 29 Page Replacement Page replacement takes the following approach: if no frame is free, find one that is not currently being used and free it Free a frame by writing its contents to swap space and changing the page table to indicate that the page is no longer in memory If no frames are free, 2 page transfers are required (1 out + 1 in) which doubles the page-fault service time and increases the EAT Can EAT be minimized?
30
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 30 Page Replacement Solution: use modify (dirty) bit to reduce overhead of page transfers (reduces I/O time by one half) – only modified pages are written to disk Each frame has a modify bit associated with it When a page is selected for replacement, its modify bit is examined If bit is set, page has been modified since it was read from the disk write that page to disk If bit is not set, page has not been modified since it was read into memory no need to write the memory page to disk
31
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 31 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. Bring the desired page into the (newly) free frame; update the page and frame tables 4. Restart the process
32
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 32 Page Replacement
33
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 33 Page Replacement Algorithms Two major problems Frame allocation algorithm Page replacement algorithm Disk I/O is so expensive 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
34
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 34 Page Replacement Algorithms For the reference string: 1, 4, 1, 6, 1, 6, 1, 6, 1, 6, 1 with 3 or more frames only 3 faults 1 frame we have 11 faults How many page faults with 2 frames? As the number of frames available increases, the number of page faults decreases
35
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 35 Graph of Page Faults Versus The Number of Frames
36
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 36 FIFO Page Replacement Associate with each page the time when that page was brought into memory When a page must be replaced, the oldest page is chosen Create a FIFO queue to hold all pages in memory Replace the page at the head of the queue When a page is brought into memory it is inserted at the tail of the queue A bad replacement choice increases the page-fault rate and slows process execution. It does not, however, cause incorrect execution
37
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 37 FIFO Page Replacement 15 page faults
38
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 38 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 Belady’s Anomaly: more frames more 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
39
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 39 FIFO Illustrating Belady’s Anomaly
40
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 40 Optimal (OPT) 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 we know this? Used for measuring how well your algorithm performs 1 2 3 4 6 page faults 4 5
41
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 41 Optimal (OPT) Page Replacement 9 page faults
42
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 42 Least Recently Used (LRU) Algorithm FIFO uses the time when the page was brought into memory OPT uses the time when a page is to be used (not feasible) LRU uses the recent past as an approximation of the near future The page that has not been used for the longest period of time is replaced It associates with each page the time of the page’s last use LRU is as OPT but looking backward in time rather than forward
43
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 43 Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 5 2 4 3 1 2 3 4 1 2 5 4 1 2 5 3 1 2 4 3 Least Recently Used (LRU) Algorithm 8 page faults
44
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 44 LRU Page Replacement 12 page faults
45
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 45 LRU Algorithm (Cont.) 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 changed, look at the counters to determine which are to change Search is needed for replacement Stack implementation – keep a stack of page numbers in a double link form: Page referenced: move it to the top No search for replacement
46
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 46 Use Of A Stack to Record the Most Recent Page References
47
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 47 Allocation of Frames How to allocate the fixed amount of free memory among the various processes? Each process needs minimum number of pages When a page fault occurs before executing instruction is complete, the instruction must be restarted We must have enough frames to hold all different pages that any single instruction can reference Two major allocation schemes Fixed allocation Priority allocation
48
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 48 Fixed Allocation Equal allocation – For example, if there are 100 frames and 5 processes, give each process 20 frames Proportional allocation – Allocate according to the size of process
49
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 49 Priority Allocation Use a proportional allocation scheme using priorities rather than size If process P i generates a page fault: Select for replacement one of its frames Select for replacement a frame from a process with lower priority number
50
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 50 Global vs. Local Allocation Global replacement – process selects a replacement frame from the set of all frames; one process can take a frame from another Set of pages in memory for a process depends not only on the paging behavior of that process but also on the paging behavior of other processes Same process may perform quite differently (e.g. it takes 0.5 sec. for one execution and 10.3 sec. for the next execution)
51
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 51 Global vs. Local Allocation Local replacement – each process selects from only its own set of allocated frames Set of pages in memory for a process is affected by the paging behavior of only that process Hider a process by not making available to it other, less used pages of memory Global repalcement generally results in greater system throughput and therefore the more common method
52
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 52 Thrashing If a process does not have “enough” pages, the page-fault rate is very high. This leads to: Low CPU utilization Operating system thinks that it needs to increase the degree of multiprogramming Another process added to the system When a process needs more frames, it starts faulting and taking frames from other processes. Theses processes needs those pages and so they also fault, taking frames from other processes
53
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 53 Thrashing (Cont.) A process is busy swapping pages in and out A process is spending more time paging than executing It results in severe performance problems The ready queue is empty while the processes queue up for paging CPU utilization decreases and as a result CPU scheduler increases the degree of multiprogramming
54
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 54 Thrashing (Cont.) The new process causes more page faults, CPU utilization drops even further, and the CPU scheduler tries to increase the degree of multiprogramming even more. Thrashing has occurred, and the system throughput plunges Page fault increases tremendously EAT increases No work is getting done, because processes are spending all their time paging
55
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 55 Thrashing (Cont.)
56
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 56 Demand Paging and Thrashing Thrashing can be limited by using a local replacement algorithm If one process starts thrashing, it cannot steal frames from another process and cause the latter to thrash as well Why does demand paging work? Locality model A locality is a set of pages that are actively used together A program is generally composed of several different localities
57
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 57 Demand Paging and Thrashing Why does demand paging work? Locality model e.g. when a function is called, it defines a new locality in which memory references are made to instructions of the function call, its local variables, and a subset of global variables. When we exit the function, the process leaves this locality States: as a process executes, it migrates from one locality to another A process will fault for pages in its locality until all these pages are in memory; then it will not fault again until it changes localities Localities may overlap Why does thrashing occur? size of locality > total memory size
58
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 58 Locality In A Memory-Reference Pattern
59
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 59 Working-Set Model Based on the assumption of locality An approximation of the program’s locality Working set: the set of pages in the most recent page references working-set window a fixed number of page references
60
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 60 Working-Set Model WSS i (working set size of Process P i ) = total number of pages referenced in the most recent (varies in time) if too small will not encompass entire locality if too large will encompass several localities if = will encompass entire program (all process pages)
61
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 61 Working-Set Model D = WSS i D = WSS i total demand frames if D > m (available frames) Thrashing Policy if D > m, then suspend one of the processes Working-set strategy prevent thrashing while keeping the degree of multiprogramming as high as possible optimizes CPU utilization
62
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 62 Working-Set Model = 10 WSS(t 1 ) = 5 WSS(t 2 ) = 2
63
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 63 Keeping Track of the Working Set Difficulty of keeping track of the working set Working set window is a moving window; at each memory reference, a new memory reference appear at one end and the oldest reference drops off the other end Approximate with interval timer + a reference bit Example: = 10,000 Timer interrupts after every 5000 time units Keep in memory 2 bits for each page Whenever a timer interrupts copy and sets the values of all reference bits to 0 If one of the bits in memory = 1 page in working set
64
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 64 Keeping Track of the Working Set Why is this not completely accurate? Can not tell where, within an interval of 5000, a reference occurred (uncertainty) Improvement = 10 bits and interrupt every 1000 time units Cost will be correspondingly higher
65
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 65 Working Sets and Page Fault Rates start of new locality
66
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 66 Page-Fault Frequency Scheme How to prevent thrashing? Establish “acceptable” page-fault rate If actual rate too low, process loses (remove) frame If actual rate too high, process gains (allocated) frame
67
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 67 Other Issues: Prepaging Prepaging An attempt to reduce/prevent the large number of page faults that occurs at process startup Prepage all or some of the pages a process will need, before they are referenced How? Keep with each process a list of pages in its working set. When the process is to be resumed, automatically bring back into memory its entire working set before restarting the process But if prepaged pages are unused, I/O and memory was wasted
68
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 68 Other Issues: Prepaging Prepaging Assume s pages are prepaged and α of the pages is used (0 α 1) Is cost of s * α saved page faults > or < than the cost of prepaging s * (1- α ) unnecessary pages? α near zero prepaging loses α near one prepaging wins
69
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 69 Other Issues: Page Size Page size selection must take into consideration: Internal fragmentation: on the average, ½ of final page of each process will be wasted Table size: decreasing page size increases the table size I/O overhead: Transfer rate of 512 bytes = 0.2 ms, latency = 8 ms, seek time = 20 ms, total time = 28.2 ms, Transfer rate for 1024 bytes = 0.4 ms, total time = 28.4 ms < 56.4 ms (two pages of size 512 bytes 1024 bytes) Locality: A smaller page size allows each page to match program locality more accurately Improved with small page size
70
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 70 Other Issues: Program Structure Program structure int[128,128] data; Each row is stored in one page Program 1 for (j = 0; j <128; j++) for (i = 0; i < 128; i++) data[i,j] = 0; 128 x 128 = 16,384 page faults Program 2 for (i = 0; i < 128; i++) for (j = 0; j < 128; j++) data[i,j] = 0; 128 page faults i j
71
Adopted from and based on Textbook: Operating System Concepts – 8th Edition, by Silberschatz, Galvin and Gagne Updated and Modified by Dr. Abdullah Basuhail, CSD, FCIT, KAU, 1431H 71 End of Chapter 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.