How can kernels context-switch between processes?

Whenever the transaction between user and kernel mode is required, a context switch is not necessary; simply a mode transition is not a context switch. Because, depending on the operating system, a context switch may also take place at this time.

Let us see the major steps of Context Switching

  • The values of the CPU registers are generally saved in the process table of the process which was running before the clock interrupt occurred.

  • To run next the registers are loaded from the process and picked by the CPU scheduler.

Consider a multi-programmed single processor system, the context switches occur frequently so that all processes appear to run concurrently. If a process has more than one thread, the Operating System uses the context switching technique to schedule the threads so they appear to execute in parallel.

It is the case if threads are implemented at the kernel level. Threads can also be implemented at the user level in run-time libraries. Because, no thread scheduling is provided by the Operating System, and it is the responsibility of programmer to provide the CPU frequently enough in each thread so all threads in the process can make progress

Context switches among processes are very expensive. Before a process is switched its process control block (PCB) has to be saved by the operating system.

The PCB consists of the following information −

  • The process state.

  • The program counter, PC.

  • The values of the different registers.

  • The CPU scheduling information for the process.

  • Memory management information regarding the process.

  • Possible accounting information for this process.

  • I/O status information of the process.

Whenever the PCB of the currently executing process is saved by the operating system, then that loads the PCB of the next process that has to be run on the CPU. This is a heavy task and it takes more time.

Let us see the diagram below of how the context switching takes place between the processes −

Updated on: 01-Dec-2021

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started