Virtual memory separates the memory that a program sees from the physical memory, so that a program larger than memory can still run.
Virtual Memory
Virtual memory is a technique that allows a process to run even if only part of it is in main memory. Programs often contain code for errors and rare options that is seldom used, and arrays declared larger than needed. With virtual memory, a program is not limited by the size of physical memory, more programs can run at once, raising CPU utilisation, and less I/O is needed to load or swap programs. It also makes sharing of libraries and fast process creation easy.
Definition: Demand paging. A virtual-memory technique in which a page is brought into memory only when it is first referenced, rather than loading the whole program at the start.
Demand Paging
In demand paging, pages are loaded only when they are needed. A lazy swapper, the pager, never brings in a page unless it is required. Each page-table entry has a valid-invalid bit: valid means that the page is in memory, invalid means that it is not (or is not part of the process).
Page Fault Handling
When a process refers to a page marked invalid, a page fault occurs, which traps to the OS. The steps are: (1) check whether the reference is legal; if not, end the process. (2) Find a free frame. (3) Schedule a disk read to bring the page into the frame. (4) When the read finishes, update the page table and set the bit to valid. (5) Restart the instruction that caused the fault. If there is no free frame, a page must be replaced, as the next topic describes. Pure demand paging starts a process with no pages in memory.
Performance
Demand paging is efficient only if page faults are rare. With page-fault rate p, memory access time ma and page-fault service time t, the effective access time is EAT = (1 − p) × ma + p × t. Since t is in milliseconds and ma in nanoseconds, even a small p slows the system greatly. Programs show locality of reference: they use a small set of pages at any time, which keeps p low.
Table 4.4: Steps of handling a page fault
| Step | Action | Done by |
|---|---|---|
| 1 | Reference to an invalid page traps | Hardware |
| 2 | Check legality, find a free frame | OS |
| 3 | Read the page from disk | OS and disk |
| 4 | Update the page table to valid | OS |
| 5 | Restart the instruction | Hardware |
Example 4.4: Effective access time
Problem: Memory access takes 200 ns and a page fault takes 8 ms to service. Find the effective access time if 1 access in 1000 causes a page fault. What fault rate keeps the slowdown under 10 percent?
Solution:
- EAT = (1 − 0.001) × 200 + 0.001 × 8,000,000 = 199.8 + 8000 = 8199.8 ns, about 41 times slower.
- For EAT < 220 ns: 200 + p × 7,999,800 < 220, so p < 0.0000025.
Result: EAT is about 8.2 µs; to keep the slowdown below 10 percent, fewer than 1 access in 400,000 may fault.
#include <stdio.h>
int main(void) {
double ma = 200, t = 8e6, p[] = {0.001, 0.0001, 0.0000025};
for (int i = 0; i < 3; i++)
printf("p = %.7f: EAT = %.1f ns\n",
p[i], (1 - p[i]) * ma + p[i] * t);
return 0;
}
Output
p = 0.0010000: EAT = 8199.8 ns
p = 0.0001000: EAT = 1000.0 ns
p = 0.0000025: EAT = 220.0 ns
Common mistakes
- Thinking that virtual memory is the same as swapping whole processes.
- Ignoring how much a small page-fault rate slows the system.
- Forgetting to restart the faulting instruction.
Key takeaways
- Virtual memory lets a program run with only part of it in memory.
- Demand paging loads a page only on its first reference.
- A page fault traps to the OS, which loads the page.
- EAT = (1 − p) × ma + p × fault time.
Practice questions
- What is a page fault?
- What is the valid-invalid bit?
- Why does demand paging work well in practice?
Answers to the practice questions
- A trap that occurs when a process refers to a page that is not in memory.
- A bit in a page-table entry showing whether the page is in memory.
- Because of locality of reference, page faults are rare.
Sources
- Official BCA syllabus Operating Systems, course code 25BCA402DS03, Unit IV
