Right now your computer is probably running a browser, a music player and a terminal, and dozens of programs you never started yourself. They all want the processor. They all want memory. Several of them want to draw on the same screen at the same moment. None of them knows the others exist.
So who stops them clashing? Who decides which one runs next, keeps one program from reading another’s passwords, and makes sure two programs saving files at once do not scramble the disk?
That is the job of the operating system (OS). In this course you will open it up and watch each part work, step by step.
Two jobs
An operating system does two things.
- It is an abstraction: it hides messy hardware behind simple ideas. Your program says “write these bytes to a file”. It does not say “move the disk head to track 1,822 and wait for sector 9 to come round”. A file, a process and a window are all ideas the OS invents so that programs do not have to care about the hardware underneath.
- It is a resource manager: it shares out the hardware fairly and safely. The processor, memory, disks, the network and the screen are resources: there is a limited amount, and many programs want them. The OS decides who gets what, and when, and stops anyone taking what is not theirs.
The heart of the OS is the kernel: the part that is always running, with full control of the hardware. Everything else (your programs, but also the shell, the desktop and most of the tools that come with the system) runs on top of it and has to ask the kernel for help.
Follow one printf
Here is what happens when a C program prints hi. Watch the dark marker on the right: it shows where the work is happening. The top two layers are part of your program. The orange layers are the kernel.
What happens when your program prints "hi"
-
Your code calls printf
Your program runs printf("hi\n"). It has no idea where the screen is or how to draw on it.
- Running
- your code
- CPU mode
- user
- Call
- printf("hi\n")
-
printf turns it into bytes
printf lives in the C library, which is ordinary code loaded into your program. It builds the 3 bytes h, i and a newline, then needs the operating system to deliver them.
- Running
- libc
- CPU mode
- user
- Call
- write(1, "hi\n", 3)
-
The write system call
The library asks the kernel to write 3 bytes to file descriptor 1. This is a system call: the CPU switches to kernel mode and runs the kernel's code.
- Running
- kernel
- CPU mode
- kernel
- Call
- write(1, "hi\n", 3)
-
The kernel picks the driver
File descriptor 1 is your terminal. The kernel passes the bytes to the terminal driver, the part of the kernel that knows how to talk to it.
- Running
- terminal driver
- CPU mode
- kernel
- Call
- (inside the kernel)
-
hi appears
The bytes reach the terminal, and hi shows up on your screen.
- Running
- terminal driver
- CPU mode
- kernel
- Call
- (inside the kernel)
-
Back in your program
write returns 3, the number of bytes written. The CPU drops back to user mode, printf returns, and your program carries on with its next line.
- Running
- your code
- CPU mode
- user
- Call
- write returned 3
Your program never touched the screen. It handed 3 bytes to the kernel and got back a number: how many bytes were written. The kernel worked out that file descriptor 1 means “this terminal”, found the right driver, and sent the bytes on their way. That one request across the line, from your program into the kernel, is a system call. You will see exactly how it works in Lesson 1.3.
Switch to Sequence to see the same trip as a sequence diagram: one column per layer, one arrow per step.
Reading a file goes down and comes back up
Writing sends data down the layers. Reading a file sends a request down and brings the data back up. This time there is a slow device at the bottom, so look at what the CPU does while the disk works.
Reading the first line of a file
-
Your code asks for a file
Your program calls fgets to read the first line of notes.txt.
- Running
- your code
- CPU mode
- user
- Call
- fgets(line, 100, f)
-
The library needs more bytes
fgets keeps a buffer. It is empty, so the library asks the kernel for the next 4096 bytes of the file in one go.
- Running
- libc
- CPU mode
- user
- Call
- read(3, buf, 4096)
-
The read system call
The CPU switches to kernel mode. The kernel checks that file descriptor 3 is open for reading in this process.
- Running
- kernel
- CPU mode
- kernel
- Call
- read(3, buf, 4096)
-
Which blocks hold the data?
The file system looks up where this part of notes.txt lives on the disk.
- Running
- file system
- CPU mode
- kernel
- Call
- (inside the kernel)
-
The driver asks the disk
The disk driver tells the disk which blocks to read and where in memory to put them. Then the CPU is free to run other programs while it waits. This is the trick that makes the whole machine feel fast: the slowest part of the job does not keep the CPU busy.
- Running
- disk driver
- CPU mode
- kernel
- Call
- (inside the kernel)
-
The disk does the work
The disk reads the blocks into memory, then raises an interrupt to say it has finished.
- Running
- disk
- CPU mode
- kernel
- Call
- (the disk is busy)
-
The driver gets the news
The interrupt runs the driver's handler, which marks the read as done.
- Running
- disk driver
- CPU mode
- kernel
- Call
- (inside the kernel)
-
Data goes into your buffer
The kernel keeps a copy of the blocks in memory, so the next read of this file is fast, and copies the bytes into your program's buffer.
- Running
- file system
- CPU mode
- kernel
- Call
- (inside the kernel)
-
Back in your program
read returns how many bytes it copied, the CPU drops back to user mode, and fgets hands you the first line.
- Running
- your code
- CPU mode
- user
- Call
- fgets returned the line
Four layers took part, and each one only knew about the layer next to it. The C library knew nothing about disks. The file system knew nothing about your program’s buffer size. The disk driver knew nothing about file names. That separation is why the same program can read a file from an SSD, a USB stick or a network drive without changing one line.
Now press Try it under either simulation and pick malloc(32). Asking for memory is something programs do all the time, and the answer may surprise you.
The map of this course
Each module of this course opens one part of that picture. Step through the map, then come back to it whenever you want to see where a lesson fits.
The course map
-
Programs become processes
A program on disk does nothing. Once started, it becomes a process with its own memory, and it may split its work into threads. Module 2 shows how processes are born, how fork and exec work, and what it costs to switch between them.
- Layer
- Programs you run
- Module
- 2: Processes and threads
-
Who runs next?
There are far more processes than CPU cores. The scheduler picks who runs next and for how long, many times a second. Module 3 compares the classic ways to choose.
- Layer
- Scheduler
- Module
- 3: CPU scheduling
-
Sharing without chaos
When threads share data, timing bugs appear. Locks fix them, and can cause a new problem: everyone waiting for everyone else. Modules 4 and 5 cover concurrency and deadlock, and how processes send each other messages.
- Layer
- Locks and messages
- Module
- 4 and 5: Concurrency and deadlock
-
Every process thinks it has the memory to itself
The memory manager gives each process its own private addresses and maps them onto real RAM, loading pages from disk only when they are needed. Module 6 follows an address all the way to RAM.
- Layer
- Memory manager
- Module
- 6: Memory
-
Bytes that survive a reboot
Drivers talk to devices, and file systems turn raw disk blocks into files and folders that survive a power cut. Module 7 goes from the read call down to the disk and back.
- Layer
- Files and drivers
- Module
- 7: Storage and files
-
The hardware underneath
Module 1, which you are in now, covers how the kernel takes control of this hardware in the first place: interrupts, user mode and kernel mode, and booting.
- Layer
- CPU cores, RAM, disks
- Module
- 1: What an operating system does
-
Many systems on one machine
Finally, Module 8 puts it all together: virtual machines and containers, and a capstone where you find out why a real server got slow.
- Layer
- Programs you run
- Module
- 8: Modern systems
In the real world
You can watch the system calls a program makes. On Linux, the strace tool prints each one as it happens. Try it on ls, with -c to get a count of each kind instead of the full list:
$ strace -c ls
Even a program that only lists a folder makes many system calls: it opens files, reads them, asks for memory and writes to the terminal. On macOS the same idea is called dtruss, and on Windows, Process Monitor shows a program’s requests to the kernel. :::
Quick check
Quick check
-
The OS hides the hardware behind the simple idea of a file. Resource management is about sharing hardware out fairly and safely.
-
Memory is limited and many programs want it at once, so the OS decides who gets how much. Your program's own variables are yours to manage.
-
malloc is library code. It asks the kernel for a big piece now and then (with brk or mmap) and serves most requests from that pool, with no system call at all.
-
Everything else, including your programs and the shell, runs on top of the kernel and asks it for help.
Recap
- An operating system abstracts the hardware (files, processes and windows instead of sectors and registers) and manages resources (CPU, memory, disks) between many programs.
- The kernel is the core of the OS. It alone touches the hardware directly.
- Programs ask the kernel for help with system calls, like
writeandread. - Work travels down through layers and back up. Each layer only knows its neighbours.
- Not every library call reaches the kernel:
mallocusually does not.
Next: the hardware the kernel drives, and the interrupt that lets it take control back from any program.
Join the discussion