Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    User Mode, Kernel Mode and System Calls

    The CPU itself enforces the locked doors. It has (at least) two modes:

    Chapter 2 of 125 min read

    The Plain Picture#

    In the hotel, a guest can do lots of things alone: read, watch TV, eat food they already have. But for anything that touches the building — "open the gym", "make a phone call", "get a new room key" — they must call the front desk. The desk checks if they're allowed, does the job, and hangs up.

    Guests physically cannot do these things themselves — the doors are locked.

    The Real Names#

    • The CPU itself enforces the locked doors. It has (at least) two modes:

      • User mode — normal programs run here. Instructions that touch hardware directly are forbidden. If a program tries, the CPU stops it and hands control to the kernel.

      • Kernel mode — only kernel code runs here, with full access to everything.

    • A system call (syscall) is the "call to the front desk": a special instruction that switches the CPU into kernel mode, runs a specific kernel function, and switches back.

    Under the Hood#

    Common system calls (Linux names; macOS is very similar):

    System callWhat it asks for
    open"Give me access to this file"
    read / write"Read/write these bytes from/to this file or connection"
    close"I'm done with this file"
    fork / execve"Make a copy of me" / "Replace me with this other program"
    mmap"Give me more memory"
    socket, connect, acceptnetwork connections (Networking 2: TCP, UDP and Ports)
    exit"I'm finished"

    When StudyBuddy saves Hi 😀 (How Computers Work Ch 9), the app doesn't write to the SSD. It makes system calls:

      StudyBuddy (user mode)                      Kernel (kernel mode)
      ──────────────────────                      ────────────────────
      open("notes.txt")        ── syscall ──▶     check permission, find the file,
                               ◀── returns 3 ──   give back a handle number: 3
      write(3, 7 bytes)        ── syscall ──▶     copy bytes, later ask SSD driver to store them
                               ◀── returns 7 ──   "7 bytes written"
      close(3)                 ── syscall ──▶     release the handle

    You never write these calls yourself in Java or Python. Files.writeString(...) in Java or open(...).write(...) in Python are library functions that make these system calls for you.

    Cost: switching into kernel mode and back is much slower than a normal function call — roughly hundreds of nanoseconds instead of about 1 nanosecond. That's why libraries buffer writes: they collect many small writes in memory and send them in one system call (Ch 10).

    You can watch system calls happen: on Linux, strace python3 -c "print('hi')" prints every call the program makes. (On macOS the equivalent tool, dtruss, needs extra permissions.)

    Gotchas

    • "Segmentation fault" / "access violation" = a program tried to touch memory it doesn't own, the CPU refused, and the kernel killed it. It's the locked door working as intended.

    • Many small writes are slow, not because the disk is slow but because each is a system call. Buffering fixes it.

    Check yourself

    3 questions
    1

    What's the difference between user mode and kernel mode?

    Show answer

    User mode can't touch hardware or others' memory directly; kernel mode can do everything. The CPU enforces the difference.

    2

    What is a system call? Give two examples.

    Show answer

    A request from a program to the kernel that switches into kernel mode to do something privileged — e.g. open, read, write, exit.

    3

    Why do libraries collect small writes into one big write?

    Show answer

    Each system call has a fixed cost; one big write costs far less than thousands of tiny ones.