Explain Like I'm Human 2: Operating Systems

Episode 2: Operating Systems!

Why you should care

You’ve probably heard of operating systems, fancy words like Windows, MacOS, iOS, Android and Linux. This blog post should hopefully help you understand why you should care about your Operating System. More importantly, it should convince you to always update your operating system when an update is released.

I use the words Operating System so many times, I’ll abbreviate it by OS.

Here are a few reasons you, no matter how technical you consider yourself to be, should care:

  • Your data could be at risk! If you don’t update your OS, you are putting yourself at risk of getting hacked and someone stealing your personal information.
  • Operating Systems are one of the most interesting programs you can learn about. They back almost every piece of modern technology!
    Wait, what doesn't have an operating system?
    Some single-purpose hardware does not require an OS. You’ll know more why as you read on, a lot of chips/IoT(internet of things) devices don’t have an OS because they are constrained on memory, and Operating Systems are typically large.
  • Wanna sound cool at parties? Nothing better than talking about Operating Systems

So what the heck is an Operating System?

An Operating System is a program whose job is to manage hardware and other programs (getting meta aren’t we 😆)

Let’s demonstrate with an example, you are probably reading this in a browser, like chrome (or hopefully firefox 🦊). The browser is a program.. and at the same time, you probably have other programs running, like messenger, spotify. You also can use your mouse and keyboard, seamlessly at the same time as the music is playing, and while the browser is running.

That is the power of an operating system! It allows you to do all the above when in reality you only have a limited number of CPUs.

The heck is a CPU?
It’s a computer’s brain. Each CPU can only execute one thing at a time. To the technical folks, it’s a little more complicated, I know - but fundamentally a CPU can run one thing at a time, all the magic happens at the OS.

In other words, your OS is your computer’s coordinator! It makes it all work

Ok okay, sounds cool, how does it work?

Glad you asked 😆. Lets first break down the two main parts of any computer:

  • Hardware: This is your mouse, keyboard, screen, speakers, etc
  • Software: This is all the programs in your computer that will probably need access to hardware:
    • Most need access to your screen, your keyboard and mouse, some need access to more (like your network hardware to access the internet)

An OS manages both by doing the following:

  • Limited Resource Management
  • Task Scheduling
  • Task Isolation

Resource Management

Your computer has limited resources, mostly your hardware resources like RAM, Disk space, and CPUs.

It’s your OS’s job to make sure those resources are managed and optimally utilized, and here’s how it does it.

Memory management

There is a whole post that could be written about “memory”, so we’ll do a quick summary of the two main types of memory and how they’re relevant to Operating Systems:

  • RAM: This memory is super fast to access! All your programs love this memory and want to utilize it. Two issues though, it’s limited and does not survive if you turn off your computer!
  • Disk: Super slow memory, but there is tons of it and it survives if you turn off your computer
    So I heard about this thing called SSD?
    Yeah… Disk should be Persisted Storage but that sounds a little scarier. SSD is an electronic (i.e no mechanical disk) alternative for disks. It’s faster than disks, but carries less memory and typically is more expensive… If you’re keen, search up NVRAM, it’s RAM that can survive being turned off, fancy right?

An OS needs to make sure that all your programs get to utilize those two resources, in the best way possible. That’s a little vague, so let’s try an example.

Imagine you’re running 100 Firefox windows, but are only looking at 2. Browsers are heavy programs and they need lots of memory and having 100 of them compete for the little RAM storage you have will end in disaster. So, the OS (browsers are also smart enough not to drain all your resources when they are not used) will put the idle windows into Disk so that the two active ones get to use the fast memory!

Ummmm well actually
Imma stop you right there, this is an extreme oversimplification. There’s a lot that goes in here, and attempting to go into detail is probably more damage than good. The moral of the story is it manages memory, move around the memory a program uses to disk when it’s not needed. If the reader needs more information, lookup Virtual Memory you’ll find painful details on how all this works!
A note on browsers
My full-time work is on a browser (Firefox 🦊) so naturally one of my next posts on this series will be on How Browsers Work, the funny thing, browsers have evolved so much nowadays, that they are very similar to operating systems…

Task Scheduling

This is probably the most mind-blowing part about OSs! I briefly mentioned earlier that you have a limited number of CPUs, but somehow your computer is running many programs at the same time… So how is that possible?

The answer is task scheduling. Here’s how it works: Let’s go back to the 100 firefox windows example, you don’t have 100 CPUs, so what’s happening there?

Whenever a new program wants to start, your OS would schedule a CPU to run it, however, and this is where the cool part comes.

After an interval of time passes (we’re talking milliseconds here) your operating system would stop the program! and it would let the CPU run a different program. This happens so often and so quickly, that our feeble human senses think that all programs are running at the same time when in fact, they’re running in sequence, but the OS is switching between them so quickly that it seems to us that they’re all running at the same time. Amazing right???

A bit more on scheduling

What I have above is one example of how operating systems schedule tasks, and it’s called Preemption.

Operating Systems nowadays do much much more. For example, each program would be assigned a priority, and the Operating System will run programs with a higher priority first.

Additionally, as we’ll discuss in the next section, programs will sometimes give away control on their own (typically when they need something from the operating system), and the Operating System will use that as an opportunity to schedule something else!

There are also fancy algorithms at play here to decide which program goes next - hope you’re as excited as I was when I first learned all of this 🤩

What's the right preemption time?
The short answer, it depends - each operating system is free to decide, Linux will base it off of your CPU but could be somewhere around 100 to thousands of times a second. There’s a tradeoff of course. The more times you stop a program, you take a cost, the cost of switching between programs is very expensive… So OS designers have to be mindful of picking the right values

Task Isolation

This is probably the most useful piece someone can know about Operating Systems

When the Operating System runs programs, it ensures that each run in their own space, with their own memory and can never access other programs without permission

In practice, this means that if you are running both messenger and spotify, spotify can’t steal messenger's data. This one of the most useful features of an OS. But that’s not all.

In addition to restricting access to other programs, operating systems restrict access to hardware. In short, only the operating system itself can directly interact with the hardware. If a program needs to talk to the hardware, it has to ask the OS to do it on its behalf, and the OS can say no.

Imagine if a program has full non-restricted access to your hardware… an evil program will be able completely to take over your computer!

So this takes us to the punchline, OS Engineers make mistakes, they introduce bugs, and sometimes, those bugs can be exploited to allow programs to bypass the isolation we just talked about.

Oftentimes, OS and Security Engineers would catch those bugs and release fixes. Those fixes are included in the dreaded System Update Available - so whenever you see one of those, please make sure you update right away!

Some very recent scary OS bugs

Honestly, the list would never end.. and that’s expected. Operating Systems engineers are people and people make mistakes, sometimes those vulnerabilities aren’t even mistakes, at the time the code was written it probably was totally fine, but programs evolve…

Umm not so human?

This post is a little less “human” than the last one, but I’m trying to find a balance of using analogies and getting the core message out… I’m still experimenting with this and welcome all feedback.

If you like it or even dislike it, let me know. Thanks for reading and I hope you gained some value from this ❤️