OS architecture¶
Exercise os-architecture-annex.pdf · PDF
On the last page you typed cat /etc/shadow and got Permission denied. The disk worked. The file existed. The command was correct. Somebody said no. Who, exactly? And how did your command even ask?
To answer, we go inside the manager's office. Not every part of the operating system is equally powerful. One part has the keys to everything; the rest has to knock. Knowing where that line runs explains why one bad update stopped airports all over the world on a Friday in July 2024.
What you will learn
- Define the kernel and say what only the kernel is allowed to do
- Tell kernel mode from user mode and say which programs run in each
- Explain what a system call is and read one in the output of
strace - Describe the shell as an ordinary program for giving orders, graphical or command line
- Find your kernel version, the loaded modules and the driver of a device
- Compare a monolithic kernel and a microkernel by what happens when a driver fails
The kernel: the core of the OS¶
The kernel is the core of the operating system: the part that stays in RAM from boot to shutdown and the only software that touches the hardware.
Everything else that comes with Ubuntu — the desktop, the terminal, the file manager — is a collection of ordinary programs that came in the same box. They are part of the operating system, but they are not the kernel.
Careful: the kernel is not “a program in the background”
It is tempting to picture the kernel as one more process, like an antivirus running quietly. It is not in the process list at all. It is the code that runs whenever a program asks for something or a device needs attention — and it decides which process goes next.
Two modes: the back room and the front¶
Remember the kitchen: the OS is the manager. Now split the kitchen in two, with a wall in the middle.
The kitchen with a back room (we keep it all page)
- The back room holds the gas valves, the main switch, the knives and the pantry keys. Only the manager goes in.
- The front is where the orders are cooked. Each cook works on their own piece of countertop and nothing else.
- In the wall there is one small hatch with a bell. A cook who needs something from the back room rings and asks.
In the CPU, those two halves have names:
| Kernel mode (the back room) | User mode (the front) | |
|---|---|---|
| What is allowed | Every instruction; all the hardware can be reached | Calculating with its own memory, nothing else |
| Can touch the disk, screen, network? | Yes | No — it must ask |
| Can touch another program's memory? | Yes | No |
| Who runs here | The kernel and (on Linux and Windows) most drivers | Firefox, bash, games, LibreOffice — and the whole desktop |
The wall is not a rule of politeness. The CPU itself has the two modes. On Intel and AMD processors they are called ring 0 (kernel) and ring 3 (user), and the CPU refuses a forbidden instruction coming from a user-mode program.
The surprise for most people: the whole graphical desktop of Ubuntu runs in user mode. If it crashes, the kernel is still alive underneath.
Careful: kernel mode is not the administrator account
Modes belong to the CPU; accounts belong to the OS. Programs started by an administrator still run in user mode and still have to ask the kernel. Being administrator only means the kernel says "yes" more often.
System calls: ringing at the hatch¶
A system call is a request from a program to the kernel: "open this file", "write these bytes on the screen", "give me more memory".
When a program makes one, the CPU switches to kernel mode, the kernel checks whether this program may, does the work, and answers with a number. Then the CPU goes back to user mode.
flowchart LR
subgraph USER ["User mode: the front"]
APP["<b>Program</b><br>cat, Firefox, a game"]
end
subgraph KERNEL ["Kernel mode: the back room"]
K["<b>Kernel</b><br>the manager:<br>checks, then acts"]
DRV["<b>Driver</b><br>the appliance manual"]
end
HW["<b>Hardware</b><br>disk, screen, network"]
APP -->|"system call:<br>'open note.txt'"| K
K -->|"answer: a number<br>3 = yes, -1 = no"| APP
K --> DRV --> HW
A program never opens a file. It asks the kernel to open it. A program never draws on the screen. It asks. Even hello on your terminal goes through the hatch.
Careful: a system call is not something you type
Programs make system calls, thousands per second. You never type one. You can only watch them with a tool like strace.
Watch the hatch with strace¶
strace runs a program and prints every system call it makes. Try these four, in order. The Exercise sheet has space for each answer.
1. How many requests does it take to list a folder?
The output ends with a table. The last row, total, gives the number of calls: around 100–150 just to list one folder. The exact figure changes between machines.
2. One request, in detail:
Read it aloud: "kernel, write these 6 bytes to output number 1, the terminal". The kernel answers 6: all six written. Six again — five letters plus the end of line.
3. Opening a file you are allowed to read:
The openat line for /etc/hostname ends in = 3. The kernel said yes and handed over ticket number 3 for that open file. That ticket is called a file descriptor. (| tail -3 keeps only the last three lines: the first twenty calls just load the program's libraries.)
4. Opening a file you are not allowed to read:
There it is — last page's refusal, caught at the hatch. The program asked; the kernel answered -1. cat only turned that number into a sentence.
What happens when a program skips the hatch?¶
Picture three people: an App, the Kernel and the Disk. The App may speak only to the Kernel.
| Scene | The conversation |
|---|---|
Save "hello" in note.txt |
App: "open note.txt for writing" → Kernel: "3" → App: "write 6 bytes to 3" → Kernel to Disk: "store these 6 bytes" → Kernel: "6" → App: "close 3" → Kernel: "0" |
Read /etc/shadow |
App: "open /etc/shadow" → Kernel checks the user: "-1, permission denied". The Disk never hears about it. |
| The App talks to the Disk directly | The CPU refuses the instruction and the kernel kills that process. Only that program dies; the others keep running. |
The shell: how you talk to the kernel¶
Programs talk to the kernel through system calls. How do you talk to it? Through a program made for that job.
A shell is an ordinary user-mode program that takes your orders and turns them into requests to the kernel.
The name is the picture: the kernel is the seed; the shell is the part you can touch from outside. It comes in two forms:
| Graphical shell (GUI) | Command-line shell (CLI) | |
|---|---|---|
| You give orders by | Clicking on windows, icons, menus | Typing commands |
| Examples | GNOME on Ubuntu, the Windows desktop | bash on Linux, PowerShell on Windows |
| Strong point | Easy to discover | Exact, repeatable, works over a network |
GUI stands for Graphical User Interface; CLI for Command-Line Interface. Both are normal programs: you can change your shell and keep the same kernel.
The first line prints /bin/bash: your shell is a program stored in a file, like any other. The second lists every shell installed (sh, bash, dash...).
Careful: the terminal is not the operating system
The terminal window shows a shell, and the shell is one more user-mode program. Close it and the OS does not even blink.
Drivers and modules¶
A driver has to reach its device, so on Linux and Windows most drivers run in kernel mode, in the back room. Linux keeps them as separate pieces called modules and loads each one only when its device is present.
uname -r prints the kernel version. lsmod | wc -l counts the modules loaded right now — usually between 100 and 300.
Now find the driver of your network card:
Look for the line Kernel driver in use: followed by a name such as e1000e, r8169 or igc — it depends on the card. Nothing on screen? Replace ethernet with network. Then ask who wrote it (replace DRIVER with the name you found):
The licence will almost always say GPL. Remember that word: the page on software licences explains it. If modinfo answers "not found", that driver is built into the kernel file itself; try usbhid (the keyboard and mouse driver) instead.
A very bad Friday¶
On Friday 19 July 2024, the security company CrowdStrike sent an automatic update to its product Falcon, which includes a driver running in kernel mode on Windows. The update contained a faulty data file, and the driver read memory it should not have.
In user mode, that kills one program. In kernel mode there is nobody above to stop it, so Windows stopped everything: the blue screen. Microsoft estimated 8.5 million Windows computers were hit. Airports, hospitals, banks and TV channels stopped. The fix had to be done by hand on each machine: start in safe mode and delete one file.
A bug in the front of the kitchen burns one dish. A bug in the back room closes the restaurant. That is the price of running in kernel mode.
A blue screen does not mean “Windows is bad”
It means code in kernel mode failed and the kernel stopped on purpose, to protect your data. Linux has its own version, called a kernel panic.
One big kernel or a tiny one?¶
After CrowdStrike, the obvious question: why not take the drivers out of the back room? Some systems do. There are three designs:
| Monolithic | Microkernel | Hybrid | |
|---|---|---|---|
| In kernel mode | Everything: scheduler, memory, file systems, drivers, network | The minimum: scheduling, memory, messages. Drivers and file systems are ordinary processes | A small core plus many services kept inside for speed |
| A driver fails | The whole system can go down | That process is restarted; the system lives | Depends on where it runs |
| Cost | One bug is everybody's bug | Slower: every request becomes several messages | In between |
| Examples | Linux (and so Android) | MINIX | Windows, macOS and iOS (XNU) |
flowchart TB
subgraph MONO ["Monolithic: one big back room"]
direction TB
M1["<b>Kernel mode</b><br>scheduler · memory · files<br>drivers · network"]
end
subgraph MICRO ["Microkernel: a tiny back room"]
direction TB
U1["<b>User mode</b><br>drivers · file systems · network<br>as normal processes"]
K1["<b>Kernel mode</b><br>scheduler · memory · messages"]
U1 -->|"messages"| K1
end
"LINUX is obsolete"¶
On 29 January 1992, Andrew Tanenbaum — a professor in Amsterdam and the author of MINIX, the small teaching system Linus Torvalds had learnt with — posted a message titled "LINUX is obsolete". A monolithic kernel, he wrote, was a step back into the 1970s. Linus, 22 years old, answered the same day, and not politely. The argument is still quoted today.
Who won? Linux stayed monolithic, added loadable modules, and ended up in phones, routers and data centres. Microkernels kept the argument about safety.
Careful: monolithic does not mean old and bad
It is a trade: speed and simplicity against isolation. The most used kernel in the world is monolithic. For a machine that keeps a patient alive, isolation may win; for a gaming PC, speed and the widest choice of drivers win.
Key takeaways¶
- The kernel is the core of the OS: always in RAM, the only software that touches the hardware.
- The CPU has two modes: kernel mode (everything allowed) and user mode (own memory only). Apps, the shell and even the desktop run in user mode.
- A program gets anything done by making a system call; the kernel checks, acts and answers with a number (
-1= refused).stracelets you watch them. - The shell — graphical (GUI) or command line (CLI, like
bash) — is an ordinary program for giving orders. - Most drivers run in kernel mode; Linux loads them as modules. A bad kernel-mode driver can stop the whole machine (CrowdStrike, 19 July 2024).
- Kernel designs: monolithic (Linux), microkernel (MINIX), hybrid (Windows, macOS) — a trade between speed and isolation.
Quick check¶
1. In which mode does Firefox run?
- User mode
- Kernel mode
- Both modes at the same time
- Neither, it runs inside the firmware
Firefox is an application, so it runs in user mode, with access only to its own memory. To open a file or use the network it must ask the kernel with a system call.
2. What is a system call?
- A request from a program to the kernel
- A command you type in the terminal
- An error message sent by the hardware
- A message between two computers on a network
A system call is how a user-mode program rings at the hatch: "open this file", "write these bytes". Programs make thousands per second; you only see them with a tool like strace.
3. What can a bug in a kernel-mode driver do that a bug in a normal app cannot?
- Stop the whole system
- Close its own window
- Use a lot of RAM
- Slow down the Internet connection
In kernel mode there is nobody above to stop a faulty driver, so the kernel halts everything: a blue screen or a kernel panic. That is what happened with CrowdStrike in July 2024.
4. What is bash?
- A shell, a user-mode program for giving orders
- The kernel of Ubuntu
- The driver for the keyboard
- The firmware of the PC
bash is a command-line shell: an ordinary program stored in /bin/bash. You can swap it for another shell and keep the same kernel.
5. Which design is the Linux kernel?
- Monolithic, with loadable modules
- Microkernel
- Hybrid
- It has no kernel, only a shell
Linux is monolithic: scheduler, memory, file systems and drivers all run in kernel mode. Drivers are loaded as modules only when needed. Windows and macOS are hybrid; MINIX is a microkernel.
6. strace shows a line for /etc/shadow ending in = -1 EACCES. Who refused to open the file?
- The kernel, answering the system call with -1
- The cat program, which checked the file itself
- The disk, which could not find the file
- The shell, before running the command
cat asked through the openat system call; the kernel checked the user and answered -1. cat only printed that answer as "Permission denied". The disk was never asked.
7. The Ubuntu desktop crashes and restarts, but a download in another program carries on. Pick the two that are true.
- The graphical desktop runs in user mode, so its crash does not stop the kernel
- Other processes keep running because only the crashed program died
- The desktop is part of the kernel, so the kernel must have restarted
- The machine switched to a microkernel to survive the crash
The whole GUI is ordinary user-mode software. When a user-mode program crashes, the kernel cleans it up and the rest of the system carries on — the opposite of a crash in kernel mode.
Next: Windows, macOS, Linux, Android — which manager for which kitchen? Comparing operating systems.