Module 14 · From Scratch · Systems from scratch · about 16 min to read
Continue where you left off ×
A kernel that boots
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.
The idea in 30 seconds
A PC runs the first 512 bytes of a disk, if the last two bytes are 0x55 and 0xAA.
A kernel can print with no screen driver, by writing bytes to the serial port.
The timer chip rings the CPU through the interrupt vector table, and the handler must say end of interrupt or it rings once.
A context switch saves 22 bytes of state for one task, loads the same for another, and ends in iret.
A scheduler is the rule for who goes next. The simplest one gives each task one tick in a circle.
Preemption means the timer, not the task, decides when a turn ends.
Read the story
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 plans Already 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.
Review From Scratch
Mark as read Saved in this browser, and to your account when you sign in.