A context switch happens when a running process gets interrupted by the operating system, in order to run another process.

Literally speaking, the operating system uses a process control block (PCB), which contains information about running processes. Typically, memory address spaces, program counter, and stack information are present on this block.

When one process is interrupted by another, the PCB for the first process is moved off the hardware cache, and replaced by the PCB for the second one. This finishes the context switch, and the new ‘running’ process can send requests to the operating system.

Now, onto whether it is good for you? The answer is NO!
This is an expensive operation, for two reasons:

  1. The one time swap of the process control block is relatively expensive
  2. Continuously cycling through processes moves PCB’s off the super fast hardware cache to slow read/write memory, or even the super slow disk.

More to come!

Comment and share

Today, I shall attempt to shed some light on why computers seem to do so many things at once, and the illusion of parallel processing on a single core.

It is easy to see examples of this in everyday life. Our computers, and phones have multiple apps that run simultaneously. I can check my email in Outlook while editing an image in Photoshop. But if you really think about it, is this really happening simultaneously?

A few years ago, most computers had only one processor core. It is changing now, and as of 2016, some mobile phones have quad (4) core capabilities. However, back in the day, it was still possible to run multiple programs at once, and experience a multi-tasking environment. This is made possible by having some kind of a process scheduler.

Let me provide an analogy. We are operating a restaurant that has a lot of chefs, but only one waiter. However, he is incredibly fast, and has a notebook that he writes orders on. One way of operating this restaurant might be to send the waiter to a table, wait for the customers to decide their order for a particular course, and then submit their order to the chef, and then wait by them as they ate that course, until they are ready to order the next course. However, this seems very inefficient, especially as several customers are waiting to submit their order.

So, what can we do instead? We can have our waiter carry an order for a single course, bring it back to the customer, and move on to another customer. The waiter opens another page in his notebook in anticipation for the next customer’s order. Then, when the first customer is finally ready to order the next course, the waiter goes back to them.

This, in computer parlance, is the essence of process scheduling. Programs are composed of multiple instructions (orders), interspersed with periods of time when they are just waiting for data/signals. The scheduler realizes this, and re-allocates memory (similar to the waiter writing new instructions on a different page) to another process in the priority queue while waiting for the first program to provide a signal that it is ready to send new instructions. By doing this, we are able to acheive the illusion of multi-tasking!

Comment and share

  • page 1 of 1

Pranav

Software Engineer in SF. I enjoy learning about new technologies, and love to organize after hours hand on sessions to teach others. I am also an avid bicyclist, and enjoy biking up to Marin on weekends.


Full Stack Engineer at Pariveda Solutions