Tasks, scheduling and interrupts
Quick answer
The scheduler selects runnable tasks for available CPUs. Interrupts let hardware or software events request attention without requiring a user task to poll continuously.
A blocked task is not competing for CPU in the same way as a runnable task. High delay may come from waiting for I/O rather than executing slowly. Compare task state and CPU measurements over the same interval before attributing a problem to scheduling.
Prepare the practice environment
Run python3 lab-fixtures/kernel/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Only private files are used; optional examples require an authorized snapshot VM. Do not write kernel attributes, load modules, or change boot settings. Supplied records are fictional; read only the selected non-sensitive live fields. Review the lab guide.
Prerequisites
Review: Kernel mode and system call boundary.
How it works
Linux schedules threads as tasks; a process can contain multiple threads sharing an address space. A runnable task may be executing or waiting for a CPU; sleeping tasks wait for events. A context switch saves and restores execution state when a CPU changes tasks, while a mode switch can occur without changing tasks. Hardware interrupts and deferred work can consume CPU time without corresponding directly to application threads. Scheduler classes and policies differ, and fair scheduling implementation evolves with kernel versions. Use per-CPU observations and deltas of cumulative counters over the same interval, rather than assuming a single interrupt equals a context switch. The synthetic task list uses R (runnable), S (interruptible sleep) and D (uninterruptible sleep).
Try the examples
ps -o stat= -p "$$" | grep -Eq '^[A-Z]' && printf 'task state present\n'The state varies; only its presence is checked.
Expected stdout:
task state presentThis counts synthetic states, not live load.
Expected stdout:
runnable=2 sleeping=1 uninterruptible=1Two fictional samples two seconds apart distinguish both counters.
Expected stdout:
context switches/s=40; interrupts/s=25Common mistakes
Do not treat R as proof of continuous CPU execution or D as proof of a disk problem. Do not change scheduling policy to investigate a counter.
Practice
Classify each synthetic task and explain why switch and interrupt rates differ.
Related references
Study: ps. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tasks, scheduling and interrupts — Knowledge Check
Apply kernel concepts to read-only observations and synthetic models.
What can state R mean?
Must a mode switch change tasks?
What should be computed from two cumulative samples?
Does every interrupt cause a context switch?