Introduction
Swapping is a memory management technique where the kernel moves entire processes between main memory and a secondary storage device (swap device/swap space). When physical memory is scarce, the swapper (process 0) selects processes to swap out; when memory becomes available, it swaps processes back in.
Why Swapping?
- Early UNIX systems had limited physical memory (often a few MB).
- Multiple processes compete for memory — not all can fit simultaneously.
- Swapping allows the system to support more processes than can fit in memory at once.
- Idle or low-priority processes are temporarily moved to disk to make room for active processes.
Swap Device
- The swap device (or swap space) is a dedicated area on disk used for storing swapped-out processes.
- It can be a separate disk partition or a swap file.
- The swap space is organized as a contiguous sequence of disk blocks.
- Access to swap space is faster than regular file I/O because it bypasses the file system — data is read/written in raw blocks.
Key Data Structures
---
1. Swap Map
- The swap map tracks the allocation of blocks on the swap device.
- It is an array of entries, each recording:
| Field | Description |
|---|---|
| Address | Starting block number on the swap device |
| Number of blocks | Contiguous block count |
- The swap map uses a first-fit or best-fit algorithm to allocate contiguous disk blocks for a process.
- When a process is swapped out, the kernel allocates space from the swap map.
- When the process is swapped back in, the space is freed in the swap map.
Example Swap Map:
Index Address Length (blocks)
0 0 50 (Free)
1 50 30 (Used by Process A)
2 80 20 (Free)
3 100 40 (Used by Process B)
4 140 60 (Free)
---
2. Process Table Fields for Swapping
The process table entry contains fields relevant to swapping:
| Field | Description |
|---|---|
| p_stat | Process state (includes SRUN, SSLEEP, SIDL, SZOMB, SSTOP) |
| p_flag | Flags: SLOAD (in memory), SSWAP (being swapped), SLOCK (locked in memory) |
| p_swaddr | Address on the swap device where the process is stored |
| p_swsize | Size of the swapped image (in blocks) |
| p_time | Time the process has been in memory or on swap (used by swapper for selection) |
| p_pri | Priority (influences swap-out selection) |
Key Flags:
- SLOAD — Set when the process is loaded in memory; cleared when swapped out.
- SSWAP — Set during the actual swap operation.
- SLOCK — Set when the process must NOT be swapped (e.g., doing I/O with a user buffer).
---
3. Memory Map (per-process)
- Describes the process's memory regions (text, data, stack).
- Each region entry contains:
- Virtual address range
- Physical address or swap address
- Size
- Protection flags (read/write/execute)
How Swap Space is Managed
Physical Memory: Swap Device:
┌───────────────────┐ ┌───────────────────┐
│ Kernel │ │ Process B image │ ← swapped out
├───────────────────┤ ├───────────────────┤
│ Process A │ │ Free │
├───────────────────┤ ├───────────────────┤
│ Process C │ │ Process D image │ ← swapped out
├───────────────────┤ ├───────────────────┤
│ Free │ │ Free │
└───────────────────┘ └───────────────────┘
▲ ▲
└──── Swap In (B) ◄──────────────┘
└──── Swap Out (A) ─────────────►│
Summary
- Swapping moves entire processes between memory and the swap device.
- The swap map tracks free and used blocks on the swap device.
- The process table stores swap-related fields like
p_swaddr,p_flag, andp_time. - The SLOAD, SSWAP, and SLOCK flags control swapping behavior.
- Swap space is accessed as raw disk blocks for speed.