DEV Community

Cover image for Context Switch πŸ” in REAL life
Atul Kushwaha
Atul Kushwaha

Posted on

Context Switch πŸ” in REAL life

ONLY THE BANNER IS AI GENERATED, BLOG ISN'T

I love computer science because it is so practical. I recently experienced a real-life example of a context switch. It’s a great analogy to explain what a context switch is and why the time quantum in Round Robin scheduling shouldn't be too small.

Key Concepts

  • Context Switch: is defined as the procedure of Saving the state of the Old Process and Loading the state of the New Process

  • PCB (Process Control Block): A PCB is created for every individual process by the OS to track various parameters, such as the last executed statement (using the Program Counter), registers being used, and the data or files accessed by the process.

  • Degree of Multiprogramming: This refers to the number of processes present in the Ready State.

The Exam Analogy

I recently sat for the GATE 2026 exam. Imagine you are in the examination room. A common strategy we follow is this: you start solving a question but realize midway that it will take a huge chunk of your time. If you stayed on it, you would be blocked from answering other, easier questions btw, this is essentially the Convoy Effect seen in FCFS (First-Come, First-Served) scheduling (i.e Process with large burst time blocking processes with significantly lesser burst time)

Thinking

To avoid this, you leave the question half-solved and jump to another. This is essentially a context switch. Leaving the question is equivalent to saving its context (i.e., leaving the page in your scribble pad where you were working so the half-solved answer remains there).

In an OS, there are several reasons to perform a context switch:

  1. Higher Priority Process: Similar to realizing the current question is worth 1 mark, but there are 2-mark questions left.
  2. Interrupts: Similar to an invigilator coming to your desk for an attendance sheet signature. etc etc....

Saving and Loading Context

let's say now the next question you are solving is new, i.e you haven't read that question yet, so there is nothing you know about the question and hence nothing to retrieve from brain (except obv the concepts you'd use to solve it) btw this can also be seen as runtime environment

Runtime environment, manages essential tasks like memory allocation, dynamic linking, and resource access (e.g., files, networks, I/O devices), independent of the specific OS.

coming back, so a this action is same as switching to a new process in terms of OS, you save the context of the current process in its PCB.

Similarly, in an exam, when you switch to a new question, you are saving the context of your current work simply by leaving it as is. Since the next question is new, there is no need to "load" a previous context because it does not yet exist; instead, you initiate a new context by reading the question and writing down the given data.

However, if you switch back to a question you left earlier, it is equivalent to saving the current context and loading the context of the old, partially executed process in terms of OS. Just as the half-solved answer on your scribble pad is the "context" that needs to be loaded back into your brain to resume solving the earlier left question, the OS retrieves the saved state from the PCB to continue execution of earlier partially executed process.

Small Qt in Round Robbin

Panic GIF

Now, imagine there are only 10 minutes left. In a panic, you start a question, realize it’s too long, switch to another, realize the same, and switch again. You are now acting like a Round Robin algorithm with an extremely small time quantum. You end up spending more time switching between questions (the overhead) than actually solving them.

also, If the invigilator takes your paper away because time is up, that is a Preemptive Context Switch. You didn't choose to leave the question; the OS (Invigilator) forced you out because your 'Time Quantum' expired.

also If you’ve ever struggled to differentiate between these two:

Parallel vs. Concurrent Execution

  • Parallel Execution: This is only possible with multiple CPUs or multiple cores within a CPU. It would mean solving three questions simultaneously at the same time while its possible for CPU's with multiple cores not possible for us humans as we only have 1 brain (cpu)

  • Concurrent Execution: as human being's we only have a single brain(CPU), we can only work on a single task at a time, here i was not solving questions parallelly but concurrently i.e., solving multiple question in an interleaved fashion so i could cover maximum questions and making sure that my time utilization is maximum

which is the sole goal of concurrent programming i.e. executing multiple process concurrently in an interleaved fashion so that the CPU utilization can be maximized

Top comments (1)

Collapse
 
aarya_jha_f1a82f701d53f3e profile image
AARYA JHA

Very well!!!