Learning outcomes
- Evaluate page replacement algorithms and explain the phenomenon of thrashing.
These notes explain how demand paging affects performance, how the operating system chooses a page to replace, why a system may enter thrashing, how cache memory is organized, and why locality of reference is the key idea behind both caching and virtual memory.
Demand paging allows a process to run without loading all its pages into RAM at the beginning. This is memory-efficient, but it creates a new cost: when a required page is not in memory, the system must handle a page fault.
A page fault is expensive because the missing page has to be brought from disk into memory. RAM access is extremely fast, while disk access is extremely slow in comparison. So a small page-fault rate can still hurt performance badly.
The total time required to service a page fault usually includes:
Among these, disk I/O dominates the total delay.
The impact of page faults is measured using Effective Access Time (EAT).
where:
Suppose:
If p = 0.001 (that is, 1 fault per 1000 accesses), then:
So one tiny-looking page-fault probability causes a massive slowdown. That is classic OS mischief.
When a page fault occurs and there is no free frame available, the operating system must choose some page currently in memory to remove. This decision is made by a page replacement algorithm.
The objective is simple: choose a page whose removal causes the fewest future page faults. That objective is simple on paper; the machine, naturally, makes it annoying.
FIFO replaces the page that has been in memory the longest. The oldest page leaves first.
| Algorithm | Rule | Strength | Weakness |
|---|---|---|---|
| FIFO | Replace the page that entered memory first. | Very simple to implement. | May replace a heavily used page; can suffer from Belady’s anomaly. |
OPT replaces the page that will not be used for the longest time in the future. It gives the minimum possible page-fault rate for a fixed number of frames.
| Algorithm | Rule | Strength | Weakness |
|---|---|---|---|
| Optimal (OPT) | Replace the page whose next use is farthest in the future. | Best possible page-fault performance. | Needs future knowledge, so impractical in real systems. |
LRU replaces the page that has not been used for the longest time in the past. The idea is based on locality: pages used recently are likely to be used again soon.
| Algorithm | Rule | Strength | Weakness |
|---|---|---|---|
| LRU | Replace the least recently used page. | Usually performs well and approximates OPT. | Pure LRU is costly to implement exactly. |
Some algorithms, especially FIFO, can show Belady’s anomaly, where increasing the number of frames actually increases the number of page faults. That sounds wrong because it is wrong in spirit, but it happens.
| Algorithm | Nature | Belady’s Anomaly | Practical Use |
|---|---|---|---|
| FIFO | Simple queue-based | Yes | Educational / basic implementation |
| OPT | Theoretical best | No | Benchmark / comparison only |
| LRU | Locality-based | No | Conceptually strong; often approximated in practice |
Thrashing is a condition in which a process or the whole system spends more time paging than executing useful instructions.
It happens when the system does not have enough frames to support the current working sets of active processes. The CPU waits while pages are constantly swapped in and out.
Common approaches include:
A working set is the set of pages actively used by a process during a recent interval. If the process does not get enough frames to hold its working set, page faults rise and thrashing may begin.
CPU speed is much higher than main-memory speed. To reduce this speed gap, systems use cache memory, which is smaller but much faster than RAM.
Frequently used instructions and data are copied into cache so the CPU can access them quickly.
Computer storage is organized as a hierarchy:
| Term | Meaning |
|---|---|
| Cache hit | Required data is found in cache. |
| Cache miss | Required data is not in cache and must be fetched from lower memory level. |
| Hit ratio | Fraction of accesses that are cache hits. |
| Miss penalty | Extra time required when a miss occurs. |
| Method | Idea | Characteristic |
|---|---|---|
| Direct Mapping | Each memory block maps to exactly one cache line. | Simple and fast, but conflict misses can occur. |
| Associative Mapping | A memory block can go to any cache line. | Flexible but expensive hardware. |
| Set-Associative Mapping | A memory block maps to one set, but within that set it can occupy any line. | Good compromise between flexibility and cost. |
Direct mapping is like having one fixed chair per student. Associative mapping is like letting the student sit anywhere. Set-associative mapping is like giving the student one row, but freedom inside that row.
Locality of reference means that a program tends to access a relatively small set of memory locations repeatedly for some period of time.
This is the key reason why both caching and virtual memory work well.
| Type | Meaning | Example |
|---|---|---|
| Temporal Locality | If a location is used now, it is likely to be used again soon. | Loop counter reused every iteration |
| Spatial Locality | If a location is used now, nearby locations are likely to be used soon. | Sequential array traversal |
| Sequential Locality | Instructions or data are often accessed in sequence. | Executing program instructions one after another |
In virtual memory, locality means that a process usually needs only a small group of pages at one time. That group is often called a locality or part of its working set.
If those pages stay in memory, page faults remain low. If they do not, the process begins faulting frequently and may thrash.
Cache memory works because recently used and nearby data are likely to be accessed again. A good cache exploits locality and gives a high hit ratio.
| Topic | Main idea | What to remember |
|---|---|---|
| Performance of Demand Paging | EAT depends heavily on page-fault rate. | Even a small page-fault rate can cause large slowdown. |
| Page Replacement | Choose a victim page when no free frame exists. | FIFO is simple, OPT is best but impractical, LRU is strong but costly. |
| Thrashing | System spends more time paging than executing. | Usually caused by too few frames for current working sets. |
| Cache Organization | Fast small memory between CPU and RAM. | Hit/miss behavior and mapping policy matter. |
| Locality of Reference | Programs reuse recent and nearby memory locations. | This is why cache and virtual memory work. |
Demand-paging performance depends mainly on the page-fault rate. Since servicing a page fault is far slower than normal memory access, the operating system must keep faults rare.
Page replacement algorithms decide which page to remove when memory is full. FIFO is simple, OPT is ideal but theoretical, and LRU follows locality and performs well conceptually.
Thrashing occurs when the system spends too much time moving pages between disk and RAM and too little time doing useful work.
Cache memory organization places a small fast storage area between CPU and RAM. It depends on hit rate, miss penalty, and mapping policy.
Locality of reference is the principle that programs repeatedly use recent and nearby memory locations. It is the foundation of both caching and virtual memory.
The Optimal Page Replacement algorithm (OPT) replaces the page whose next use lies farthest in the future. In other words, when a page fault occurs and no free frame is available, the operating system looks ahead in the reference string and removes the page that will not be needed for the longest time.
The basic intuition is straightforward: if one page will be used very soon, it should be kept in memory, whereas a page that will not be required for a long time is the best candidate for replacement. Because of this decision rule, OPT produces the minimum possible number of page faults for a given page-reference string and a fixed number of page frames.
| Algorithm | Rule | Strength | Weakness |
|---|---|---|---|
| Optimal (OPT) | Replace the page whose next use is farthest in the future. | Gives the lowest possible page-fault rate for a fixed number of frames. | Requires exact knowledge of future page references, so it cannot be implemented perfectly in real systems. |
OPT is called an ideal or theoretical benchmark algorithm because no other page-replacement algorithm can perform better on the same reference string with the same number of frames. For this reason, OPT is often used as a standard for comparison when evaluating practical algorithms such as FIFO and LRU.
Consider the reference string:
Let the number of page frames be 3.
Current frames contain:
Now look ahead in the future reference string:
Either 1 or 7 may be replaced, since neither is needed again. Suppose 7 is replaced.
OPT always makes the best replacement decision because it uses complete future knowledge.
Even though OPT cannot be implemented exactly in real systems, it remains extremely important in Operating Systems because it provides a benchmark. When we compare FIFO, LRU, or any approximation algorithm, we often compare their page-fault count with OPT to judge how efficient they are.