Context Switching in Processors — Is there a thing called parallelism?

Harsimar Singh
4 min readSep 24, 2022

--

Imagine a scenario where you have time travelled and this is the era of 1903. Ford is coming up with its production line to launch the famous model T. All the designers are brainstorming and the company has its sight to produce a car that will potentially become USA citizens’ most preferred mode of transportation. Henry Ford, the inventor and the man of vision, is learning from his experience of creating several models starting from model A and constantly upgrading thinking all about hitting the apex point during this flow.

Ford was a pioneer in establishing an assembly pipeline which created actual parallelism and derived the power of parallelism. Now imagine Henry Ford had to face a scenario where his employees are on sick leave and he now has only one person but multiple pipelines. This single person is handling all the pipelines, we can predict the amount of work but we will pull our zoom lens and focus on what is going on inside the factory processes.

Now when this superhero guy of Henry Ford, who goes by the name of Klark Cent, gets his first car order, he starts assembling it. He analyses the design and the work starts. While he finishes up the outer structure of the first car, then comes the second order. Now Ford says we need to do this before everyone else as it is ordered by Elon Musk ( Guess who Elon Musk is, hint — he is Martian ). Klark makes a note of the first car’s current condition, his work done, and the tools required in his notebook and he begins working on Elon Musk's car. While he was doing the second car’s body paint, the machine broke and will require third-party service. He is stuck now and can’t proceed further with working on this machine.

He then looks up his book, gets the status of the first car and moves back to assembling this first car’s engine. Till now we have covered quite a lot of concepts here. Hence these are the analogies and their interpretations —

  1. Klark here is our single core-single thread processor which means he can process only one instruction at a single instance of time. He can do multiple tasks if assigned but at one instance time, he can either fix the car paint or work on wheels (unless he is in the quantum realm).
  2. The cars in the assembly line are the jobs waiting in the queue that are ready to be submitted. Be it clicking on an icon to open the window or playing a song in the windows media player with fancy visualisation (The era before Apple Music), these jobs arrive one at a time and are submitted to the processor to do their job by the scheduler. A job is just a program which is written in C++ or Java and converted to CPU instruction set by the compiler. This scheduler can be of two types — Long-term scheduler or short-term scheduler.
  3. Tesla car is now a higher priority job. That means Klark must focus on this task because it can’t wait in the queue else there can be consequences of cancelling the order. e.g. In the language of computers, we have user processes and OS/System processes which are categorised according to their priority. One such system process is SIGKILL (I interpret it as “signal to kill” ) and when a process initiates this, every other task which is running on the process must be added to the waiting queue. These processes will be picked up later. This queue is maintained in the form of a linked list in the CPU registers.
  4. The book in our example is called process control block (PCB) which stores the current state of the process and all its data. This information is analogous to the condition of the first car’s state before switching to a priority job. This PCB holds information related to CPU registers’ value and state, current File or Stack pointer location, instruction pointer etc.
  5. When our assembly line broke down, fixing the assembly line is analogous to I/O operations. I/O operations are read and write operations performed on secondary memory to fetch stored data. There is an I/O processing unit which handles this operation.

When Klark switches back to his first card after getting the status from his PCB, this is called context switching. This is driven by OS and not by user programs for the obvious reasons.

He could switch because he had the liberty to move back and forth on multiple assembly lines. This is called preemption which indicates we can assign processing time to a different task. This context switching gives the user the feel that his all programs/ tasks are running at once. Here we have not introduced the concept of paging to give that real gist of multitasking.

We assumed that Klark had all the tools to work on the assembly line tasks. What if he doesn’t have the table’s capacity to keep all his work tools on the desk he is working on? He has to fetch the tools and work on a particular project unit. This concept is called paging where a piece of code is fetched in pages from secondary disk to RAM.

This was the concept of context switching and an additional note is this happens very fast. Questions to ask here -

a. What if our registers are flushed?

b. What if the scheduler has no preemption?

c. What will be the consequences if a priority job is eating up a lot of resources?

d. What happens if user processes handled the context switching?

Hit up like if you love the analogy!

--

--

Harsimar Singh
Harsimar Singh

Written by Harsimar Singh

Co-Founder VAAR Lab, Alumni IIT Ropar ( 2018-2020 ) M.Tech CSE. Loves breaking complex things into granular objects like Rutherford did.

No responses yet