Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 14 · From Scratch · Systems from scratch · about 16 min to read

Play it as a journeyTutor comes with a plan

A kernel that boots

Processes Threads and the Kernel
Processes Threads and the Kernel · Open board

: the kernel handing out turns on a core to many programs. This lesson builds the smallest version of that picture: a program that starts with no operating system under it, prints, takes a timer interrupt, and gives two tasks turns.

Before you start. A machine starts by running the first sector of a disk, and each later stage is loaded by the one before it (From power button to PID 1: how a machine boots). An interrupt is a signal from a device that makes the CPU stop and run a handler (I/O, Interrupts & the Bus). The kernel hands out turns on the CPU, and the scheduler is the part that picks who goes next (Processes, Threads & the Kernel, Whose turn, and who decides). Our last build, A file system of your own, ran on top of an operating system, and this one has nothing under it.

Lesson 117 of 117 in From Scratch

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • 1991: two programs that print A and B
  • The boot sector: 512 bytes the machine runs without being asked
  • The timer interrupt: a bell that rings 100 times a second
  • The context switch: save one task, load the other
  • The run: ten ticks and a clean exit
  • What the model leaves out
  • The program
  • Build and run
  • Break it: forget the interrupt flag
  • Break it: forget end of interrupt
  • Trade-offs and what real kernels add

Terms it explains: boot sector, interrupt vector table, context switch, scheduler, preemption, fork/exec, init/systemd, kernel, MMU + page table, namespaces + cgroups, page (~4KB) / frame, page fault, PID, process, segfault, swap, syscall, thread, user vs kernel mode (ring 3/0), virtual memory.

Lab

The program needs twelve pages at once. What happens when it gets eleven frames?

Drag Frames of real memory from 24 down to 11, then back to 12, and watch Time spent working.

Open The working set

Three long jobs are running when you press a key. How long until the keypress gets the CPU?

Click Take turns, drag How long one turn lasts from 100 ms down to 1 ms, and watch The keypress waits.

Open The time slice

How many workers should a pool have, and why does the answer flip when the tasks mostly wait?

Drag Workers on shift down to the core count, then push Share of each task spent waiting to 90% and watch Finished each second.

Open The thread pool

Related papers

Review From Scratch