Your phone is playing a song, uploading a photo and showing this page at once. What keeps them from tripping over each other?

Right now your phone is playing a song, uploading a photo to WhatsApp, and showing you this page. Three apps, one phone, one chip, and none of them trips over another.

Something is keeping the peace. Here is what it does.

See it#

Open the phone up and look at it from the side. Your apps sit on top. The hardware sits at the bottom: the chip that runs everything (the CPU), memory, storage, the camera, the speaker, Wi-Fi. In between is a layer that every request passes through.

Here is what never happens. The song doesn't drive the speaker itself: it asks, and the middle layer does it. The game doesn't grab the CPU: it waits for its turn. And when it reaches into WhatsApp's memory, it's stopped.

Name it#

That middle layer has a name. An operating system (OS) is the software between your apps and your hardware. It shares the hardware among the apps and stops them from getting in each other's way.

You already use a few. Your phone runs Android or iOS. Your laptop runs Windows, macOS or Linux. Your game console, your smart TV and even the screen in a car run one too. Different names, the same job.

How it works#

Everything the phone did comes down to three jobs, plus the one part of the OS that does them.

Job 1: it shares the CPU#

Your phone has one CPU chip and dozens of apps that want it. The OS lets one app run for a moment, then the next, then the next. It switches so fast, hundreds of times a second, that they all seem to run at once. (Most phones have several cores, small CPUs on one chip, and each one takes turns the same way. How the switch happens is the Multitasking lesson.)

Job 2: it shares memory, and keeps it private#

Every app needs memory to hold what it's working on: your chats, the song that's playing, the level you're on. The OS gives each app its own space in memory and fences it off, so one app can't read another's. That's why a game you installed yesterday can't read your WhatsApp messages, even though both sit in the same memory chip.

Job 3: it runs the hardware for the apps, and guards it#

Apps never touch the camera, the storage or the Wi-Fi themselves. They ask, and the OS does the work. That request is a system call: opening a file, taking a photo and sending a message are all system calls. Because the OS does the work, "Save" works the same whether a photo goes to the phone's own storage or to an SD card: the app doesn't need to know which one, because the OS does.

Asking also gives the OS the chance to say no. When you see "Allow Instagram to use the camera?", that pop-up is the OS guarding the hardware. Until you tap Allow, Instagram can't switch the camera on.

The kernel: the part that does the jobs#

The home screen, the settings and the files app are part of the OS too, but they're its friendly outside: the parts you see and tap. The part that actually shares the CPU, fences off memory and drives the hardware is the kernel. It's loaded as the phone starts up and keeps running until it switches off, and every app runs on top of it.

The kernel is also the only part allowed to touch the hardware, and the chip itself enforces that:

  • Your apps run in user mode. There the chip refuses any instruction that reaches for the hardware or for another app's memory.
  • The kernel runs in kernel mode, where it can do anything.

A system call is the one door between the two. It takes four steps:

  1. The app asks with a special instruction, a trap.
  2. The chip switches to kernel mode.
  3. The kernel does the job.
  4. The app gets the answer back, in user mode.

Why it matters#

Every device that runs apps has an operating system: phones, laptops, the servers behind every website, ATMs, smart watches. Some must react within a fixed deadline every single time, like the computer that fires a car's airbag. Those run a real-time OS, built never to be late.

In interviews this is one of the most asked OS questions: "What is an operating system and what are its functions?" Interviewers use bigger words for the same things you just watched:

Interviews call itWhat you saw
Process managementsharing the CPU (job 1)
Memory managementseparate, private spaces (job 2)
Device and file managementapps ask, the OS drives the hardware (job 3)
Security and protectionthe blocked reach, the permission pop-up
User interfacethe home screen and settings: the outside around the kernel
User mode and kernel modeapps ask with a trap, the kernel does it

(A running app is called a process. What that means exactly is the Processes lesson.)

What to remember#

  • An operating system sits between your apps and the hardware. Android, iOS, Windows, macOS and Linux are all one.
  • It shares the CPU: apps take turns so fast they all seem to run at once.
  • It shares memory: each app gets its own private space.
  • Apps ask, the OS does: every request is a system call, from user mode into the kernel.
  • The part that does all this and is always running is the kernel.