Understanding Linux Context Switching: Concepts, Types, and Monitoring Tools
This article explains what Linux context switching is, details the information stored in a process control block, distinguishes between process, thread, and interrupt context switches, and shows how to monitor them using vmstat and pidstat commands.
Concept
Linux is a multitasking operating system that can run many tasks concurrently, but at any instant only as many tasks as there are CPU cores can execute. The CPU switches between tasks by saving and restoring each task's state, a operation known as a context switch. The saved state includes both user‑mode resources (virtual memory, stack, global variables) and kernel‑mode resources (kernel stack, registers).
What Information Is Stored in a Context
A process’s context is kept in a Process Control Block (PCB). Typical fields include:
Process state : ready, running, blocked, etc.
Program Counter (PC) : address of the next instruction.
Register contents : general‑purpose and special registers.
Memory management info : page tables, segment tables, limits.
Scheduling info : priority, queue position.
I/O state : open file descriptors, device usage.
PID : unique process identifier.
Parent/child information : parent PID and child list.
Timing info : start time, CPU time consumed.
In the Linux kernel these fields are stored in the task_struct structure.
Types of Context Switches
Process context switch : switching between different processes; the most common case.
Thread context switch : includes switches between threads of the same process and between threads of different processes; the cost is similar to a process switch.
Interrupt context switch : occurs when a hardware interrupt preempts the current task (e.g., a slow I/O operation), allowing the CPU to handle other work until the interrupt handler finishes.
Monitoring Tools
vmstat displays system‑wide context‑switch statistics. Example output:
vmstat 1
// ====================================
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 0 0 1944104 1026704 24011784 0 0 12 14 10 6 1 1 98 0 0
...Key columns: cs: number of context switches per second. in: number of interrupts per second. r: length of the run queue (runnable processes). b: number of blocked processes.
pidstat shows context‑switch data for individual processes. Example:
pidstat -w 5
// =======================================
Average: UID PID cswch/s nvcswch/s Command
Average: 0 12 0.60 0.00 ksoftirqd/0
Average: 0 13 62.48 0.00 rcu_sched
...In the output, cswch/s is the rate of voluntary context switches, while nvcswch/s is the rate of non‑voluntary switches.
Source: https://my.oschina.net/u/4526289/blog/4539592
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
