l4


"/home/yossef/notes/Su/os2/l4.md"

path: Su/os2/l4.md

- **fileName**: l4
- **Created on**: 2026-01-04 17:04:09

1. What is Virtual Memory?

Normally, a program must be in the computer's RAM
(Physical Memory) to run. Virtual Memory is a trick
where the computer separates the memory the user sees
(Logical Memory) from the actual Physical Memory.

2. Demand Paging (Loading "On Demand")

Instead of loading a whole program into RAM, the system
only brings in a Page (a small piece of the program)
when it is actually needed.

3. The "Page Fault" (The "Oops" Moment)

If a program tries to use a page marked i, a
Page Fault happens. Here is how the Operating
System (OS) fixes it:

  1. The OS checks if the request was a mistake. If it's
    a real request, it just isn't in RAM yet.
  2. The OS finds an empty frame (a space) in the RAM.
  3. It pulls the page from the disk and puts it in that
    RAM space.
  4. It changes the bit from i to v.
  5. It restarts the instruction that failed so the
    program can continue.

4. Copy-on-Write (COW)

When a program creates a "child" process, they both
start by sharing the exact same pages in memory to
save space.

5. Page Replacement (Making Space)

What if the RAM is full and you need to bring in a
new page? You have to kick one out. This is called
Page Replacement.

6. Thrashing (The Computer is Overworked)

If a process does not have enough pages in RAM, it
will have page faults constantly.

7. Allocating Kernel Memory

The "Kernel" (the heart of the OS) needs memory
differently than your apps. It uses two main systems:

  1. Buddy System: It splits memory into chunks that
    are "powers of 2" (like 4KB, 8KB, 16KB). If it needs
    a small piece, it keeps splitting a large chunk in
    half until it fits.
  2. Slab Allocator: It creates "Caches" (pre-made
    storage) for specific types of data. This is very
    fast and prevents wasted space.

8. Real-World Examples

mind map

images/l4_mind_map.png|500

continue:./l5.md
before:./l3.md