CS330 - Operating Systems
Demand Paging, Page Replacement, Thrashing & Working Set Model
Virtual memory is a technique that allows the execution of processes that are not completely in memory.
Virtual memory can be implemented via:
Bring a page into memory only when it is needed
With each page table entry, a valid-invalid bit is associated:
| Page | Frame | Valid Bit | Meaning |
|---|---|---|---|
| 0 | 4 | v | In memory |
| 1 | 6 | v | In memory |
| 2 | - | i | Not in memory |
| 3 | 9 | v | In memory |
| 4 | - | i | Not in memory |
CPU references page
Invalid bit set
Find page on disk
Bring page to frame
Set valid bit
Continue execution
Page table entry has invalid bit set, causing page fault trap
OS checks internal table to verify if reference is valid
Find free frame from free-frame list
Read page from backing store into frame
Set page table entry, valid bit = v
Restart instruction that caused page fault
EAT = (1 - p) × ma + p × page_fault_time
Where:
Memory access time = 200 nanoseconds
Page fault service time = 8 milliseconds
Page fault rate (p) = 0.001
EAT = (1 - 0.001) × 200 + 0.001 × 8,000,000
EAT = 0.999 × 200 + 0.001 × 8,000,000
EAT = 199.8 + 8,000
EAT = 8,199.8 nanoseconds
This is about 40 times slower than no page faults!
For acceptable performance, page fault rate must be very low (< 0.0001)
Even 1 page fault per 1,000 accesses causes significant slowdown
When no free frame is available, we must replace a page
Want lowest page-fault rate
Evaluate algorithm by running on reference string and computing number of page faults
Using 3 frames
Replace the oldest page in memory
| Reference | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frame 0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 7 | 7 | 7 |
| Frame 1 | 0 | 0 | 0 | 0 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 1 | 1 | 1 | 1 | 1 | 0 | 0 | |
| Frame 2 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 3 | 3 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 1 | ||
| Fault? | F | F | F | F | - | F | F | F | F | F | F | - | - | F | F | - | - | F | F | F |
Total Page Faults: 15
Replace page that will not be used for longest period of time
| Reference | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frame 0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 2 | 7 | 7 | 7 |
| Frame 1 | 0 | 0 | 0 | 0 | 0 | 0 | 4 | 4 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | |
| Frame 2 | 1 | 1 | 1 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 3 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | ||
| Fault? | F | F | F | F | - | F | - | F | - | F | - | - | - | F | - | - | - | F | - | - |
Total Page Faults: 9 (Optimal but requires future knowledge)
Replace the page that has not been used for the longest time
| Reference | 7 | 0 | 1 | 2 | 0 | 3 | 0 | 4 | 2 | 3 | 0 | 3 | 2 | 1 | 2 | 0 | 1 | 7 | 0 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frame 0 | 7 | 7 | 7 | 2 | 2 | 2 | 2 | 4 | 4 | 4 | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| Frame 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 3 | 3 | 3 | 3 | 0 | 0 | 0 | 0 | 0 | |
| Frame 2 | 1 | 1 | 1 | 3 | 3 | 3 | 2 | 3 | 3 | 3 | 2 | 2 | 2 | 2 | 2 | 7 | 7 | 7 | ||
| Fault? | F | F | F | F | - | F | - | F | F | - | F | - | F | F | - | F | - | F | - | - |
Total Page Faults: 12
| Algorithm | Description | Page Faults | Advantages | Disadvantages |
|---|---|---|---|---|
| FIFO | Replace oldest page | 15 | Simple to implement | Poor performance, Belady's anomaly |
| Optimal | Replace page not used for longest time in future | 9 | Minimum page faults | Requires future knowledge (impossible) |
| LRU | Replace least recently used page | 12 | Good performance, approximates optimal | Expensive to implement exactly |
| Clock | Approximation of LRU using reference bit | ~13 | Efficient implementation | Not as good as true LRU |
| LFU | Replace least frequently used | Varies | Considers usage frequency | Pages used heavily initially stay |
For some page replacement algorithms (like FIFO), more frames can cause MORE page faults!
Example: Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
A process is thrashing if it is spending more time paging than executing
Degree of Multiprogramming →
If a memory location is referenced, it will tend to be referenced again soon
Example: Loop counters, frequently used variables
If a memory location is referenced, nearby locations will tend to be referenced soon
Example: Array elements, sequential code
Working set W(t, Δ) = set of pages referenced in last Δ time units
Working Set Window (Δ = 10)
Working Set = {1, 2, 3, 5, 6, 7}
Working Set Size (WSS) = 6
If Total WSS > Available memory → Thrashing
If D = Σ WSSᵢ > m (available frames)
Then suspend one or more processes
Establish acceptable page-fault frequency range
Each process gets equal share
m frames, n processes
Each gets m/n frames
Allocate according to size
sᵢ = size of process i
S = Σsᵢ
aᵢ = (sᵢ/S) × m
Based on process priority
Higher priority = more frames
Reference String: 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
Frames: 3
Calculate page faults for FIFO, LRU, and Optimal algorithms
FIFO: 16 page faults
LRU: 15 page faults
Optimal: 11 page faults
Memory access time = 100 ns
Page fault service time = 25 ms
Page fault rate = 0.0001
Calculate EAT
EAT = (1 - 0.0001) × 100 + 0.0001 × 25,000,000
EAT = 99.99 + 2,500
EAT = 2,599.99 ns
Reference string: 2, 3, 2, 4, 5, 3, 6, 3, 4, 5, 3
Window size Δ = 4
Find working set at t = 7
Look at last 4 references: 5, 3, 6, 3
Working Set = {3, 5, 6}
WSS = 3
Q1. Which page replacement algorithm suffers from Belady's anomaly?
Answer: FIFO
Q2. A system is thrashing when:
Answer: Processes spend more time paging than executing
Q3. The working set model is based on:
Answer: Locality of reference
Q4. Valid-invalid bit in page table indicates:
Answer: Whether page is in memory (v) or on disk (i)
Q5. List two ways to improve system performance when thrashing occurs.
Answer:
Q6. Why is the optimal page replacement algorithm not implementable?
Answer: It requires future knowledge of the reference string, which is impossible to know in advance. It's used only as a benchmark to compare other algorithms.
[Insert detailed page fault handling diagram with OS, memory, and disk]
[Insert visual comparison of FIFO, LRU, and Optimal]
[Insert CPU utilization vs degree of multiprogramming curve]
[Insert working set window and WSS calculation example]
Note: Please provide the actual diagrams from your course materials to replace these placeholders.