Daylila
How computers actually work

Lesson 10 of 13

The operating system: who's in charge

Explain what an operating system does — sharing the one processor, the memory, and the devices among many programs — and why no app talks to the hardware directly.

01 · Learn · the idea

Look at your screen right now. A browser is open. Music might be playing. Messages sit in a window, a clock ticks in the corner, a download crawls along. A dozen programs, all alive at once. And yet item 7 told you the truth underneath: there is one processor, doing one instruction at a time. One finger walking one list.

So how does one finger keep a dozen programs going? It can’t, really — not all at once. Something has to stand between the programs and the machine and decide, moment by moment, whose turn it is. That something is the operating system — Windows, macOS, Linux, Android, iOS. The referee you never see.

The puzzle: one machine, many programs

A program, you now know, is a list of instructions the processor runs (items 7 and 8). Open three apps and you have three lists, all wanting the same single processor — and the same memory, screen, and disk. One of each, many programs reaching.

If they all grabbed at once, it would be chaos. Two programs writing to the same patch of memory would scribble over each other; a crash in one would take the whole machine down. You need a manager whose whole job is to hand out the shared hardware fairly and keep the programs from wrecking each other. That manager is the operating system.

Sharing the processor: rapid turn-taking

Here is the trick that makes “everything at once” possible on one processor. The OS doesn’t run the programs together — it runs them in turns, very fast.

It lets program A run for a few milliseconds, then freezes it exactly where A’s finger was on its list, and hands the processor to B, then to C. Then back to A, picking up where it paused. Round and round, dozens of times a second. This is called scheduling, or time-slicing: cutting the processor’s time into tiny slices and dealing them out.

It feels simultaneous because it is so fast. Your eye can’t catch a switch that happens in thousandths of a second. “Multitasking” on a single processor is one thing at a time, swapped so quickly the seams vanish.

And when it can’t, you feel it. A program that won’t give up its turn leaves everything else starved. That is the spinning wheel: the referee unable to pry the processor loose.

Handing out memory, and keeping walls up

The OS does the same refereeing with memory (item 6). Each program gets its own slice, and the OS draws a hard line around it: program A literally cannot read or write program B’s memory. The wall is enforced, not just polite.

That wall does two jobs. It keeps programs from corrupting each other’s data. And it means one program crashing doesn’t bring down the rest — its mess stays in its own slice. A misbehaving app falls over; you close it; everything else keeps running.

Owning the devices: no app touches the hardware

Here is the rule that ties it together: no app talks to the hardware directly. An app never reaches in and pokes the screen, the disk, or the keyboard. It asks the OS — “save this file,” “draw this here” — through a fixed set of requests the OS provides.

This is item 9’s abstraction, exactly. The OS is the layer that hides the messy real hardware behind clean, simple requests. The app says “save this file” and trusts the OS to know your particular disk. The same app runs on a different machine, with a different disk and screen, without a single change — because the OS papers over the differences underneath. The app only ever spoke to the OS, never to the metal.

Going through the OS buys three things. Safety: a rogue app can’t trash the disk or read another app’s memory — it can only ask, and the OS can refuse. Portability: one app runs on many machines, because the OS hides what makes them different. Order: one manager owns each shared device, instead of a free-for-all.

A worked example: three programs and one processor

Picture three programs that each need one full second of processor time: a music player, a download, and a ticking clock. One processor between them.

Hog it. Say the OS lets the music player run to completion first. For one whole second it owns the processor — the download frozen, the clock dead. Then the download runs its second, alone; then the clock. Each finishes, but for two-thirds of the time, two of your three programs looked frozen. That’s the stalled, unresponsive feeling.

Take turns. Now the OS slices each second into ten-millisecond turns and rotates: a sliver of music, a sliver of download, a sliver of clock, round and round. Over one second, each collects about a third of a second — spread across the whole second in tiny pieces. The music plays smoothly, the download creeps forward, the clock ticks on time. All three feel alive, because none waits long for its next turn. That rotation — a short, fair slice each, in turn — is called round-robin, and you’ll run it in a moment.

Same processor. Same total work. The only difference is how the OS deals out the turns — and that is the gap between a machine that feels alive and one that feels stuck.

The referee behind the calm surface

The smooth screen in front of you — a dozen programs, all apparently at once — is a fast, invisible manager taking turns on a single processor and holding walls up between everything that shares the machine. You never see it work. That is the mark of a good operating system, and a good piece of abstraction: the better it does its job, the less you know it’s there.

It’s worth sitting with how much that calm hides. The music that doesn’t stutter, the app that can’t read your other app’s secrets, the file that saves the same on any machine — each rests on a referee deciding, thousands of times a second, whose turn it is. You are not above that system, watching it run. You are inside it: one more user whose programs are scheduled, whose memory is walled, whose requests are granted or refused by a manager you’ll never meet. Seeing the referee doesn’t put you in charge of it. It just lets you understand why the machine sometimes feels alive and sometimes stuck — and that there was always someone taking turns, out of sight.

02 · Try · the lab

03 · Check · quick quiz

1. Your screen shows a browser, music, and messages all apparently running at once — but the machine has one processor that does one instruction at a time. How does the operating system pull this off?

  • It runs each program on its own separate processor
  • It gives each program a tiny turn on the one processor, switching between them so fast it looks simultaneous
  • It merges the three programs into one big program
  • It pauses two of them until you click on the third
Answer

It gives each program a tiny turn on the one processor, switching between them so fast it looks simultaneous — With one processor there is no true 'at once'. The OS slices the processor's time and rotates between programs thousands of times a second. The switches are too fast to see, so the seams vanish and it feels simultaneous.

2. An app wants to save a file to disk. What actually happens?

  • The app reaches into the disk hardware and writes the bytes itself
  • The app asks the operating system to save the file, and the OS deals with the actual disk
  • The app waits until no other program is using the disk, then takes direct control
  • The disk asks the app for permission before storing anything
Answer

The app asks the operating system to save the file, and the OS deals with the actual disk — No app touches the hardware directly. It asks the OS through a fixed set of requests, and the OS owns the device. That's what makes apps safe (they can only ask, the OS can refuse) and portable (the same app runs on a machine with a different disk).

3. Three programs each need one second of processor time. The OS lets the first one run all the way to completion before the others move at all. What does that feel like?

  • All three feel alive and responsive the whole time
  • The first runs while the other two sit frozen, then the next, then the last — two of them look stuck most of the time
  • Nothing runs, because they cancel each other out
  • The processor speeds up to handle all three together
Answer

The first runs while the other two sit frozen, then the next, then the last — two of them look stuck most of the time — Letting one program hog the processor means the others get no turns until it finishes — they appear frozen. Round-robin instead gives each a short slice in rotation, so all three make progress together and feel alive. Same work, same processor; only the turn-taking differs.

4. When you write a line of code like 'save the file' and trust the operating system to handle which disk and how, which idea from earlier in the course is that?

  • Abstraction — a layer hides the messy layer below behind a simple request
  • Time-slicing — cutting the processor's time into turns
  • The fetch-decode-execute loop running faster
  • Storing everything as on/off bits
Answer

Abstraction — a layer hides the messy layer below behind a simple request — This is abstraction from item 9: the OS is the layer that hides the real, messy hardware behind clean requests like 'save the file'. You trust the layer below without watching it — which is also why your app runs unchanged on a machine with different hardware.