DevOps Interview Questions: What is a context switch and its impact on Linux.

In the last article, we talked about what is /proc file system and how you can see different information there. If you miss the article you can read it below. In this article, we are going to see what exactly is a context switch and what are its impact.

What is a context switch?

A context switch is a process of saving a process or thread’s state so that the CPU can execute another thread or process. Now there are many questions that come with it. It then loads the next process which has to be executed.

Why you need a context switch?

With context switching, you are able to make use of the CPU in a better way. Multiple processes can execute on the same CPU by context switching after a given interval.

What does it save?

It saves everything to define the process in the current state so that the process can be executed from the same state. Generally, these are the value of registers, variables, thread states, and other such information. Context switch saves everything that is defined in PCB [Process Control Block]. Read more about PCB below

Impact of context switches?

Context switches need some CPU cycles to save old processes and load new processes. This is normal. But when the number of context switches increases it means your CPU has to do a lot of work context switching as compared to before. A very high context switch can indicate that your CPU is wasting a lot of CPU cycles in just the loading and saving process.

How you can see a number of context switches?

You can see this detail in the proc file system. If you read the file status in any process directory, you will be able to see the context switches. Use the below command.

cat /proc/<process_id>/status

If you don’t know about the proc file system. You must read it now as it is one of the most asked interview questions. You can read below.

This was very basic of what is a context switch its uses and its complications. If you like the article please share and subscribe.

Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.