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:
- 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.
- Allocate swap space — find contiguous blocks on the swap device using the swap map.
- 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.
- Free physical memory — deallocate the memory frames used by the process.
- 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:
- 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.
- 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.
- Allocate physical memory — find free memory frames for the process.
- Read process image from swap — copy data from the swap device back into physical memory.
- Free swap space — return the swap blocks to the swap map's free list.
- 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.