- Asked 3/19
- 2070 Bhadra · 3 marks
- 2074 Bhadra · 4 marks
- 2069 Bhadra · 4 marks
Context switching in RTOS
Short answer
The kernel saves the current task’s registers, PC, and SP into its TCB, then runs the scheduler to select the next ready task and restores its saved context, allowing it to resume transparently. The process repeats at each task switch.
Describe the context switching process in detail (with suitable diagram).
Context switching is the process by which the RTOS kernel stops the running task, saves its state (context), and loads the saved state of another task so that the CPU can run it. The context consists of the program counter, CPU registers, status register, stack pointer and the task's stack and task control block (TCB) information.
When it happens
- A higher-priority task becomes ready (pre-emption).
- The running task blocks (waits for an event, delay or semaphore) or finishes.
- The time slice expires (round-robin).
- An interrupt makes a task ready.
Steps
- An event (interrupt, timer tick, system call) occurs; the CPU saves the PC and flags and enters the kernel.
- The kernel saves the context of the current task (registers, PC, SP) into its TCB/stack, and sets its state to Ready or Blocked.
- The scheduler selects the next task to run (the highest-priority ready task).
- The kernel loads (restores) the context of the selected task from its TCB: registers, SP and PC.
- The new task's state becomes Running, and execution resumes where that task was last stopped.
Task A running
| interrupt / event
v
save A's context -> TCB(A)
|
scheduler picks B
|
load B's context <- TCB(B)
v
Task B running
Context switching is pure overhead (no useful work is done), so an RTOS keeps the context-switch time short and bounded.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- RTOS and its architecture · Embedded System, chapter 6 · asked 2×
- Process vs thread; context switching · Operating System, chapter 2
- Context switching, 5-state model, MLFQ · Operating System, chapter 2
- Context switching; distributed vs network OS · Distributed System, chapter 3