Table of content

  • OS

Since you want understanding rather than a raw copy, I’ll correct a few points and explain them with intuition.

Chapter 9: Main Memory (corrected + explained)

Daily life intuition

Think of your computer like a study table.

  • CPU → your brain
  • RAM (Main memory) → the table where books are open
  • Hard disk / SSD → bookshelf
  • OS → the person organizing the table
  • Process → a book currently being studied

The table is small, so the OS decides what stays on it and what goes back to the shelf.


Why do we need memory management?

Multiple programs run at the same time:

  • Browser
  • VS Code
  • Music player
  • Terminal

All need RAM.

OS must:

  1. Allocate memory
  2. Protect memory
  3. Use memory efficiently

Memory allocation methods

1. Bare Machine

Idea: Only one program occupies memory.

RAM
+-------------+
| Program A   |
+-------------+

Problems:

  • Only one process
  • CPU wastes time

2. Fixed Partitioning

RAM divided into fixed blocks.

RAM
+-------------+
| Partition 1 |
+-------------+
| Partition 2 |
+-------------+
| Partition 3 |
+-------------+

Problem:

Internal Fragmentation

Example:

Partition = 100 MB

Program needs = 70 MB

Unused:

100−70=30 MB

30 MB gets wasted.


3. Dynamic Partitioning

Memory created according to process size.

RAM
+-------------+
| Process A   |
+-------------+
| Process B   |
+-------------+

Advantage:

  • Less waste

Problem:

External Fragmentation

Small free spaces become scattered.


Protection

Imagine your exam answer sheet.

You do not want another student writing on it.

Similarly:

Process A should not modify Process B memory.

Also:

User programs should not modify OS memory.


Base Register and Limit Register

Base Register

Starting memory location.

Limit Register

Maximum size allowed.

Example:

Base = 3000

Limit = 1000

Allowed addresses:

3000 → 3999

If process tries:

4500

OS blocks it.


Address concepts

This is where many students get confused.

Logical Address

Address generated by CPU.

Example:

100
200
300

CPU thinks:

“Give me address 100”


Physical Address

Actual RAM location.

Example:

5100
5200
5300

MMU (Memory Management Unit)

Acts like a translator.

CPU
 ↓
Logical address
 ↓
MMU
 ↓
Physical address
 ↓
RAM

Example:

CPU says:

100

MMU converts:

5100

TLB (Translation Lookaside Buffer)

Your notes said:

TLB stores recent page translations

Correct.

Daily life:

Suppose you repeatedly visit the same pages in a textbook.

Instead of finding page numbers every time, you place sticky notes.

TLB works similarly.

Logical page
↓
TLB checks first
↓
If found → fast
Else → Page Table

Context Switching

CPU can work on only one process at a moment.

OS switches between them rapidly.

Example:

Chrome
↓
VS Code
↓
Music Player
↓
Chrome

Steps:

  1. Save current process state in PCB
  2. Load next process state
  3. Continue execution

Swapping

RAM is small.

Move less-used process to disk.

RAM
↕
Disk

Example:

You move books from table to shelf.


Virtual Memory

Students usually memorize this and forget it.

Intuition:

Program thinks:

“I have huge memory.”

Reality:

RAM may be smaller.

OS creates an illusion.

Example:

Program needs:

16 GB

RAM available:

8 GB

OS temporarily keeps some parts on disk.


Paging

Memory divided into equal-size blocks.

Program side:

Pages

RAM side:

Frames

Example:

Program:

Page1
Page2
Page3

RAM:

Frame1
Frame2
Frame3

Mapping:

Page1 → Frame3
Page2 → Frame1
Page3 → Frame2

Advantage:

  • Removes external fragmentation

Demand Paging

Load only needed pages.

Daily life:

Instead of placing all books on your table:

  • Keep only current book
  • Bring others when needed

Page Fault

If required page is absent in RAM:

CPU requests page
↓
Not found
↓
OS loads from disk
↓
Continue execution

This delay is called a page fault.


Corrected flow from your notes

Your note had some mixed ordering.

Correct flow:

Source code (.cpp)

↓

Compiler

↓

Object code

↓

Linker

↓

Executable file

↓

Loader

↓

RAM

↓

CPU executes instructions

CPU never executes source code directly.


Memory chapter in one picture

CPU
 ↓
Logical Address
 ↓
MMU
 ↓
TLB check
 ↓
Page Table
 ↓
Physical Address
 ↓
RAM

This mental picture alone covers much of the chapter.