16.1 Purposes of an Operating System (OS)

Bulk view disabled for Guests. View lessons individually.

Virtual Memory and Paging

1. What is Virtual Memory?

Virtual Memory is a feature of the OS that allows a computer to compensate for shortages of physical RAM by temporarily transferring pages of data from RAM to Secondary Storage (HDD/SSD).

Virtual Memory
(App View)
RAM
(Fast)
SSD/Disk
(Slow)

2. Paging vs. Segmentation

The OS manages memory in two distinct ways:

  • Paging: Memory is divided into fixed-size chunks called Pages. This is a physical division.
  • Segmentation: Memory is divided into variable-sized chunks called Segments based on logical parts of a program (e.g., a function or a data structure).

3. The Page Table & Page Faults

The OS keeps a Page Table to map Virtual Addresses to Physical Addresses. When an application tries to access a page that is not in the RAM:

  1. A Page Fault is triggered.
  2. The OS looks for the page on the Hard Drive.
  3. If the RAM is full, the OS must swap out an existing page to make room (using algorithms like Least Recently Used).
  4. The required page is loaded into RAM.
The Librarian Analogy:
Imagine a small desk (RAM) and a massive bookshelf (Hard Drive). You are the student (the CPU). If you need a book not on the desk, the Librarian (the OS) has to go find it. If the desk is full, they must take a book you haven't touched in a while and put it back on the shelf to make space.

4. Disk Thrashing

This occurs when the computer spends more time moving pages between the RAM and the Hard Drive than actually executing instructions. This happens when the RAM is too small for the active tasks, causing a massive performance drop.

⚠️ Exam Note: Paging Efficiency

In A2 exams, you are often asked why Paging is used. Key points: It allows programs to run even if they are larger than RAM, it prevents memory fragmentation, and it allows multiple programs to share the same physical RAM without seeing each other's data.