Virtual Memory & Demand Paging in OS: Page Faults, Performance & Examples

OPERATING SYSTEM - CHAPTER 24

Virtual Memory & Demand Paging in OS: Page Faults, Performance & Examples

Virtual Memory is one of those Operating System topics that sounds more complicated than it really is. Students often think that virtual memory means extra physical RAM. It does not.

Virtual memory creates the illusion that a process has a large, continuous memory space even when only some of its pages are currently present in physical RAM.

Demand Paging takes this idea one step further: a page is brought into main memory only when the process actually needs it.

Quick Answer:

Virtual Memory is a memory-management technique that allows a process to use a virtual address space that can be larger than the currently available physical memory. Only the required pages need to remain in RAM. Other pages may remain on secondary storage until needed.

Demand Paging loads a page only when that page is referenced. If the required page is not currently present in RAM, a Page Fault occurs. The operating system then loads the page into a free frame, updates the page table and restarts the interrupted instruction.

Why Does Virtual Memory Feel Difficult?

Virtual Memory & Demand Paging in OS: Page Faults, Performance & Examples

Virtual Memory becomes confusing mainly because students try to imagine that the entire program must always remain inside RAM.

Demand paging works differently. Only the pages that are currently needed may be kept in physical memory.

1
Virtual Does Not Mean Fake RAM Virtual memory is an address-space abstraction supported by memory-management hardware and the operating system.
2
Page Fault Is Not Always an Error A normal demand-paging page fault can simply mean that a valid page is currently not resident in RAM.
3
Disk Access Is Very Slow Handling a page fault is much slower than a normal RAM access, so even a small page-fault rate can affect performance.
Easy Memory Trick:

Virtual Memory = Large logical view

RAM = Pages currently needed

Secondary Storage = Pages not currently resident

Demand Paging = Load page only when demanded

What Is Virtual Memory in Operating System?

A process works with addresses generated inside its own logical or virtual address space.

These addresses do not directly mean that every byte of the process must already exist in physical RAM.

The operating system and memory-management hardware translate virtual addresses into physical memory addresses.

Simple Definition:
Virtual Memory is a memory-management technique that gives a process a virtual address space and allows only the required parts of that process to remain in physical memory at a given time.

The Big Idea

A program may contain many pages. But the program may currently be using only a small number of them.

Instead of loading everything immediately, the operating system can keep only the active pages in RAM.

Virtual Address Space vs Physical RAM

Virtual Address Space
Page 0
Page 1
Page 2
Page 3
Page 4
Page 5
→
Physical RAM
Frame 0 - Page 3
Frame 1 - Other Process
Frame 2 - Page 0
Frame 3 - Free
Notice that the process has six virtual pages in this simple example, but only some pages are currently present in RAM.

Why Do We Need Virtual Memory?

M
Better Memory Usage Pages that are not currently required do not have to occupy RAM.
P
More Processes RAM can hold active pages from several processes instead of complete processes.
A
Large Address Space A process can use a virtual address space that is not limited to one continuous physical-RAM area.

Where Are Non-Resident Pages Kept?

Pages that are not currently in RAM may have backing data on secondary storage.

When such a page is needed, the operating system can bring it into a physical frame.

SIMPLE VIRTUAL MEMORY VIEW
Process
Virtual Pages
↔
Physical RAM
Active Pages
↔
Secondary Storage
Backing Data
RAM and secondary storage are not equally fast. Accessing storage during a page fault is far slower than performing a normal memory access.

What Is Demand Paging?

Demand Paging follows a simple rule:

Load a page into physical memory only when that page is actually needed.

Suppose a process has 20 pages. The operating system does not necessarily need to load all 20 pages before execution begins.

It may load only the pages that the process currently references.

Demand Paging is sometimes described as a lazy loading approach for pages: do not bring a page into RAM until it is demanded.

What Is Pure Demand Paging?

In the strict idea of pure demand paging, a page is brought into memory only after the process actually attempts to reference it.

Page not referenced?
Do not load it.

Page referenced?
Bring it into memory if necessary.

How Does the System Know Whether a Page Is in RAM?

A page-table entry can contain status information indicating whether the required mapping is currently resident and usable.

Page
Frame
Status
0
2
Present
1
-
Not Resident
2
-
Not Resident
3
0
Present

How to Read This

Page 0 is currently stored in Frame 2.

Page 3 is currently stored in Frame 0.

Page 1 and Page 2 are valid process pages in this example, but they are not currently resident in RAM.

What Is a Page Fault?

Suppose the CPU generates an address belonging to Page 2.

The page table indicates that Page 2 is not currently present in physical memory.

The processor cannot complete the memory access normally.

Simple Definition:
A Page Fault is an exception that occurs when a process references a virtual-memory page that is not currently resident in physical memory and the operating system must handle the situation.
A page fault does not automatically mean that the program has made an illegal memory access. In demand paging, a valid page may simply need to be loaded into RAM.

Valid Page Fault vs Invalid Memory Reference

When a page fault occurs, the operating system must determine whether the requested virtual address is actually valid for the process.

Valid Reference

The page belongs to the process but is currently not resident.

The operating system can load the required page and continue execution.

Invalid Reference

The process is trying to access an address that is not part of its valid address space or violates protection.

The operating system should not treat this as a normal page-in operation.

How Does the Operating System Handle a Page Fault?

PAGE FAULT HANDLING PIPELINE

From Missing Page to Restarted Instruction

1 Detect Fault Required page is not currently resident in RAM.
2 Validate Address OS checks whether the memory reference is legal.
3 Find a Frame Use a free frame or select a victim page if replacement is required.
4 Load Page Required page data is brought into the selected frame.
5 Update Mapping Page table and related translation information are updated.
6 Restart Instruction The instruction that caused the page fault can be attempted again.

What If No Free Frame Is Available?

If a free frame exists, the operating system can use it for the incoming page.

But physical memory may already be full.

In that situation, the operating system may need to choose an existing page as a victim page and reuse its frame.

The method used to decide which page should leave memory is called a Page Replacement Algorithm. Common algorithms such as FIFO, Optimal and LRU are normally studied as the next topic.

What If the Victim Page Was Modified?

A page that has been modified while in RAM may need to be written back to its backing storage before its frame can be reused, depending on the page and system design.

A page-table entry may contain a Dirty or Modified Bit that helps indicate whether the page has changed.

A page fault can become even more expensive when the operating system must first write a modified victim page and then read the required page.

Why Are Page Faults Expensive?

A normal RAM access is extremely fast compared with loading data from secondary storage.

Therefore, demand paging performs well only when page faults remain relatively infrequent.

NORMAL ACCESS Memory Access
RAM access is comparatively fast
PAGE FAULT Fault Handling
Trap + OS work + possible storage I/O + page-table update + restart
RESULT Performance
Even a small fault rate can increase average access time

Effective Access Time in Demand Paging

Demand-paging performance is commonly studied using Effective Access Time.

Let:

  • p = Page-fault probability
  • m = Normal memory-access time
  • F = Time for a page-fault access path
A common simplified formula is:
Effective Access Time = (1 - p) × m + p × F
Different textbooks may define the page-fault service term slightly differently. For example, some definitions make the fault time include the final restarted memory access while others list individual components separately. In numerical questions, always follow the exact definition given in the question.

Page Fault Performance Solved Example

Question

Suppose:

Normal Memory Access Time = 100 ns Page Fault Access Time = 8 ms Page Fault Probability = 0.00001

Step 1: Convert Milliseconds to Nanoseconds

1 ms = 1,000,000 ns 8 ms = 8,000,000 ns

Step 2: Apply the Formula

EAT = (1 - p) × m + p × F = (1 - 0.00001) × 100 + 0.00001 × 8,000,000

Step 3: Calculate

EAT = 0.99999 × 100 + 80 = 99.999 + 80 = 179.999 ns
Final Answer:
Effective Access Time ≈ 180 ns

Why Even a Small Page-Fault Rate Matters

Look at the previous example carefully.

The page-fault probability was only 0.00001.

That means page faults were very rare.

Even then, the average access time increased from about 100 ns to about 180 ns because a page fault is extremely expensive compared with RAM access.

This is why demand-paging performance depends strongly on keeping the page-fault rate low.

Demand Paging Performance Calculator

Use this calculator to see how page-fault probability affects Effective Access Time.

Page Fault Performance Lab

Locality of Reference and Demand Paging

Demand paging works well because programs normally do not access every page equally at every moment.

Programs often repeatedly access a relatively small group of instructions and data for some period of time.

This behavior is called Locality of Reference.

Example Active Page Region
P0
P1
P2
P3
P4
P5
P6
P7

In this simple illustration, the process is currently working mainly with Pages 2, 3 and 4.

If these actively used pages remain in RAM, many memory references can complete without generating page faults.

Temporal and Spatial Locality

Temporal Locality

If a memory location has been used recently, it may be used again soon.

Example: instructions inside a loop.

Spatial Locality

If one memory location is accessed, nearby locations may also be accessed soon.

Example: reading sequential elements of an array.

Basic Idea of a Working Set

At a particular time, a process may be actively using only a subset of its pages.

This frequently used group is commonly discussed using the idea of a Working Set.

Think of the Working Set as the group of pages a process needs actively during a recent period of execution.

If enough of these important pages remain in memory, the process can execute with relatively few page faults.

What Is Thrashing?

Demand paging becomes harmful when the system spends too much time moving pages between storage and RAM instead of doing useful CPU work.

Simple Definition:
Thrashing is a condition in which a system experiences excessive paging activity and spends a large amount of time servicing page faults rather than executing useful process instructions.
Page-Fault Activity
Low Increasing Thrashing Risk

How Can Thrashing Happen?

  1. Too many processes compete for a limited number of frames.
  2. A process receives too few frames for its active pages.
  3. The process generates frequent page faults.
  4. Pages are repeatedly loaded and removed.
  5. Storage activity increases.
  6. Useful CPU progress may fall.
High multiprogramming is not automatically better. If processes do not have enough frames for their active working sets, page faults can rise dramatically.

Page Fault Rate and System Performance

Page Fault Rate What It Means Possible Performance
Very Low Most required pages are already resident Good demand-paging performance
Moderate Pages are occasionally fetched Additional overhead becomes visible
Very High Frequent page movement Severe slowdown and possible thrashing

Advantages and Disadvantages of Demand Paging

Advantages

  • Pages are loaded only when needed.
  • Unused pages do not immediately occupy RAM.
  • More processes may share physical memory.
  • Large virtual address spaces are possible.
  • Program startup may avoid loading every page immediately.
  • Works naturally with virtual-memory systems.

Disadvantages

  • Page faults are expensive.
  • Secondary-storage I/O can be slow.
  • Page tables require management.
  • Page replacement may be necessary.
  • Too many page faults can cause thrashing.
  • Hardware and OS support are required.

Virtual Memory vs Physical Memory

Basis Virtual Memory Physical Memory
Meaning Address-space abstraction seen by processes Actual RAM hardware
Address Virtual or logical address Physical address
Size Can exceed the amount of currently available RAM Limited by installed physical memory
Translation Requires virtual-to-physical mapping Represents actual memory locations
Demand Paging Pages can be loaded when needed Holds currently resident pages

Paging vs Demand Paging

Basis Paging Demand Paging
Main Idea Divide logical memory into pages and physical memory into frames Load a page into RAM only when required
Focus Address translation and non-contiguous allocation On-demand page loading
Page Fault Paging alone explains mapping Page faults are central to on-demand loading
Secondary Storage Not the main concept in basic paging explanation Non-resident page data may need to be fetched from storage

Simple Demand Paging Example

Process Pages

Process has: Page 0 Page 1 Page 2 Page 3 Page 4

Suppose RAM currently contains:

Page 0 → Frame 2 Page 3 → Frame 5

CPU Requests Page 3

Page 3 is already present.

No demand-page transfer is required for this reference.

CPU Requests Page 1

Page 1 is a valid page of the process, but it is not currently resident.

A page fault occurs and the operating system must bring Page 1 into a physical frame before the reference can complete.

Common Student Confusions

Is Virtual Memory the Same as RAM?

No.

RAM is physical memory hardware. Virtual memory is an address-space and memory-management abstraction.

Does a Page Fault Mean the Program Is Wrong?

Not necessarily.

In demand paging, a valid page may simply not be resident yet.

Does Every Page Fault Require Page Replacement?

No.

If a free physical frame is available, the operating system can use that frame. Replacement becomes necessary when a suitable free frame is not available.

Does Demand Paging Load the Complete Process?

No.

The key idea is to load pages when they are required.

Is a Higher Degree of Multiprogramming Always Better?

No.

If too many processes compete for too few frames, page-fault activity can increase and lead to thrashing.

Does Virtual Memory Make Storage as Fast as RAM?

No.

Storage-backed page transfers are much slower than normal RAM accesses.

One-Minute Revision

Virtual Memory Gives processes a virtual address space independent of direct one-to-one physical placement.
Demand Paging Load a page when it is actually referenced.
Resident Page Page is currently available in physical memory.
Page Fault Required virtual page is not currently resident and needs OS handling.
Free Frame Can receive the incoming page directly.
Page Replacement Needed when another page must leave to make a frame available.
Locality Programs tend to reuse a small region of memory for some time.
Working Set Pages actively needed during a recent execution period.
Thrashing Excessive page-fault activity dominates useful execution.

Virtual Memory and Demand Paging Exam Preparation

2-Mark Virtual Memory Definition

Virtual Memory is a memory-management technique that provides a process with a virtual address space and allows only the required parts of the process to be present in physical memory at a time.

2-Mark Demand Paging Definition

Demand Paging is a virtual-memory technique in which a page is loaded into physical memory only when it is actually referenced.

Page Fault Short Answer

A page fault occurs when a process references a virtual page that is not currently resident in physical memory and the operating system must handle the missing-page condition.

Page Fault Handling Steps

  1. Detect the page fault.
  2. Check whether the reference is valid.
  3. Find a free frame or select a victim page.
  4. Load the required page.
  5. Update page-table information.
  6. Restart the interrupted instruction.

Effective Access Time Formula

EAT = (1 - p) × Memory Access Time + p × Page Fault Access Time

For a 5-Mark Question

  1. Define Virtual Memory.
  2. Explain Demand Paging.
  3. Define Page Fault.
  4. Explain Page Fault Handling.
  5. Write advantages and limitations.

For a 10-Mark Question

  1. Meaning of Virtual Memory.
  2. Need for Virtual Memory.
  3. Virtual Address Space and Physical Memory.
  4. Demand Paging.
  5. Page-table resident information.
  6. Page Fault.
  7. Page Fault Handling Steps.
  8. Free Frame and Page Replacement.
  9. Dirty Page Concept.
  10. Demand-Paging Performance.
  11. Effective Access Time.
  12. Locality of Reference.
  13. Working Set.
  14. Thrashing.
  15. Advantages and Disadvantages.

Frequently Asked Questions

What is Virtual Memory in Operating System?

Virtual Memory is a memory-management technique that gives a process a virtual address space and allows only required pages to be present in physical memory at a given time.

What is Demand Paging?

Demand Paging is a technique in which a page is brought into physical memory only when the process actually references that page.

What is a Page Fault?

A Page Fault occurs when a process references a virtual page that is not currently resident in physical memory and the operating system must handle the condition.

What Happens After a Page Fault?

The operating system checks the memory reference, obtains a frame, loads the required page if the reference is valid, updates the mapping and restarts the interrupted instruction.

Does Every Page Fault Require Page Replacement?

No. If a free frame is available, the required page can be loaded into that frame. Page replacement is needed when a usable free frame is not available.

Why Is a Page Fault Slow?

A page fault may require operating-system processing and secondary storage access, which is much slower than a normal RAM access.

What is Effective Access Time in Demand Paging?

Effective Access Time is the average memory-access time after considering both normal memory accesses and the probability and cost of page faults.

What is Locality of Reference?

Locality of Reference is the tendency of programs to repeatedly access a relatively small group of memory locations or nearby locations during a period of execution.

What is Thrashing?

Thrashing is a condition in which excessive page faults cause the system to spend a large amount of time transferring pages instead of performing useful computation.

What is the Difference Between Paging and Demand Paging?

Paging divides logical memory into pages and physical memory into frames, while Demand Paging adds the rule that pages are brought into physical memory when they are actually needed.

Conclusion

Virtual Memory allows a process to work with a virtual address space without requiring every page of the process to remain in physical RAM at the same time.

Demand Paging improves memory use by loading a page only when it is actually required.

When a valid required page is not currently resident, a Page Fault occurs. The operating system then checks the reference, finds or creates an available frame, loads the page, updates the page table and restarts execution.

The idea is powerful, but page faults are expensive because storage operations are much slower than normal RAM accesses.

Demand paging therefore works best when programs show good Locality of Reference and when their actively used pages remain in memory.

If the system produces too many page faults, it can enter a condition called Thrashing, where paging activity dominates useful processing.

Final Memory Trick:

Virtual Memory = Large Virtual Address Space

Demand Paging = Load When Needed

Page Present = Normal Access

Page Missing = Page Fault

Free Frame = Load Page

No Free Frame = Page Replacement May Be Needed

Low Page Fault Rate = Better Performance

Too Many Page Faults = Thrashing

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Join Telegram Exam updates and free resources