Difference Between Voluntary And Involuntary Context Switches

So I assume that you know what context switching is. Suppose you are running an OS in a dual core processor. And at some point, your application is running four threads. Operating system kernel needs to divide the CPU time among them. You are running four threads in two cores after all. This is the job of process scheduler which runs as a function in kernel. When invoked, it will look into a queue & select next thread which will get CPU time. This switching between threads can happen in two ways.

A thread can give up control of CPU as it will generally wait on I/O, user input. Or it might be sleeping. So another thread can be selected to start running on the same CPU core. This is called voluntary context switch.
But sometimes the thread is doing some CPU intensive task & runs out of CPU time scheduled for it. Then process scheduler will take the thread out & add it back to the queue. It will then put next thread on the CPU. This is called involuntary context switch.
Hope this clarifies the difference in simple terms. I might write some more on involuntary context switches later.

Leave a Comment