BCS401 Operating System Semester IV · AY 2025-26 onward
Unit 4 · Memory Management

Lecture 7: Virtual memory & demand paging

Learning outcomes

  • Discuss the concept of virtual memory and how demand paging works.
Lecture Notes

Main content

Paged Segmentation, Virtual Memory Concepts, and Demand Paging

These notes explain three closely related memory-management concepts: paged segmentation, virtual memory, and demand paging. These techniques help an operating system manage large programs efficiently, protect memory, and allow processes to run even when the complete program is not present in main memory.

Memory Management Paged Segmentation Virtual Memory Demand Paging Page Fault MMU

1. Paged Segmentation

1.1 Meaning

Paged segmentation is a hybrid memory-management technique that combines the logical advantages of segmentation with the physical-memory efficiency of paging.

In segmentation, a program is divided into meaningful logical parts such as code, data, stack, heap, and procedures. However, pure segmentation suffers from external fragmentation because segments are variable in size.

In paging, memory is divided into fixed-size pages and frames. Paging removes external fragmentation, but it does not represent the programmer’s logical view very naturally.

Core idea: In paged segmentation, a program is first divided into logical segments. Then each segment is further divided into fixed-size pages.

1.2 Why paged segmentation is used

Paged segmentation is used to achieve both:

  • Logical organization of memory through segmentation.
  • Efficient physical allocation through paging.

This means the operating system can protect and share logical program units, while still avoiding the major external-fragmentation problem of pure segmentation.

1.3 Address format in paged segmentation

In paged segmentation, the logical address usually contains three parts:

Logical Address = < Segment Number, Page Number, Offset >
Address Part Meaning
Segment Number Identifies the logical segment, such as code segment, data segment, or stack segment.
Page Number Identifies the page inside the selected segment.
Offset Identifies the exact byte or word inside the selected page.

1.4 Translation process

  1. The CPU generates a logical address containing segment number, page number, and offset.
  2. The segment number is used to access the segment table.
  3. The segment-table entry gives the base address of the page table for that segment.
  4. The page number is used to index the page table of that segment.
  5. The page-table entry gives the frame number in physical memory.
  6. The frame number is combined with the offset to generate the physical address.
Physical Address = Frame Number + Offset
Logical Address < Segment, Page, Offset > Segment Table Seg 0 → PT0 Seg 1 → PT1 Seg 2 → PT2 Page Table Page 0 → Frame 9 Page 1 → Frame 3 Page 2 → Frame 12 Physical Address [ Frame | Offset ] Physical Memory Frames Frame 3 Frame 9 Frame 12
Figure 1: Address translation in paged segmentation.

1.5 Protection in paged segmentation

Protection can be applied at two levels:

  • Segment level: each logical segment can have access rights such as read-only, read-write, or execute-only.
  • Page level: each page inside a segment can have valid-invalid bits and protection bits.

This makes paged segmentation powerful for systems where different parts of a program require different levels of protection.

1.6 Advantages of paged segmentation

  • Combines logical view of segmentation with physical efficiency of paging.
  • Reduces external fragmentation because segments are divided into pages.
  • Allows better sharing of program modules.
  • Supports fine-grained protection.
  • Useful for large and complex address spaces.

1.7 Disadvantages of paged segmentation

  • Address translation becomes more complex.
  • Requires both segment tables and page tables.
  • More memory overhead due to multiple tables.
  • May require TLB support to keep translation fast.

Worked Example: Paged Segmentation Translation

Suppose a logical address is:

< Segment 2, Page 3, Offset 100 >

Segment table entry for Segment 2 points to Page Table PT2. In PT2, Page 3 maps to Frame 8.

Physical Address = Frame 8 + Offset 100

If frame size is 1024 bytes:

Physical Address = (8 × 1024) + 100 = 8292

2. Virtual Memory Concepts

2.1 Meaning of virtual memory

Virtual memory is a memory-management technique that allows a process to execute even when the entire process is not present in main memory.

The process sees a large continuous logical address space, but physically, only some required parts of the process are kept in RAM. The remaining parts are stored on secondary storage, usually disk.

Simple meaning: Virtual memory creates the illusion of large memory even when actual RAM is limited.

2.2 Why virtual memory is needed

Without virtual memory, a complete program must be loaded into RAM before execution. This creates problems when:

  • the program is larger than available physical memory,
  • many processes must run at the same time,
  • only a small part of a large program is actually used at one time.

Virtual memory solves this by loading only required parts of the program into memory.

2.3 Logical memory and physical memory

Memory Type Meaning
Logical / Virtual Memory The memory space seen by the process. It may be much larger than actual RAM.
Physical Memory The actual RAM available in the computer system.
Backing Store Disk area used to store pages that are not currently in RAM.
Virtual Address Space Page 0 Page 1 Page 2 Page 3 Page 4 Physical Memory Page 0 in RAM Other process / free Page 3 in RAM Other process / free Page 4 in RAM Backing Store Page 1 on Disk Page 2 on Disk
Figure 2: Virtual memory separates logical address space from physical memory.

2.4 Basic working of virtual memory

  1. The process generates virtual addresses.
  2. The MMU translates virtual addresses into physical addresses.
  3. If the required page is in RAM, execution continues normally.
  4. If the required page is not in RAM, a page fault occurs.
  5. The OS brings the required page from disk into RAM.

2.5 Locality of reference

Virtual memory works efficiently because most programs follow the principle of locality of reference.

This means that at a given time, a program tends to use only a small part of its code and data repeatedly. For example, inside a loop, the same few instructions and variables may be accessed again and again.

Type of Locality Meaning Example
Temporal Locality Recently used items are likely to be used again soon. A loop variable accessed repeatedly.
Spatial Locality Nearby memory locations are likely to be accessed soon. Sequential access of array elements.

2.6 Advantages of virtual memory

  • Allows execution of programs larger than physical memory.
  • Improves memory utilization.
  • Allows more processes to run simultaneously.
  • Provides process isolation and protection.
  • Supports sharing of code and libraries.
  • Reduces the need to load the full program at startup.

2.7 Disadvantages of virtual memory

  • Requires hardware support such as MMU and page tables.
  • Page faults can be very expensive.
  • Too many page faults can cause thrashing.
  • Page-table management adds overhead.
Important: Virtual memory is powerful, but it depends on keeping page faults low. If the system keeps fetching pages from disk again and again, performance becomes painfully slow.

3. Demand Paging

3.1 Meaning

Demand paging is a virtual-memory technique in which a page is loaded into main memory only when it is actually needed.

Instead of loading the complete process into RAM, the OS loads only those pages that are demanded during execution.

Simple definition: Demand paging means loading a page only when a process actually refers to it.

3.2 Basic idea

At the beginning, only a few pages of a process may be present in RAM. The remaining pages stay on disk. If the process tries to access a page that is not in RAM, the hardware generates a page fault. The OS then brings that page into memory.

3.3 Valid-invalid bit

Demand paging uses a valid-invalid bit in the page table.

Bit Value Meaning
Valid The page is legal and currently present in main memory.
Invalid The page is either not part of the process address space or is currently not in memory.

3.4 Page fault

A page fault occurs when a process refers to a page that is not currently present in RAM. In demand paging, page faults are normal events, not always errors.

However, the OS must check whether the reference is legal:

  • If the page belongs to the process but is not in RAM, it is a valid page fault.
  • If the page does not belong to the process, it is an illegal memory reference.
1. CPU references page 2. Check page table 3. Page not in RAM Page Fault Trap 4. OS checks validity 5. Find free frame 6. Load page from disk into selected frame 7. Update page table mark page valid 8. Restart instruction
Figure 3: Page-fault handling in demand paging.

3.5 Steps in handling a page fault

  1. The process references a page.
  2. The MMU checks the page-table entry.
  3. If the valid-invalid bit shows that the page is not in memory, a page fault occurs.
  4. The operating system checks whether the memory reference is legal.
  5. If the reference is illegal, the process is terminated.
  6. If the reference is legal, the OS locates the page on disk.
  7. The OS finds a free frame in main memory.
  8. If no free frame is available, a page-replacement algorithm is used.
  9. The required page is read from disk into the selected frame.
  10. The page table is updated.
  11. The instruction that caused the page fault is restarted.

3.6 Pure demand paging

In pure demand paging, no page is loaded until it is actually referenced. A process may start execution with very few pages in memory.

This saves memory, but it may cause many page faults at the beginning of execution. Therefore, some systems may use limited prepaging, where a few likely-needed pages are loaded in advance.

3.7 Demand paging performance

Demand paging is efficient only when the page-fault rate is low. A normal memory access is very fast, but a page fault may require disk I/O, which is much slower.

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

Here:

  • p = page-fault rate
  • Memory Access Time = time for normal RAM access
  • Page Fault Service Time = time required to handle a page fault
Important: Even a very small page-fault rate can seriously affect performance because page-fault service time is much larger than normal memory-access time.

3.8 Advantages of demand paging

  • Only required pages are loaded into memory.
  • Less initial loading time for a process.
  • Better use of RAM.
  • Allows execution of large programs.
  • Supports more processes in memory at the same time.

3.9 Disadvantages of demand paging

  • Page faults are costly.
  • Requires page-table support and valid-invalid bits.
  • Requires backing store.
  • May lead to thrashing if too many page faults occur.
  • Needs efficient page-replacement algorithms.

Worked Example: Page Fault Case

Suppose a process has pages 0, 1, 2, 3, and 4 in its logical address space. Pages 0 and 3 are currently in RAM. Pages 1, 2, and 4 are on disk.

Page Status
Page 0 In RAM
Page 1 On Disk
Page 2 On Disk
Page 3 In RAM
Page 4 On Disk

If the CPU references Page 3, no page fault occurs because it is already in RAM.

If the CPU references Page 2, a page fault occurs. Since Page 2 is a legal page, the OS loads it from disk into a free frame.

If the CPU references Page 8, this is an illegal reference because Page 8 is not part of the process address space.

4. Comparison Table

Feature Paged Segmentation Virtual Memory Demand Paging
Main idea Segments are divided into pages. Process can run without being fully loaded into RAM. Pages are loaded only when referenced.
Purpose Combine segmentation and paging benefits. Create illusion of large memory. Implement virtual memory efficiently.
Address format Segment number, page number, offset. Virtual address translated to physical address. Virtual page mapped when demanded.
Main benefit Logical structure with reduced fragmentation. Runs larger programs and improves memory use. Avoids loading unused pages.
Main drawback Complex address translation. Page faults can slow the system. High page-fault rate causes poor performance.

5. One-Page Summary

Paged segmentation is a hybrid memory-management technique in which a program is divided into logical segments, and each segment is further divided into pages. It combines the logical organization of segmentation with the efficient allocation of paging.

Virtual memory allows a process to execute even when the entire process is not present in main memory. It separates the logical address space from physical memory and gives the user the illusion of a large memory.

Demand paging is a virtual-memory technique where pages are loaded into memory only when they are actually needed. If a required page is not in RAM, a page fault occurs, and the operating system brings the page from disk into memory.

These techniques are central to modern operating systems because they improve memory utilization, support large programs, allow better multiprogramming, and provide memory protection.

6. Likely University Questions

  1. Explain paged segmentation with address translation diagram.
  2. Differentiate between paging, segmentation, and paged segmentation.
  3. What is virtual memory? Explain its need and advantages.
  4. Explain logical address space and physical address space.
  5. What is demand paging? Explain page-fault handling.
  6. What is the role of valid-invalid bit in demand paging?
  7. Explain effective access time in demand paging.
  8. Why can frequent page faults degrade system performance?