Containers · Course contents
Machine not started
30 min·Intermediate

Be responsible for PID 1

Create a PID namespace and reap an orphan adopted by its init process.

Jump to code and terminal ↓

PID 1 has a job

When a parent exits before its child, Linux reparents the orphan to the namespace init. A container init must wait for these children. Otherwise their exit records remain as zombies until init exits.

The new namespace begins at fork

unshare(CLONE_NEWPID) changes the namespace for future children, not the caller. The following fork creates PID 1. The supplied worker forks once and exits with 17; its orphan later exits with 23.

Your change

Create the namespace before the first fork, then replace the single-child wait with a waitpid(-1) loop. Keep collecting children until ECHILD, and retry EINTR. The test verifies both independent exit statuses.

Your goal

Make the namespace init become PID 1 and collect the exit status of both its child and an adopted grandchild.

What the tests observe

  • Real PID namespace
  • Orphan adoption
  • Two exit statuses reaped

Need a nudge?

Hint 1

CLONE_NEWPID is a flag to unshare(); the next fork enters the new PID namespace.

Hint 2

waitpid(-1, &status, 0) collects any child. Linux may adopt a new child while you are waiting.

Hint 3

Use WIFEXITED and WEXITSTATUS; return only after waitpid reports ECHILD.

What happens inside Linux

PID 1 is a process with lifecycle responsibilities, not merely a smaller number. Linux delivered the orphan to your init, and your wait loop released both exit records. Production init also needs deliberate signal forwarding and shutdown behavior.

Optional account sync. Exercises work without an account.

C/work/init.cLoading local work…
Restoring your workspace…
⌘ / Ctrl ↵
TerminalRISC-V 64 · root · /work

Your Linux machine is ready to start.

Boot a disposable environment in this browser. You’ll get a real root shell, with no remote server or account required.

Isolation checks

Observed inside Linux

Run tests to inspect namespace identities, process visibility, and the hostname. Each check reports the behavior your program produced.