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 In & Swapping Out

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

Introduction

The swapper (process 0, also called sched) is a kernel process responsible for moving processes between main memory and the swap device. It runs in an infinite loop, checking whether processes need to be swapped in or out.

---

Swapping Out — Moving a Process to Disk

When does swap-out occur?

  • The kernel needs memory for a new process (fork()).
  • The kernel needs to expand a process's memory (brk(), stack growth).
  • The swapper is trying to make room for a process to be swapped in.

Swap-Out Algorithm:

  1. Select a victim process — the swapper chooses a process to swap out based on:
  • The process must be in memory (SLOAD flag set).
  • The process must NOT be locked (SLOCK flag NOT set).
  • Preference for:
  • Sleeping processes over ready/running ones.
  • Processes that have been in memory the longest (p_time).
  • Processes with the lowest priority.
  • Among sleeping processes, choose the one sleeping the longest.
  • Among ready processes, choose the one with lowest priority and longest residence time.
  1. Allocate swap space — find contiguous blocks on the swap device using the swap map.
  1. Write process image to swap — copy the process's memory (data + stack + u-area) to the allocated swap blocks.
  • The text segment is usually NOT swapped — it can be reloaded from the executable file.
  • If the text segment is shared, it stays in memory as long as any process uses it.
  1. Free physical memory — deallocate the memory frames used by the process.
  1. Update process table:
  • Clear the SLOAD flag.
  • Set p_swaddr to the swap device address.
  • Set p_swsize to the size of the swapped image.

Swap-Out Selection Priority:

Priority for swap-out (highest to lowest):
1. Sleeping processes (longest sleep time first)
2. Stopped processes
3. Ready-to-run processes (lowest priority, longest in memory)
4. NEVER: Locked processes (SLOCK), zombie processes

---

Swapping In — Moving a Process to Memory

When does swap-in occur?

  • The swapper wakes up periodically (every few seconds).
  • It checks if any swapped-out processes are ready to run.
  • It tries to bring them back into memory.

Swap-In Algorithm:

  1. Select a process to swap in:
  • Must be in the "Ready to Run, Swapped" state.
  • Among eligible processes, choose the one that has been swapped out the longest (p_time).
  • Higher-priority processes are preferred.
  1. Check if enough memory is available:
  • If yes → proceed to step 3.
  • If no → swap out another process first (go to swap-out algorithm), then retry.
  1. Allocate physical memory — find free memory frames for the process.
  1. Read process image from swap — copy data from the swap device back into physical memory.
  1. Free swap space — return the swap blocks to the swap map's free list.
  1. Update process table:
  • Set the SLOAD flag.
  • Change state to "Ready to Run, In Memory".

---

The Swapper's Main Loop

swapper() {                          // Process 0
    loop forever {

        // Phase 1: Try to swap in
        for each process P where P is "Ready, Swapped" {
            select P with highest priority and longest swap time;
        }
        if (found a process P to swap in) {
            if (enough free memory) {
                swap_in(P);
            } else {
                // Phase 2: Swap out to make room
                select victim Q (sleeping longest, lowest priority);
                if (found victim Q) {
                    swap_out(Q);
                    swap_in(P);
                } else {
                    // No suitable victim — sleep and retry
                    sleep(event: memory_available);
                }
            }
        } else {
            // Nothing to swap in — sleep
            sleep(event: swap_needed);
        }
    }
}

Thrashing

  • Thrashing occurs when the system spends more time swapping than executing processes.
  • This happens when too many processes compete for limited memory.
  • The swapper continuously swaps processes in and out, but no process makes progress.
  • Solution: Reduce the number of active processes or add more memory.

Summary

  • Swap-out: Select victim → allocate swap → write to disk → free memory → update proc table.
  • Swap-in: Select candidate → check memory → allocate memory → read from disk → free swap → update proc table.
  • The swapper prefers to swap out sleeping, low-priority processes and swap in ready, high-priority ones.
  • Thrashing occurs when excessive swapping dominates CPU time.