Module4 Virtual Memory PPT
Module4 Virtual Memory PPT
Operating System
Operating Concepts
System – 9th–Edition
Concepts 9th Edition 9.1 Silberschatz, Galvin and Gagne ©2013
Virtual Memory
Background
Demand Paging
Copy-on-Write
Page Replacement
Allocation of Frames
Thrashing
Operating System Concepts – 9th Edition 9.2 Silberschatz, Galvin and Gagne ©2013
1. Background
Operating System Concepts – 9th Edition 9.3 Silberschatz, Galvin and Gagne ©2013
What is virtual memory?
Virtual memory is a technique that allows the execution of processes that are not
completely in memory.
One major advantage of this scheme is that programs can be larger than physical
memory.
Logical address space can therefore be much larger than physical address space
Operating System Concepts – 9th Edition 9.4 Silberschatz, Galvin and Gagne ©2013
Virtual Memory That is Larger Than Physical Memory
Operating System Concepts – 9th Edition 9.5 Silberschatz, Galvin and Gagne ©2013
Operating System Concepts – 9th Edition 9.6 Silberschatz, Galvin and Gagne ©2013
Virtual address space – logical view of how process is
stored in memory
Usually start at address 0, contiguous addresses until end of
space
Meanwhile, physical memory organized in page frames
MMU must map logical to physical
Virtual memory can be implemented via:
1. Demand paging
2. Demand segmentation
Operating System Concepts – 9th Edition 9.7 Silberschatz, Galvin and Gagne ©2013
Virtual-address Space
Usually design logical address space for stack to start at Max logical
address and grow “down” while heap grows “up”
Maximizes address space use
Unused address space between the two is hole
No physical memory needed until heap or stack grows to a
given new page
Enables sparse address spaces with holes left for growth, dynamically
linked libraries, etc
System libraries shared via mapping into virtual address space
Shared memory by mapping pages read-write into virtual address
space
Pages can be shared during fork(), speeding process creation
Operating System Concepts – 9th Edition 9.8 Silberschatz, Galvin and Gagne ©2013
Shared Library Using Virtual Memory
Operating System Concepts – 9th Edition 9.9 Silberschatz, Galvin and Gagne ©2013
2. Demand Paging
Bring a page into memory only when it is
needed
Lazy swapper is used which never swaps
a page into memory unless page will be
needed
Operating System Concepts – 9th Edition 9.10 Silberschatz, Galvin and Gagne ©2013
Basic Concepts
With swapping, pager guesses which pages will be used before swapping out
again
Instead, pager brings in only those pages into memory
How to determine that set of pages?
Need new MMU functionality to implement demand paging
If pages needed are already memory resident
No difference from non demand-paging
If page needed and not memory resident
Need to detect and load the page into memory from storage
Without changing program behavior
Without programmer needing to change code
Operating System Concepts – 9th Edition 9.11 Silberschatz, Galvin and Gagne ©2013
Valid-Invalid Bit
With each page table entry a valid–invalid bit is associated
(v in-memory – memory resident, i not-in-memory)
Initially valid–invalid bit is set to i on all entries
Example of a page table snapshot:
During MMU address translation, if valid–invalid bit in page table entry is i page fault
Operating System Concepts – 9th Edition 9.12 Silberschatz, Galvin and Gagne ©2013
Page Table When Some Pages Are Not in Main Memory
Operating System Concepts – 9th Edition 9.13 Silberschatz, Galvin and Gagne ©2013
Page Fault
Operating System Concepts – 9th Edition 9.14 Silberschatz, Galvin and Gagne ©2013
Steps in Handling a Page Fault
Operating System Concepts – 9th Edition 9.15 Silberschatz, Galvin and Gagne ©2013
Aspects of Demand Paging
Extreme case – start process with no pages in memory
OS sets instruction pointer to first instruction of process, non-memory-resident -> page fault
And for every other process pages on first access
Pure demand paging
Actually, a given instruction could access multiple pages -> multiple page faults
Consider fetch and decode of instruction which adds 2 numbers from memory and stores result back
to memory
Pain decreased because of locality of reference
Hardware support needed for demand paging
Page table with valid / invalid bit
Secondary memory (swap device with swap space)
Instruction restart
Operating System Concepts – 9th Edition 9.16 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging
Stages in Demand Paging (worse case)
1. Trap to the operating system
2. Save the user registers and process state
3. Determine that the interrupt was a page fault
4. Check that the page reference was legal and determine the location of the page on the disk
5. Issue a read from the disk to a free frame:
1. Wait in a queue for this device until the read request is serviced
2. Wait for the device seek and/or latency time
3. Begin the transfer of the page to a free frame
6. While waiting, allocate the CPU to some other user
7. Receive an interrupt from the disk I/O subsystem (I/O completed)
8. Save the registers and process state for the other user
9. Determine that the interrupt was from the disk
10. Correct the page table and other tables to show page is now in memory
11. Wait for the CPU to be allocated to this process again
12. Restore the user registers, process state, and new page table, and then resume the interrupted
instruction
Operating System Concepts – 9th Edition 9.17 Silberschatz, Galvin and Gagne ©2013
Performance of Demand Paging
Three major activities
1. Service the interrupt
2. Read the page
3. Restart the process
The first and third tasks can be reduced, with careful coding( normally 1
to 100 microseconds)
Second task will probably take 8 milliseconds
Operating System Concepts – 9th Edition 9.18 Silberschatz, Galvin and Gagne ©2013
Page Fault Rate 0 p 1
if p = 0 no page faults
if p = 1, every reference is a fault
Effective Access Time (EAT)
EAT = (1 – p) x memory access
+ p (page fault overhead)
Operating System Concepts – 9th Edition 9.19 Silberschatz, Galvin and Gagne ©2013
Demand Paging Example
Let p be the probability of page fault
Memory access time = 200 nanoseconds
Average page-fault service time = 8 milliseconds
– EAT = (1 – p) x 200 + p (8 milliseconds)
= (1 – p ) x 200 + p x 8,000,000
= 200 + 7,999,800 x p
If one access out of 1,000 causes a page fault, then
EAT = 8.2 microseconds.
EAT is directly proportional to no.of page fault
Operating System Concepts – 9th Edition 9.20 Silberschatz, Galvin and Gagne ©2013
Copy-on-Write
Operating System Concepts – 9th Edition 9.21 Silberschatz, Galvin and Gagne ©2013
Before Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.22 Silberschatz, Galvin and Gagne ©2013
After Process 1 Modifies Page C
Operating System Concepts – 9th Edition 9.23 Silberschatz, Galvin and Gagne ©2013
Page Replacement
When free frames not available then old page will be replaced by new
Operating System Concepts – 9th Edition 9.24 Silberschatz, Galvin and Gagne ©2013
Need For Page Replacement
Operating System Concepts – 9th Edition 9.25 Silberschatz, Galvin and Gagne ©2013
Basic Page Replacement
1. Find the location of the desired page on disk
3. Bring the desired page into the (newly) free frame; update the page and frame tables
4. Continue the process by restarting the instruction that caused the trap
Note now potentially 2 page transfers for page fault – increasing EAT
Operating System Concepts – 9th Edition 9.26 Silberschatz, Galvin and Gagne ©2013
Steps in Page Replacement
Operating System Concepts – 9th Edition 9.27 Silberschatz, Galvin and Gagne ©2013
Page and Frame Replacement Algorithms
Operating System Concepts – 9th Edition 9.28 Silberschatz, Galvin and Gagne ©2013
1. First-In-First-Out (FIFO) Algorithm
Example
Given a Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
3 frames available
Calculate the number of page faults?
Operating System Concepts – 9th Edition 9.29 Silberschatz, Galvin and Gagne ©2013
Solution
Operating System Concepts – 9th Edition 9.30 Silberschatz, Galvin and Gagne ©2013
Graph of Page Faults Versus The Number of Frames
Operating System Concepts – 9th Edition 9.31 Silberschatz, Galvin and Gagne ©2013
Limitation of FIFO
For some reference string Adding more frames can cause more page
faults is known as Belady’s Anomaly
Consider 1,2,3,4,1,2,5,1,2,3,4,5
Notice that the number of faults for four frames (ten) is greater than the
number of faults for three frames (nine)!
Operating System Concepts – 9th Edition 9.32 Silberschatz, Galvin and Gagne ©2013
FIFO Illustrating Belady’s Anomaly
Operating System Concepts – 9th Edition 9.33 Silberschatz, Galvin and Gagne ©2013
2. Least Recently Used (LRU) Algorithm
Replace page that has not been used in the longest period of time
Use past knowledge rather than future
Associate time of last use with each page
Example
Given a Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
3 frames available
Calculate the number of page faults?
Operating System Concepts – 9th Edition 9.34 Silberschatz, Galvin and Gagne ©2013
12 faults – better than FIFO but worse than OPT
Generally good algorithm and frequently used
Operating System Concepts – 9th Edition 9.35 Silberschatz, Galvin and Gagne ©2013
3. Optimal Algorithm
Replace page that will not be used for longest period of time
Best among other two
Results lowest number of page faults
Example
Given a Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1
3 frames available
Calculate the number of page faults?
Operating System Concepts – 9th Edition 9.36 Silberschatz, Galvin and Gagne ©2013
No of page faults is 9
Operating System Concepts – 9th Edition 9.37 Silberschatz, Galvin and Gagne ©2013
Drawback
Operating System Concepts – 9th Edition 9.38 Silberschatz, Galvin and Gagne ©2013
Example-2
Operating System Concepts – 9th Edition 9.39 Silberschatz, Galvin and Gagne ©2013
Example-3
Operating System Concepts – 9th Edition 9.40 Silberschatz, Galvin and Gagne ©2013
Example-4
Operating System Concepts – 9th Edition 9.41 Silberschatz, Galvin and Gagne ©2013
LRU Algorithm implementation
1. Counter implementation
we associate with each page-table entry a time-of-use field and add to the CPU a logical clock or
counter
When a page needs to be changed, look at the counters to find smallest value
2. Stack implementation
Stack contains the page numbers
When page is referenced it is kept at top
Bottom will contain the LRU page
Use doubly linked list for middle element deletion
Operating System Concepts – 9th Edition 9.42 Silberschatz, Galvin and Gagne ©2013
Allocation of Frames
Operating System Concepts – 9th Edition 9.43 Silberschatz, Galvin and Gagne ©2013
Fixed Allocation
Operating System Concepts – 9th Edition 9.44 Silberschatz, Galvin and Gagne ©2013
Priority Allocation
Operating System Concepts – 9th Edition 9.45 Silberschatz, Galvin and Gagne ©2013
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
process execution time can vary greatly
greater throughput so more common
Local replacement – each process selects from only its own set of
allocated frames
More consistent per-process performance
But possibly underutilized memory
Operating System Concepts – 9th Edition 9.46 Silberschatz, Galvin and Gagne ©2013
Thrashing
Operating System Concepts – 9th Edition 9.47 Silberschatz, Galvin and Gagne ©2013
Thrashing (Cont.)
Operating System Concepts – 9th Edition 9.48 Silberschatz, Galvin and Gagne ©2013
End
Operating System
Operating Concepts
System – 9th–Edition
Concepts 9th Edition 9.49 Silberschatz, Galvin and Gagne ©2013