Introduction

Let’s refresh some basics first. As we know, everything we execute in a computer program boils down to an OS thread that is executed by the CPU. Each core in a CPU can execute a single thread. A program may have several types of tasks, such as doing some computation, waiting on some condition or blocking on an I/O, etc. So, all the tasks in a program are not CPU intensive.

For an instance when there is an I/O operation the thread gets blocked and the CPU time gets wasted. In order to avoid that the OS creates a new thread for another task and CPU switches to that, we call this context switching.

Introduction
Let’s refresh some basics first. As we know, everything we execute in a computer program boils down to an OS thread that is executed by the CPU. Each core in a CPU can execute a single thread. A program may have several types of tasks, such as doing some computation, waiting on some condition or blocking on an I/O, etc. So, all the tasks in a program are not CPU intensive.
For an instance when there is an I/O operation the thread gets blocked and the CPU time gets wasted. In order to avoid that the OS creates a new thread for another task and CPU switches to that, we call this context switching. […]