---
title: "Context switching, 5-state model, MLFQ"
subject: "Operating System"
chapter: 2
key: "semester_6/operating_system:context-switch-5state-mlfq"
source: https://www.bctengineer.com/q/semester_6/operating_system/context-switch-5state-mlfq
site: BCT Engineering
---

# Context switching, 5-state model, MLFQ

Operating System · Chapter 2: Process Management

- Asked in: 2071 Bhadra, 2+3+5 marks
- Video: [Context Switch in Operating System in Hindi  Lec-9](https://www.youtube.com/watch?v=PgM6sykp8mk) — Zeenat Hasan Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/operating_system/context-switch-5state-mlfq
- Whole chapter: https://www.bctengineer.com/md/semester_6/operating_system/chapter-2

## Question

Define context switching. Discuss the 5-state model of process. Schedule the following set of processes according to multilevel feedback queue scheduling algorithm and compute AWT and ATAT.
| Process | P1 | P2 | P3 | P4 |
|---|---|---|---|---|
| Arrival Time | 0 | 12 | 25 | 32 |
| CPU Burst (ms) | 25 | 18 | 4 | 10 |
Assume that there are three ready queues Q1, Q2 and Q3. The CPU time slice for Q1 and Q2 is 5 ms and 10 ms respectively and processes are scheduled on FCFS basis in Q3.

## Short answer

Context switching is the CPU switching between processes by saving and loading their contexts. The 5-state model includes New, Ready, Running, Blocked, and Exit states. For the MLFQ schedule, average waiting time is 13.5 ms and average turnaround time is 27.75 ms.

## Answer

#### Context switching

**Context switching** is the act of the CPU switching from one process (or thread) to another. The OS saves the context (program counter, CPU registers, stack pointer, memory-management information) of the current process in its PCB and loads the saved context of the next process from its PCB. It is pure overhead (no useful work is done during the switch) and its time depends on hardware speed, number of registers and memory-management support. It occurs on timer interrupts, I/O requests, and higher-priority arrivals.

#### Five-state model of a process

```text
 +-----+ admit +-------+ dispatch +---------+ exit +------+
 | New |------>| Ready |--------->| Running |----->| Exit |
 +-----+       +-------+<---------+---------+      +------+
                  ^      time-out      |
                  |                    | wait for event
                  |     +---------+    |
                  +-----| Blocked |<---+
                 event  +---------+
```

- **New**: process is being created (PCB is built).
- **Ready**: loaded in memory and waiting for the CPU.
- **Running**: instructions are being executed on the CPU.
- **Blocked (waiting)**: waiting for an event such as I/O completion.
- **Terminated (exit)**: finished execution; resources are released.

Transitions: New→Ready (admit), Ready→Running (dispatch), Running→Ready (time-out/preemption), Running→Blocked (waits for event), Blocked→Ready (event occurs), Running→Terminated (exit).

#### Multilevel feedback queue scheduling

Rules assumed:
- Q1 (quantum 5 ms) has highest priority, Q2 (quantum 10 ms) next, Q3 is FCFS.
- A new process enters Q1. If it does not finish in its quantum, it moves down one queue.
- A lower queue runs only when all higher queues are empty; a new arrival in a higher queue preempts the running process, which goes to the tail of its own queue.

Trace:
- 0-5: P1 in Q1 (20 left) → moves to Q2.
- 5-12: P1 in Q2 for 7 ms (13 left); at 12 P2 arrives in Q1 and preempts P1.
- 12-17: P2 in Q1 for 5 ms (13 left) → moves to Q2. Q2 order: P1, P2.
- 17-25: P1 in Q2 for 8 ms (5 left); P3 arrives at 25 and preempts.
- 25-29: P3 (4 ms) finishes in Q1.
- 29-32: P2 runs in Q2 for 3 ms (10 left); P4 arrives at 32 and preempts.
- 32-37: P4 in Q1 for 5 ms (5 left) → Q2. Q2 order: P1, P2, P4.
- 37-42: P1 finishes (5 ms). 42-52: P2 finishes (10 ms). 52-57: P4 finishes (5 ms).

**Gantt chart**
```text
| P1 | P2 | P1 | P3 | P2 | P4 | P1 | P2 | P4 |
0    12   17   25   29   32   37   42   52   57
```

| Process | Arrival | Burst | Completion | Turnaround | Waiting |
|---|---|---|---|---|---|
| P1 | 0 | 25 | 42 | 42 | 17 |
| P2 | 12 | 18 | 52 | 40 | 22 |
| P3 | 25 | 4 | 29 | 4 | 0 |
| P4 | 32 | 10 | 57 | 25 | 15 |

Average waiting time = 54 / 4 = **13.5 ms**

Average turnaround time = 111 / 4 = **27.75 ms**

**Answer: AWT = 13.5 ms, ATAT = 27.75 ms.**

## Check yourself

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

1. What is context switching?
   A. Saving the context of the current process in its PCB and loading the saved context of the next process
   B. Changing the priority of a process
   C. Copying a process to disk permanently
   D. Switching the user between two accounts
   Answer: A. Context switching saves the program counter, registers and memory information of one process and restores those of another; it is pure overhead. (Key point: Context switching)

2. In the five-state model, which transition occurs when a running process's time slice expires?
   A. Running to Blocked
   B. Running to Ready
   C. Blocked to Running
   D. Ready to Terminated
   Answer: B. Time-out or preemption moves the process from Running back to Ready; Blocked is only for waiting on an event. (Key point: Five-state transitions)

3. In the MLFQ used here, what happens when a process does not finish within its quantum in Q1?
   A. It is terminated
   B. It moves down to Q2
   C. It stays in Q1 and restarts
   D. It moves to Q3 directly
   Answer: B. A process that uses up its quantum drops one queue lower; a lower queue runs only when higher queues are empty. (Key point: MLFQ rules)

4. What are the average waiting and turnaround times found?
   A. AWT 13.5 ms, ATAT 27.75 ms
   B. AWT 27.75 ms, ATAT 13.5 ms
   C. AWT 17 ms, ATAT 42 ms
   D. AWT 54 ms, ATAT 111 ms
   Answer: A. Total waiting 54 and total turnaround 111 over 4 processes give 13.5 and 27.75 ms. (Key point: MLFQ results)

## Related in Operating System

- [Real-time scheduling; two-processor numerical](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arealtime-two-processor-fcfs-sjf-priority-rr) — Operating System, chapter 2
- [FCFS, SJF, Priority, RR Gantt; max AWT](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Ascheduling-gantt-fcfs-sjf-priority-rr-q2) — Operating System, chapter 2
- [Process vs thread; context switching](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Athread-context-switching) — Operating System, chapter 2
- [HRRN and RR scheduling (q=4)](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Ahrrn-rr-q4-awt-att) — Operating System, chapter 2
- [Deadlock conditions; Banker's with 4 processes](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Afour-conditions-banker-p1-p4) — Operating System, chapter 7
- [Rate Monotonic real-time schedule](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Arate-monotonic-schedule) — Operating System, chapter 2

## Related in other subjects

- [Priority and SJF scheduling with P4 and P5](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-and-sjf-5-process) — Embedded System, chapter 6, asked 2×
- [Preemptive priority scheduling with P4 (6 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-10-5-7-p4) — Embedded System, chapter 6, asked 2×
- [Priority scheduling (7, 8, 5 ms) with P4](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-7-8-5-p4) — Embedded System, chapter 6, asked 2×
- [Preemptive priority scheduling (4, 10, 5 ms)](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Asched-priority-4-10-5-p4) — Embedded System, chapter 6, asked 2×
