---
title: "Context switching in RTOS"
subject: "Embedded System"
chapter: 6
key: "semester_6/embedded_system:context-switching"
source: https://www.bctengineer.com/q/semester_6/embedded_system/context-switching
site: BCT Engineering
---

# Context switching in RTOS

Embedded System · Chapter 6: RTOS

- Asked in: 2070 Bhadra, 3 marks; 2074 Bhadra, 4 marks; 2069 Bhadra, 4 marks (asked 3 times)
- Video: [Context switching | ES | Embedded Systems | Lec-30 |  Bhanu Priya](https://www.youtube.com/watch?v=IC6bsDRAUUM) — Education 4u
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/embedded_system/context-switching
- Whole chapter: https://www.bctengineer.com/md/semester_6/embedded_system/chapter-6

## Question

Describe the context switching process in detail (with suitable diagram).

## 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.

## Answer

**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
1. An event (interrupt, timer tick, system call) occurs; the CPU saves the PC and flags and enters the kernel.
2. The kernel **saves the context** of the current task (registers, PC, SP) into its TCB/stack, and sets its state to Ready or Blocked.
3. The **scheduler** selects the next task to run (the highest-priority ready task).
4. The kernel **loads (restores) the context** of the selected task from its TCB: registers, SP and PC.
5. The new task's state becomes Running, and execution resumes where that task was last stopped.

```text
 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.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does the context of a task consist of?
   A. Only the task's name
   B. PC, CPU registers, status register, stack pointer and TCB information
   C. Only the priority
   D. The whole memory
   Answer: B. Context switching saves this state and loads another task's. (Key point: Definition and context)

2. Which event can cause a context switch?
   A. Only a reset
   B. Only power failure
   C. Only task completion
   D. A higher-priority task becomes ready, task blocks, time slice expires
   Answer: D. Pre-emption, blocking, time-slice expiry and interrupts all trigger switches. (Key point: Triggers)

3. What is the correct order?
   A. Reset registers, then pick next
   B. Load next context, then save current
   C. Save current context into TCB, scheduler picks next, load its context, run it
   D. Scheduler runs after loading
   Answer: C. The kernel saves, schedules, then restores. (Key point: Order of steps)

4. Why do RTOS keep context-switch time short and bounded?
   A. It is pure overhead with no useful work
   B. It performs the task's work
   C. It increases memory
   D. It is only for debugging
   Answer: A. Switching time is lost CPU time. (Key point: Overhead)

## Related in Embedded System

- [RTOS and its architecture](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Artos-architecture) — Embedded System, chapter 6, asked 2×

## Related in other subjects

- [Process vs thread; context switching](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Athread-context-switching) — Operating System, chapter 2
- [Context switching, 5-state model, MLFQ](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Acontext-switch-5state-mlfq) — Operating System, chapter 2
- [Context switching; distributed vs network OS](https://www.bctengineer.com/question.md?key=semester_7%2Fdistributed_system%3Acontext-switch-dos) — Distributed System, chapter 3
