How do computers juggle between processes & threads? You may've heard of "context switching", but what's actually happening under the hood?
When a program is being started, the Operating System simply tells the processor "execute this code". And "this code" (our app) doesn't necessarily call the OS back, it could simply crunch numbers indefinitely. There's nothing about "give the CPU back to the OS so that it could execute something else". So if the CPU doesn't see these instructions, how does it know to switch?
The CPU reads the code one instruction at a time. It stores "current line number" in one of the registers, and updates it after every instruction. But it can also listen to commands via mechanisms like interrupts. Interrupts tell the processor to stop what it's doing. They are often used to react to other devices. Like when we press something on the keyboard, or when a networking card receives data.
When you start a computer and the OS boots, it configures the CPU. One of the things that it configures is.. which OS function (well, instruction) to call if this or that interrupt happens.
So what we have is this:
- We can send some interrupt number to the CPU
- The CPU will call the pre-configured instruction somewhere in the OS code that correspond to some schedule() function.
- Since now OS has the control, it can save what instruction the CPU was just invoking, and instead of resuming it - point CPU to the instructions of another thread!
The OS keeps track of all the Threads/Processes, along with their "current line number" and other information like current call stack, state of registers, etc. This is our "context" that gets switched.
But who actually sends the interrupt? We need some hardware for this. In this case it's the CPU itself - it has a clock component (the usual Quartz clock similar to the one in wrist watches) that periodically sends electrical signals. These are our interrupts. And they happen all the time. During the production of quartz clocks they can be tuned how frequently to fire.
And so once the interrupt happens and CPU starts executing OS code, the OS determines which process or thread is important enough to resume.
PS: another point when OS can decide to switch is when returning from syscalls. But if the app doesn't make any syscalls, then the clock is the only option.
To get notifications about new posts, Watch this repository.
How do computers juggle between processes & threads? You may've heard of "context switching", but what's actually happening under the hood?
When a program is being started, the Operating System simply tells the processor "execute this code". And "this code" (our app) doesn't necessarily call the OS back, it could simply crunch numbers indefinitely. There's nothing about "give the CPU back to the OS so that it could execute something else". So if the CPU doesn't see these instructions, how does it know to switch?
The CPU reads the code one instruction at a time. It stores "current line number" in one of the registers, and updates it after every instruction. But it can also listen to commands via mechanisms like interrupts. Interrupts tell the processor to stop what it's doing. They are often used to react to other devices. Like when we press something on the keyboard, or when a networking card receives data.
When you start a computer and the OS boots, it configures the CPU. One of the things that it configures is.. which OS function (well, instruction) to call if this or that interrupt happens.
So what we have is this:
The OS keeps track of all the Threads/Processes, along with their "current line number" and other information like current call stack, state of registers, etc. This is our "context" that gets switched.
But who actually sends the interrupt? We need some hardware for this. In this case it's the CPU itself - it has a clock component (the usual Quartz clock similar to the one in wrist watches) that periodically sends electrical signals. These are our interrupts. And they happen all the time. During the production of quartz clocks they can be tuned how frequently to fire.
And so once the interrupt happens and CPU starts executing OS code, the OS determines which process or thread is important enough to resume.
PS: another point when OS can decide to switch is when returning from syscalls. But if the app doesn't make any syscalls, then the clock is the only option.
To get notifications about new posts, Watch this repository.