Contents

How Are We Able to Run Multiple Apps at Once?

Hello everyone, yeah its been one week since my last post, as expected.

Interview

So today’s question is How are we able to run multiple apps at once in our computer? You may say, just open whatever you want and be done with it. That’s true, but how?. Let me expand the question, suppose we are in early 2000s, you just got your single core Pentium 4 computer with windows XP on it. First thing you want to do is, listen songs from the CD that I gave, while texting with your facebook friends and playing MMO RPG , all at once… How can a single execution unit can run all applications at same time, cause you only got one core right? One application should be running continuously its code to run properly. You can’t say to an application to stop execution for a sec and let other application run?

Let’s stop with interview session. What I generally thought is If we wrote a infinite loop in C and execute it, we can stall the CPU cause, its stuck in running our loop. Pretty evil thought if I say so myself. But that’s not the case, we are able to run other apps while my C code is running.

So the thing that makes this Multitasking work is none other than our Operating System.

The OS is like a big machine, it take our programs (a.k.a tasks/processes) and runs them at same time*. There is an small asterisk there, cause of-course a single core can’t run multiple tasks at once, that’s physically impossible. So the OS creates an illusion that tasks are running at same time by switching the tasks for CPU to execute, that switching is so fast that we aren’t able to see that everything is running one after another. Nifty right. Just like by showing 24 images per second our brain thinks that we are watching a movie. Dumb Human Brain. Take some time to digest this.

Now once again another question arises…

How does OS work?

If OS is running continuously, we only got single core, how does other programs run, OS will take all the execution time from the CPU. OS is a also a program it also needs continuous running right? The answer is Yes and No, if you think closely We only need programs to switch at regular fast intervals for CPU to execute, We only need our OS to kick in at certain interval of time and switch the task , and do nothing till another certain interval of time is passed, and kick in again, and tell the CPU to execute another task.

Do you see a pattern here OS has to stop the current execution and take the control of CPU and tell to go and run task at other place in memory. You need a mechanism/system to stop the current code execution and run some other piece of code in some fixed time intervals.

To my ECE folks, Does this ring a bell about some concept in micro-controllers subject? If you are not from ECE, and if you are one and didn’t attended the class that day, it is Timer Interrupts concept in Micro Controllers, its a piece of hardware which counts till some time that we say and issues an interrupt to the CPU to stop all tasks and go to a place that is pre-specified in memory and execute from there. Yeah hardware to the rescue. Are you able to connect the points now?

If your brain didn’t give up yet, let’s summarise everything.

Summary

Operating System is a piece of code that runs at particular time intervals with help of timer interrupts to stop the current execution of process and CPU then point the CPU to go execute the process from other location.

This is what Context Switching is all about and its the backbone of multitasking Operating Systems.

This is just a high level explanation of how multitasking works in OS, there are lot of algorithms called scheduler algorithms to determine how much time should a programs should run before OS switches it out with other program and what all things to be saved before switching out programs cause we don’t want to lose the current state of program. It would be really bad if result of a program is changing every nanosecond cause we forgot to save the previous state of programs.

I hope i was successful in giving a small glimpse of how OS does multitasking and how complex yet simple it can be at same time.

Extras

state of program normally contains registers and stack, i could go forever and haywire if i sit and tell whole working so here are some resources you can follow on:

  • cpu land goes to full detail how computers work.
  • Chat GPT (of course).
  • xv6 OS text book from MIT
  • And thanks to stackoverflow

Fun fact: I am editing this blog from the cafeteria of a company that I am here to give interview for, hope it goes well.