Operating Systems Module 1: What an operating system does Lesson 1.1 10 min

The operating system is a referee

Why your programs never clash over the screen, the disk or memory: the operating system turns one machine into a safe, shared machine for many programs.

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.

  1. 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.
  2. 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"

  1. 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")
  2. 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)
  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)
  4. 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)
  5. hi appears The bytes reach the terminal, and hi shows up on your screen.
    Running
    terminal driver
    CPU mode
    kernel
    Call
    (inside the kernel)
  6. 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

  1. 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)
  2. 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)
  3. 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)
  4. 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)
  5. 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)
  6. 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)
  7. 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)
  8. 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)
  9. 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

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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:

Terminal
$ 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

  1. Your program saves a file without knowing which disk, sector or file system is involved. Which job of the operating system makes that possible?
  2. Which of these is a resource the operating system has to share out between programs?
  3. Does a call to malloc always go into the kernel?
  4. What is the kernel?

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 write and read.
  • Work travels down through layers and back up. Each layer only knows its neighbours.
  • Not every library call reaches the kernel: malloc usually does not.

Next: the hardware the kernel drives, and the interrupt that lets it take control back from any program.

Your progress is saved in this browser. Sign in to keep it on every device. Free, no password.

Join the discussion