Appearance
2.1.1 — Why an Operating System Exists
Picture a laptop running the photo-upload service we will follow through this whole Part. It is a small Node program. People send it photos. It saves each photo to disk and makes a small thumbnail of it. While it runs, the same laptop is also playing music, showing a browser with your bank open, and running a code editor.
Four programs are running at once. None of them can read the others' memory, so the music player cannot see your bank password. If the music player crashes, the browser does not even notice. Each program behaves as if it owned the whole machine. Something is enforcing all of that, by force, all the time. That something is the operating system, and this page explains why it has to exist and what it is made of.
1. The bare machine is powerful and unsafe
Part 1 built a machine. A CPU fetches and runs instructions (1.5). Memory feeds it (1.6). Underneath, it is all switches and voltages.
On its own, that machine can run exactly one program. That program owns everything. It can write to any byte of memory. It can send any command to the disk. It can stop the CPU. Now imagine running two programs on it at the same time. Nothing stops the first program from writing over the second program's memory. Nothing stops a buggy program from sending the disk a command that wipes it. And if one program crashes, it can take the whole machine down with it, because nothing is watching.
Raw hardware also offers no services. There is no "open a file". There is only "move the disk's read head to track 4,001 and read sector 12". There is no "send this message to that computer". There is only "put these voltages on the network card's wires". Every program would have to know the exact details of every device.
So the machine needs a referee. The referee must do four jobs:
- Share the CPU. Many programs must take turns on a few CPU cores, and no program may keep the CPU forever.
- Share memory safely. Each program gets its own memory, and no program may touch another's.
- Share the devices. The disk, the network card and the screen must be used by many programs without them tripping over each other.
- Offer simple services. Programs should be able to say "open
photo-001.jpg" and let the referee worry about which sectors that means.
The software that does these four jobs is the operating system (OS). Its core, the part that actually enforces the rules, is called the kernel. The word comes from the kernel of a nut: the small, hard centre that everything else surrounds.
The referee's dilemma
There is a problem hiding in that list. To stop one program from touching another's memory, the referee itself must be able to touch all memory. To stop a program from misusing the disk, the referee must be able to command the disk directly. So the referee needs powers that no ordinary program is allowed to have.
But if those powers were simply available to every program, there would be no referee. Any program could just use them. So the machine must have two levels of power: a trusted level that can do anything, and an ordinary level that cannot. And an ordinary program must not be able to promote itself to the trusted level.
Software cannot enforce that rule on its own, because a misbehaving program is also software. Only the hardware can. Modern CPUs have this rule built into the silicon, and the next page, 2.1.2, shows exactly how it works. For now, hold one sentence: the kernel runs at the trusted level, every other program runs at the ordinary level, and the CPU itself keeps them apart.
2. How we got here: each step fixed the last step's waste
The kernel did not appear all at once. It grew in steps over about thirty years. Each step fixed a waste in the step before, and each fix added one of the kernel's permanent jobs. Here is the whole story in one picture first.
Now the same story, one step at a time.
1940s: no operating system at all. Machines like ENIAC (1945) were programmed by plugging cables into boards and setting switches. Loading a new program took days. The program owned every part of the machine, because there was nothing else on it.
1950s: batch processing. Computers cost millions. Every minute an operator spent loading the next job was money lost. So programs were punched onto cards and collected into a pile, called a batch. A small program stayed in memory all the time and ran the jobs one after another. It loaded a job, handed it the CPU, took control back when the job finished or crashed, printed the result, and loaded the next job. That small program was called the resident monitor, because it stayed resident in memory and monitored the jobs. The resident monitor is the ancestor of every kernel. One of the first, GM-NAA I/O, was written in 1956 by General Motors and North American Aviation for the IBM 704.
The batch system still wasted a lot. A job would often stop and wait for a slow tape drive. While it waited, the expensive CPU waited too, doing nothing.
1960s: multiprogramming. The fix was to keep several jobs in memory at once. When one job had to wait for a device, the CPU switched to another job. This one idea created three of the kernel's permanent jobs at once:
- Memory protection. With several jobs in memory, one job must not be able to write over another.
- Scheduling. With several jobs ready, something must choose which one runs next.
- Sharing devices. With several jobs using the same tape drives and printers, something must keep their requests apart.
How does the CPU know when the device has finished, so it can return to the waiting job? The device sends a signal called an interrupt. 2.7.1 explains interrupts properly. For now, an interrupt is the hardware tapping the CPU on the shoulder to say "something happened".
1960s and 1970s: time-sharing. Multiprogramming kept the CPU busy. But a programmer still handed in a job and waited hours for the printout. Time-sharing changed that. It added a timer interrupt: a clock that taps the CPU on the shoulder many times a second, even if no program is waiting for anything. Each tap gives the kernel a chance to switch to another program. With dozens of people typing at terminals, the switching was so fast that each person felt the machine was theirs alone.
MIT's CTSS (1961) was the first big time-sharing system. MULTICS, a larger project by MIT, Bell Labs and General Electric, came next. In 1969, two people at Bell Labs who had worked on MULTICS, Ken Thompson and Dennis Ritchie, started writing a smaller system called UNIX. Nearly every server running today descends from UNIX's ideas. Around the same time, Britain's Atlas computer (1962) introduced a trick that lets every program believe it has more memory than the machine really has. That trick is virtual memory, and 2.5.1 builds it.
1980s onward. The same ideas moved from room-sized machines to personal computers, then to phones. Windows NT (1993), Linux (1991) and macOS each sit on a protected kernel of the kind this chapter describes. And the story keeps going in the same direction. A virtual machine shares one physical machine between several whole operating systems (14.1.2). A container shares one kernel between many walled-off programs (14.1.3). Each step is the same move: find the waste in the layer below, and share it more finely.
3. What the kernel is made of
"The kernel" is not one lump. It is a set of parts, called subsystems, and each one does one of the four jobs from section 1. The rest of Part 2 is a tour of these parts, one chapter each.
The quickest way to see them is to watch our photo-upload service start. Here are its first few lines. Each marked line asks the kernel for something, and each request goes to a different subsystem.
ts
// upload-service-startup.ts — the first lines of the photo-upload service
import { writeFileSync, mkdirSync } from "node:fs";
import { createServer } from "node:http";
console.log("my process id:", process.pid); // (1)
const photo: Buffer = Buffer.alloc(5 * 1024 * 1024); // (2)
mkdirSync("uploads", { recursive: true });
writeFileSync("uploads/photo-001.jpg", photo); // (3)
const server = createServer((req, res) => res.end("ok"));
server.listen(8080, () => { // (4)
console.log("listening on port 8080");
setTimeout(() => { // (5)
console.log("shutting down after 1 second");
server.close();
}, 1000);
});
// Prints (the process id is different on every run):
// my process id: 15904
// listening on port 8080
// shutting down after 1 secondWalk through the marked lines in order:
(1)process.pidis the program's process ID, a number the kernel gave this running program when it started. The part of the kernel that creates running programs, gives them IDs and cleans up after them is the process manager. 2.2.1 is about it.(2)Buffer.allocasks for 5 MB of memory. Node asks the kernel's memory manager for it. The memory manager hands out memory and makes sure no other program can see it. Chapter 2.5 is about it.(3)writeFileSyncsaves the photo under a name. The file system turns "the file calleduploads/photo-001.jpg" into "these numbered blocks on the disk". Chapter 2.6 is about it.(4)server.listen(8080)asks to receive network connections on port 8080. The network stack handles everything between "a connection arrived" and the electrical signals on the network card. Part 5 is about it.(5)setTimeoutasks to be woken up in one second. While the program waits, it uses no CPU at all. The scheduler decides which program runs on the CPU at each moment, and it knows not to run this one until its second is up. Chapter 2.3 is about it.
Underneath all five sits a sixth subsystem, the device drivers. A device driver is a piece of kernel code that knows how to talk to one kind of hardware: one driver for this disk, another for that network card, another for the keyboard. Each device speaks its own private language, and the driver translates. 2.7.1 is about them.
The picture below puts it all together.
Read the figure from top to bottom. A program makes a request, such as "save this file". The request enters the kernel and goes to the file system. The file system works out which blocks to write. It hands that job to the disk's driver. The driver sends the disk the exact commands it understands. No ordinary program ever talks to hardware directly. That single rule is what makes the whole arrangement safe.
4. The kernel is not the whole operating system
People often use "kernel" and "operating system" as if they meant the same thing. They do not, and the difference answers a question that confuses many beginners: is Linux an operating system?
Strictly speaking, Linux is a kernel. Linus Torvalds announced it in August 1991 as a hobby project, and it is still only the kernel. A whole operating system needs much more around the kernel:
- a program that starts everything else when the machine boots (2.1.3);
- a shell, the program that reads the commands you type (2.8.1);
- basic tools such as
ls,cpand a text editor; - the standard library that programs use to talk to the kernel (next page);
- usually a graphical desktop.
These parts outside the kernel are together called the userland, because they all run as ordinary user programs. A distribution, such as Ubuntu or Debian, is a complete operating system built from the Linux kernel plus a chosen userland. Android also uses the Linux kernel, but with a completely different userland on top. That is why an Android phone and an Ubuntu server share a kernel and yet feel nothing alike.
The same split exists everywhere. Windows has its kernel and a huge userland of services and tools around it. macOS has a kernel called XNU and a userland around that. 2.9.1 looks at both.
5. Where to draw the line: monolithic, micro and hybrid kernels
Section 3 listed the kernel's parts. Here is a design question that has started arguments for forty years: should all of those parts run at the trusted level, or should most of them be pushed out to the ordinary level?
A monolithic kernel keeps everything at the trusted level. Linux works this way. The scheduler, the memory manager, every file system, every driver and the network stack all live together. When the file system needs the disk driver, it simply calls it, like one function calling another. That is fast.
The cost is that a bug anywhere is a bug at the trusted level. A badly written driver for one cheap webcam can crash the entire machine. Linux softens this a little with loadable kernel modules. A module is a piece of kernel code, usually a driver, that can be added to the running kernel and removed again without rebooting. But once loaded, a module runs at the trusted level like everything else, so a buggy module is still dangerous.
A microkernel keeps only a tiny core at the trusted level. The core does the bare minimum: it schedules, it manages memory, and it carries messages between programs. Everything else runs as an ordinary program. The file system is an ordinary program called a server. Each driver is an ordinary program. When a program wants to read a file, it sends a message to the file server. The file server sends a message to the disk driver. The replies travel back the same way.
This is sturdier. If the disk driver crashes, it is just an ordinary program crashing. The core can restart it, and the rest of the system keeps going. The cost is speed. Every message has to cross from one program to another through the core, and each crossing takes time. Reading one file can mean several crossings.
Real microkernels are used where staying alive matters more than raw speed. QNX, an operating system found inside many car dashboards and factory machines, is a microkernel. So is MINIX, which the computer scientist Andrew Tanenbaum wrote for teaching.
A hybrid kernel sits in between. It is organised like a microkernel inside, with clear walls between its parts. But it keeps most of those parts at the trusted level, to avoid the cost of all the messages. The Windows NT kernel and Apple's XNU kernel are both described this way.
| Design | Speed | A driver crash… | Examples |
|---|---|---|---|
| Monolithic | fastest | can take down everything | Linux |
| Microkernel | slower | restarts just that driver | QNX, MINIX |
| Hybrid | close to monolithic | usually takes down everything | Windows, macOS |
The table hides one subtlety. A hybrid kernel has the structure of a microkernel, but because most parts still run at the trusted level, a crashing driver is usually as dangerous as on Linux. The walls inside a hybrid are for organising the code, not for protection.
The famous argument. In January 1992, a few months after Linux appeared, Tanenbaum posted a message titled "LINUX is obsolete" to an online discussion group. He argued that monolithic kernels were a step backwards and microkernels were the future. Torvalds replied that Linux worked, it was fast, and people could use it today. The argument became one of the most famous in computing. History gave a split verdict. Linux won the world's servers on speed. Microkernels won where a crash is not acceptable, such as in cars.
The deeper lesson is one you will meet again and again in this book. Keeping parts apart makes a system sturdier, and every wall between parts costs time to cross. The same trade appears when a company decides whether to build one big program or many small services talking over a network (10.17). There is no free answer. There is only a choice of which cost to pay.
What the interviewer will push on
"What is an operating system?" The interviewer is checking whether you can name what it does, not just give examples. A memorised answer says "software that manages hardware". A good answer names the four jobs: share the CPU, keep each program's memory private, share the devices, and offer simple services like files and network connections. The common wrong answer lists Windows, macOS and Linux and stops there.
"What is the difference between the kernel and the operating system?" They want to hear that the kernel is the trusted core that enforces the rules. The operating system is the kernel plus everything around it, such as the boot program, the shell, the tools and the libraries. The tell of real understanding is the Linux and Android example: same kernel, completely different operating systems. The common mistake is to say they are the same thing.
"Is Linux a monolithic kernel or a microkernel? Why?" Monolithic, with loadable modules. They are checking whether you know why: everything calls everything else directly, which is fast. Follow up on your own with the cost: a driver bug can crash the whole machine, because modules run at the trusted level once loaded. The common wrong answer is "modules make it a microkernel". They do not, because modules still run at the trusted level.
"Why can't the operating system just trust programs to behave?" They are checking whether you see that protection must come from hardware. A program that misbehaves, by bug or on purpose, is also software, so a software-only rule can be ignored. The good answer says the CPU has two levels of power built in, and only the kernel runs at the trusted level.
Something to volunteer that nobody asks for: the history explains the jobs. Batch systems wasted the CPU while it waited for tape, so multiprogramming kept several jobs in memory. That one change is what created memory protection and scheduling. Saying this shows you understand why each kernel job exists, not just that it does.
Recall
- The bare machine runs one program that owns everything. The operating system exists to share the CPU, keep each program's memory private, share the devices, and offer simple services. Its trusted core is the kernel.
- The kernel needs powers no ordinary program may have, so the CPU provides two levels of power in hardware, and only the kernel runs at the trusted one.
- The kernel grew in steps: the resident monitor of batch systems, then multiprogramming (which created memory protection and scheduling), then time-sharing (which added the timer interrupt).
- The kernel's subsystems are the process manager, memory manager, file systems, network stack, scheduler and device drivers. Linux is a kernel; a distribution is the kernel plus a userland.
- A monolithic kernel (Linux) keeps everything at the trusted level and is fast. A microkernel (QNX) keeps a tiny core there and is sturdier but slower. A hybrid kernel (Windows, macOS) is organised like a microkernel but keeps most parts inside.
Self-test: What four jobs does an operating system do? Why must the protection come from hardware rather than software? Which kernel job did multiprogramming create, and why? Why is Android not "Linux" even though it runs the Linux kernel? What does a microkernel gain, and what does it pay for it?
Quiz Bank
FoundationalWhat is the difference between the kernel and the operating system?
The kernel is the trusted core. It runs at the CPU's trusted level and enforces the rules: who runs on the CPU, whose memory is whose, and who may use which device. The operating system is the kernel plus everything that runs around it as ordinary programs, which is called the userland. That includes the program that starts everything at boot, the shell, basic tools such as ls, the standard library programs use to reach the kernel, and usually a desktop.
Linux is strictly a kernel. Ubuntu is an operating system built on it. Android uses the same kernel with a completely different userland, which is why the two feel nothing alike.
FoundationalWhy does an operating system need hardware help to protect programs from each other?
Because a program that misbehaves is also software. If the only thing stopping a program from writing over another program's memory were a software rule, the bad program could simply skip the rule. So modern CPUs have two levels of power built into the silicon. The kernel runs at the trusted level and can do anything. Every other program runs at the ordinary level, where the CPU itself refuses dangerous instructions. A program cannot promote itself, because the switch between levels is controlled by the hardware. Page 2.1.2 shows the mechanism.
AppliedHow did multiprogramming create the need for memory protection and scheduling?
Batch systems ran one job at a time, and the CPU sat idle whenever that job waited for a slow device such as a tape drive. Multiprogramming fixed the waste by keeping several jobs in memory at once and switching the CPU to another job whenever one had to wait.
That change created two new problems. With several jobs in memory together, one job could write over another, so the system needed memory protection. With several jobs ready to run, something had to choose which one ran next, so the system needed scheduling. Both are still permanent jobs of every kernel today.
InterviewWhat is the difference between a monolithic kernel and a microkernel, and what is the trade-off?
A monolithic kernel, such as Linux, keeps the scheduler, memory manager, file systems, drivers and network stack all at the trusted level. The parts call each other directly, so it is fast. But a bug in any part, even one small driver, can crash the whole machine.
A microkernel, such as QNX, keeps only a tiny core at the trusted level. File systems and drivers run as ordinary programs that talk by sending messages through the core. A crashing driver is just a crashing program, so the core can restart it and the system survives. The cost is speed, because every message crosses between programs.
The trade-off is isolation against speed. Linux chose speed and won the server world. Microkernels are chosen where a crash is unacceptable, such as in cars. Windows and macOS use a hybrid kernel, organised like a microkernel but with most parts kept at the trusted level for speed.
InterviewDo loadable kernel modules make Linux a microkernel?
No. A loadable kernel module can be added to the running kernel and removed without a reboot, which makes Linux flexible. But once loaded, the module runs at the trusted level, inside the same kernel as everything else. It can read and write any kernel memory, and a bug in it can crash the machine. A microkernel's defining feature is that drivers and file systems run outside the trusted level as ordinary programs. Modules change when code joins the kernel, not where it runs.
StaffA team is choosing an operating system for a medical device that must never stop, and another for a high-traffic web server. How does kernel design inform each choice?
Start from the trade-off: walls between parts buy survival, and every wall costs time to cross.
For the medical device, survival matters far more than raw speed. A microkernel system such as QNX fits, because a failing driver runs as an ordinary program and can be restarted while the rest of the device keeps working. The message-passing cost is affordable, because the device does a modest amount of work and has to do it reliably.
For the web server, throughput matters most, and a crash is handled at a different level: the load balancer sends traffic to other servers while the failed one restarts (Part 10). A monolithic kernel such as Linux fits, because its parts call each other directly and the network path is as short as possible.
The staff-level point is that reliability can be bought at different layers. The device must survive inside one machine. The web service survives by having many machines.
Scenario Drill
DrillA colleague says the operating system is just the desktop and the apps that come with it, and that the kernel is an old idea nobody needs any more. Using this page, explain what would break without the kernel.
Start with the photo-upload service and the three other programs on the same laptop.
Without a kernel, nothing would keep each program's memory private. The music player could read the browser's memory, including the bank password. A bug in one program could write over another program's data.
Nothing would share the CPU fairly. A program stuck in an endless loop would keep the CPU forever, because there would be no timer interrupt handled by a referee to take it away.
Nothing would keep device requests apart. Two programs could send the disk conflicting commands at the same moment and corrupt it.
And there would be no simple services. The upload service could not say "save photo-001.jpg". It would have to know which sectors on this exact disk model are free, and it would have to know the private command language of the disk.
The desktop and the apps are the userland. They sit on top of the kernel, and every one of them depends on it. The kernel is old, but it is the one part that cannot be removed.
Next: we keep saying the kernel runs at a "trusted level" and programs at an "ordinary level", and that the CPU keeps them apart. Page 2.1.2 opens that wall: what the two levels actually are inside the CPU, and the one guarded door, the system call, through which every program asks the kernel for anything.