Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Swapping: Data Structures

Lesson 28 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

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:
FieldDescription
AddressStarting block number on the swap device
Number of blocksContiguous 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:

FieldDescription
p_statProcess state (includes SRUN, SSLEEP, SIDL, SZOMB, SSTOP)
p_flagFlags: SLOAD (in memory), SSWAP (being swapped), SLOCK (locked in memory)
p_swaddrAddress on the swap device where the process is stored
p_swsizeSize of the swapped image (in blocks)
p_timeTime the process has been in memory or on swap (used by swapper for selection)
p_priPriority (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, and p_time.
  • The SLOAD, SSWAP, and SLOCK flags control swapping behavior.
  • Swap space is accessed as raw disk blocks for speed.