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 13 · From Scratch · Container Runtime · about 16 min to read

Play it as a journeyTutor comes with a plan

A shell of your own

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

: the kernel and the programs it runs. You will write the program in the middle of that picture, the one that starts the other programs and decides which of them the keyboard talks to.

You type sleep 30 | cat and change your mind. You press Ctrl-C. Two programs die, and your shell stays alive and shows a new prompt. Here is a real run of the shell built in this lesson, on Linux 7.0.0 with Python 3.12.3 on 2026-11-28:

$ sleep 30 | cat
^C[1] killed by signal 2
$ echo alive
alive

One key press reached two programs at once and skipped the third, the shell itself. Nobody wrote “kill these two but not me” anywhere in sleep or cat. The shell arranged it before they started. Try Ctrl-Z instead and the same two programs freeze, the shell takes back the keyboard, and one word, fg, wakes them.

By the end you will be able to write a shell of about 120 lines that parses pipelines and redirects, runs each one as a group the terminal can address, and supports Ctrl-C, Ctrl-Z, &, jobs, fg and bg.

The history (optional)

The C shell’s own introduction says that Jim Kulp added the job control and directory stack primitives to it. A discussion on the Unix history mailing list says Kulp did this at IIASA, an institute in Austria, that he did the signal work in the kernel and the shell code, and that Bill Joy folded it into 4.1BSD. A 4.2BSD job control paper by David Lennert of HP says Kulp’s design became a de facto industry standard. Before it, a terminal ran one program at a time and you waited for it.

Before you start. A shell is a loop that forks a child, replaces it with the command (exec) and waits, and a pipe is a one-way channel whose reader sees end of input only after every copy of the writing end is closed (The program that starts programs). A signal is a small numbered note the kernel delivers to a process, and Ctrl-C makes one (The tap on the shoulder). A child that ends keeps its exit status until its parent calls wait (Birth and death of a process: zombies, orphans and PID 1). Every process holds numbered doors called file descriptors (Everything is a file descriptor). The earlier shell ignored Ctrl-C. This one fixes the cause.

Lesson 102 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

  • The loop: read a line, split it, run it
  • A process group is a set of processes that get one signal together
  • The foreground process group is the one group the keyboard talks to
  • A job is one command line, and a stopped job is frozen, not dead
  • A background read stops the job, using SIGTTIN

Terms it explains: process group, foreground process group, job, stopped, context switch, fork/exec, init/systemd, kernel, MMU + page table, namespaces + cgroups, page (~4KB) / frame, page fault, PID, preemption, process, scheduler, 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