Find a course

Module 1: What an Operating System DoesLesson 1 of 1115 min

The Role of the Operating System

See the OS as both an abstraction layer and a resource manager, learn kernel mode versus user mode, and follow a system call from app to kernel and back.

Step 1 of 14Learn

Two programs, one machine

Open a music app and a web browser at the same time on a laptop with one SSD, one screen and a few CPU cores. Both programs want to use the processor right now. Both want memory. Both want to read files from the same drive and draw on the same screen. Neither was written with the other in mind, and yet they don't crash into each other, the song keeps playing while the page loads, and a bug in the browser can't corrupt the music app's memory.

The software that makes this possible is the operating system (OS): Windows, macOS, Linux, Android, iOS and many others. It sits between application programs and the hardware, and it has two big jobs.

First, it is an abstraction layer. Hardware is messy: every disk, network card and graphics chip has its own commands and quirks. The OS hides that mess behind clean, uniform ideas such as files, processes, sockets and windows. A program says open the file notes.txt and doesn't care whether the bytes live on an NVMe SSD, a USB stick or a network share.

Second, it is a resource manager. CPU time, memory, storage space and devices are limited and shared. The OS decides who gets what, when, and for how long, and it enforces those decisions so no program can grab more than its share or reach into another program's data.

This course looks at how operating systems do both jobs: processes and threads, CPU scheduling, synchronization and deadlock, virtual memory, file systems and protection.

Step 2 of 14Practice · Diagram

Where the operating system sits

Each layer uses only the one below it. Tap a layer.

Which layer is allowed to send commands to the SSD directly?

Tap the shape that answers the question, then press Check.

Where the operating system sitsLayer diagram: Applications, System libraries, Kernel, Hardware.ApplicationsSystem librariesKernelHardware
Applications

Browser, music app, editor: user mode, no direct hardware access.

System libraries

The C library or Windows API wrap system calls in friendly functions.

Kernel

Kernel mode: schedules the CPU, manages memory, files, devices and security.

Hardware

CPU cores, RAM, SSD, screen, network card.

Step 3 of 14Practice · Sort into groups

Sort each OS feature by which of the two big jobs it mainly serves.

Tap an item, then tap its group (or drag it there).

Step 4 of 14Learn

Kernel mode and user mode

If any program could do anything, one buggy or malicious app could overwrite the OS, read other programs' passwords or wipe the disk. To prevent this, processors support at least two privilege levels, and the OS uses them.

In kernel mode (also called supervisor mode), code can execute every instruction, access all memory and talk directly to devices. Only the core of the OS, the kernel, runs here.

In user mode, code is restricted. It can compute freely on its own memory, but privileged instructions, such as disabling interrupts, changing memory mappings or talking to a disk controller, are forbidden. If a user program tries one, the CPU refuses and raises an exception, and the kernel usually terminates the program.

A single bit in a CPU status register records the current mode. The hardware switches to kernel mode in only a few controlled situations: when a program makes a system call, when a device raises an interrupt, or when an exception occurs, such as dividing by zero or touching memory it doesn't own. In every case, control jumps to kernel code at an address the kernel itself chose in advance, so user code can never sneak into kernel mode at a place of its own choosing.

x86-64 processors technically have four rings, numbered 0 to 3, but mainstream operating systems use just two: ring 0 for the kernel and ring 3 for applications.

Step 5 of 14Practice · True or false

A program running in user mode can switch the CPU into kernel mode by executing an instruction that jumps directly to any address inside the kernel.

Step 6 of 14Learn

System calls: asking the kernel for help

Since user programs can't touch hardware, how does a text editor save a file? It asks the kernel through a system call (syscall), the official doorway between user mode and kernel mode.

Here's what happens when a program calls write(fd, buffer, 100) to write 100 bytes to an open file:

  1. A library function, part of the C standard library on Linux or the Windows API, puts a syscall number identifying write and the arguments into agreed registers.
  2. It executes a special trap instruction (syscall on x86-64, svc on ARM). The CPU switches to kernel mode and jumps to the kernel's syscall handler.
  3. The kernel looks up the number, checks the arguments: is fd a file this process actually opened? Does the buffer lie inside the process's own memory? Does it have permission to write?
  4. If everything is valid, the kernel does the work, perhaps copying the data into a cache and scheduling a disk write.
  5. It places a return value (bytes written, or an error code) in a register, switches back to user mode and resumes the program right after the trap.

Linux has a few hundred system calls; common ones include open, read, write, close, fork, execve, mmap and exit. Most programmers never call them directly, but every file access, network message and new process goes through one.

Step 7 of 14Practice · Put in order

Put the steps of a system call in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. The return value is set and the CPU returns to user mode
  2. The kernel validates the arguments and permissions
  3. The library places the syscall number and arguments in registers
  4. The kernel performs the requested operation
  5. A trap instruction switches the CPU to kernel mode

Step 8 of 14Note

Step 9 of 14Practice · Work it out

A program writes a 4,000,000-byte log. If it uses one system call per byte and each call has about 0.5 microseconds of overhead, roughly how many seconds of overhead does that add?

Step 10 of 14Practice · Chart

Writing 4 MB: one byte per call vs. 4 KB per call

Tap the approach a standard library's buffered output uses.

Tap the bar that answers the question, then press Check.

Writing 4 MB: one byte per call vs. 4 KB per callBar chart with 2 categories: 1-byte writes (4,000,000 calls): 2,000 ms; 4 KB writes (977 calls): 0.5 ms.0 ms500 ms1,000 ms1,500 ms2,000 ms1-byte writes (4,000,000 calls)4 KB writes (977 calls)2,000 ms0.5 ms
Illustrative: assumes 0.5 microseconds of overhead per system call.
Writing 4 MB: one byte per call vs. 4 KB per call
ItemTime in system call overhead
1-byte writes (4,000,000 calls)2,000 ms
4 KB writes (977 calls)0.5 ms

Step 11 of 14Learn

What counts as part of the OS?

The word operating system is used loosely. Strictly, it is the kernel: the code running in kernel mode that manages processes, memory, files, devices and security. In everyday speech, it also includes everything shipped with it.

A typical OS distribution contains:

  • The kernel, including device drivers.
  • System libraries, such as the C library or the Windows API DLLs, which wrap system calls in convenient functions.
  • System services (daemons on Unix-like systems), background programs that handle printing, networking, logging, updates and scheduled tasks.
  • A shell, the program that lets you start other programs: a command-line shell such as Bash or PowerShell, or a graphical shell with a desktop, taskbar and file manager.
  • Utilities and apps: a text editor, settings panel, browser and so on.

The distinction matters for security and reliability. A crash in a utility or even the graphical shell can be recovered by restarting that program. A crash in kernel code usually takes the whole machine down, producing a Windows blue screen or a Unix kernel panic, because there is no higher authority left to clean up.

Step 12 of 14Diagram

What ships in a typical OS

What ships in a typical OSParts diagram: Operating system distribution, Kernel and drivers, System libraries, System services, Shell, Graphical interface, Utilities.OperatingsystemdistributionKernel anddriversSystemlibrariesSystemservicesShellGraphicalinterfaceUtilities

Tap a shape with to see more.

Operating system distribution
Kernel and drivers

The only part that runs in kernel mode.

System libraries

Wrap system calls: libc, Windows API DLLs.

System services

Daemons for networking, logging, printing, updates.

Shell

A command interpreter such as bash, zsh or PowerShell.

Graphical interface

Desktop, windows, a file manager.

Utilities

Tools like ls, ps, a text editor, package manager.

Step 13 of 14Practice · Match the pairs

Match each component to its description.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Kernel
  2. System library
  3. Daemon or service
  4. Shell
  5. Device driver

Step 14 of 14Practice · What would you do?

What would you do?

A student writes a C program that tries to read a disk sector directly by sending commands to the disk controller's I/O port. When run as a normal user on Linux, the program is terminated immediately. Why?