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 06 · How Computers Work · Build the machine's software · about 17 min to read

Play it as a journeyTutor comes with a plan

Birth and death of a process: zombies, orphans and PID 1

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

The kernel and the programs it runs. This lesson follows one program from the moment it is made to the moment the kernel finally forgets it.

On 20 January 2015, Hongli Lai of the company Phusion published a post called “Docker and the PID 1 zombie reaping problem”. His example was a web server in a container. It runs a small script, and the script runs a search program. The server stops the script after a timeout, but the search program keeps going. When it finishes, nobody collects its result. It stays in the system as a dead entry that never leaves. Do that thousands of times and the machine cannot start any new program.

Nothing crashed and no error was shown. A program ended, and the system waited for someone to say “I have seen that it ended”.

By the end you will be able to follow any process from birth to death, say who must act at each step, and explain why PID 1 has a job that no other process has.

The history (optional)

Phusion’s post said that Docker did not run containers under an init program that waits for adopted children, so a container’s first program had to do it. Docker 1.13 added the --init option, which puts a small program called tini in as PID 1, as Linux and the Shell: Processes, Files, Pipes, Permissions and Signals shows. The Linux manual says the call that lets an ordinary program adopt orphans, PR_SET_CHILD_SUBREAPER, has existed since Linux 3.4. The second read uses it.

Before you start. The kernel is the one program that makes processes, and each process has its own id number (Processes, Threads & the Kernel). A shell makes a child with fork, replaces the child with a new program using exec, and then waits for it (The program that starts programs). The first process of a machine is PID 1, which the boot chain starts last (From power button to PID 1: how a machine boots). A first look at a zombie came when signals were introduced (The tap on the shoulder). This lesson follows the whole life.

Lesson 37 of 41 in How Computers Work

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

  • Birth: fork gives the child a number and a parent
  • Death: an exit status is a number the parent can collect
  • Zombie: a process that has ended but has not been waited for
  • Orphan: a running process whose parent has ended
  • Reaper: the process that waits for children it did not make
  • What goes wrong in a container
  • The whole life in one picture
  • What the table row holds
  • The four scenes in code
  • The subreaper: a reaper that is not PID 1
  • Why PID 1 is special in four more ways
  • Double fork: orphaning on purpose
  • Trade-offs

Terms it explains: PID, parent, exit status, zombie, orphan, reaping, reaper, SIGCHLD, subreaper, context switch, fork/exec, init/systemd, kernel, MMU + page table, namespaces + cgroups, page (~4KB) / frame, page fault, 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 How Computers Work