- 2071 Bhadra · 2+3+5 marks
Context switching, 5-state model, MLFQ
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.
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. |
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
+-----+ 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
| 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.
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Real-time scheduling; two-processor numerical · Operating System, chapter 2
- FCFS, SJF, Priority, RR Gantt; max AWT · Operating System, chapter 2
- Process vs thread; context switching · Operating System, chapter 2
- HRRN and RR scheduling (q=4) · Operating System, chapter 2
- Deadlock conditions; Banker's with 4 processes · Operating System, chapter 7
- Rate Monotonic real-time schedule · Operating System, chapter 2
- Priority and SJF scheduling with P4 and P5 · Embedded System, chapter 6 · asked 2×
- Preemptive priority scheduling with P4 (6 ms) · Embedded System, chapter 6 · asked 2×
- Priority scheduling (7, 8, 5 ms) with P4 · Embedded System, chapter 6 · asked 2×
- Preemptive priority scheduling (4, 10, 5 ms) · Embedded System, chapter 6 · asked 2×