Do Page Tables and PCBs Swap Out with a Process?
Let's understand the Process Swapping, Kernel Persistence, and Why Essential Structures Stay in RAM
Introduction
In modern operating systems, memory management is a critical component that ensures efficient utilization of physical memory. A common question arises: When a process is swapped out of main memory, are its page table and PCB (Process Control Block) also swapped out?
This article explores the mechanics of process swapping, the role of page tables, and the PCB.
Table of Contents
Process Swapping vs. Paging
Page Tables: Structure and Persistence
The PCB and Its Role
Kernel vs. User-Space Memory
Code Examples and System Analysis
Process Swapping Overview
Conclusion
1. Process Swapping vs. Paging
Swapping refers to moving an entire process from RAM to secondary storage (swap space) to free memory. Paging, however, involves moving individual pages of a process. Modern systems primarily use paging, but the terms are often used interchangeably.
Key Takeaway: Full process swapping is rare today; instead, individual pages are swapped out as needed.
2. Page Tables: Structure and Persistence
Page tables map virtual addresses to physical addresses. Each process has its hierarchical page table (e.g., in x86-64, a 4-level hierarchy).
Are Page Tables Swapped Out?
No. Page tables are kernel data structures stored in wired memory (non-swappable). They remain in RAM even if the process is swapped out.
Why? The kernel needs to track the process’s memory state. If page tables were swapped, the OS couldn’t restore the process efficiently.
Example: Linux Page Table Handling
typedef struct {
unsigned long pte; // here this is very simplified for this blog
} pte_t;
// Kernel function to free process pages (simplified)
void swap_out_process(struct task_struct *process) {
free_user_pages(process->mm); // Swaps user pages, not page tables
}
3. The PCB and Its Role
The PCB (Process Control Block) stores process metadata (e.g., state, registers, memory maps).
Is the PCB Swapped Out?
No. The PCB resides in kernel memory (part of the
task_struct
in Linux). The OS needs it to manage the process, even when swapped out.
Linux PCB Example:
struct task_struct {
volatile long state; // Process state
struct mm_struct *mm; // Memory management info
// same way for scheduler, file descriptors, etc.
};
4. Kernel vs. User-Space Memory
Kernel Space: Reserved for OS code/data. Never swapped.
User Space: Process-specific data (code, heap, stack). Swappable.
5. Code Examples and System Analysis
Example 1: Monitoring Swap Usage
Use vmstat
to observe swap activity:
vmstat 1
Output:
procs -----------memory---------- ---swap--
r b swpd free buff cache si so
0 0 1024 100000 2000 30000 0 0
swpd
: Total swap used.si/so
: Swap-ins/outs per second.
Example 2: Inspecting Process Memory
Use pmap
to view a process’s memory map:
pmap -X <PID>
Output:
Address Kbytes RSS Dirty Mode Mapping
...
00007f8e3b6e8000 18064 0 0 [swp]
[swp]
indicates swapped-out pages.
6. Process Swapping Overview
Explanation:
Page Tables and PCB stay in kernel memory during swapping.
Only user-space pages move to swap space.
7. Conclusion
Page Tables: Never swapped out; part of kernel memory.
PCB: Never swapped out; critical for process management.
User-Space Pages: Swapped as needed, but kernel structures persist.
Modern OSes prioritize efficiency by retaining essential structures in memory, ensuring swift process restoration and system stability.