CSE Roadmap

Core tier · about 3 hours · 6 parts

Part 6 of 6 · 3 min read

Exercises

Easy

Easy 1: Zombie or orphan (20 minutes)

Say whether the child is a zombie, an orphan, or neither: (a) the child ended and the parent has not waited; (b) the parent ended and the child still runs; (c) the child ended and the parent waited.

Done when: zombie, orphan and neither.

Hint

A zombie has ended; an orphan is still running.

Easy 2: PID table (20 minutes)

The system allows 32,768 PIDs and a leaking server has left 31,000 zombies. How many PIDs are left, and what happens at 0?

Done when: 1,768 left, and fork fails.

Hint

32,768 − 31,000.

Easy 3: Order the daemon steps (20 minutes)

Put in order: second fork, change to /, first fork, setsid, redirect to /dev/null.

Done when: first fork, setsid, second fork, change to /, redirect to /dev/null.

Hint

The second fork comes after the new session.

Medium

Medium 1: Make zombies (30 minutes)

Fork five children that exit at once and make the parent sleep for 10 seconds. In a second terminal, count them with ps --ppid PARENT -o stat=. Then let the parent call wait five times and count again.

Done when: 5 zombies before, 0 after.

Hint

Zombies show Z in the STAT column.

Medium 2: Watch adoption (30 minutes)

Run the orphan program, but print getppid() every 200 ms for 1.5 seconds in the child. Note when the value changes.

Done when: a list of PPID values that switches from the parent's PID to 1 (or to the reaper on your system), and a sentence saying the switch happens when the parent ends.

Hint

Use usleep(200000) in a loop.

Medium 3: Reap with a handler (30 minutes)

Write a program that forks 10 children with random short lives and reaps them only in a SIGCHLD handler. At the end, check that there are no zombies.

Done when: the program counts 10 reaped children and ps shows none left.

Hint

Remember the loop around waitpid in the handler.

Hard

Hard 1: A tiny daemon (60 minutes)

Write a daemon that, every 2 seconds, appends the time to /tmp/daemon.log, and stops cleanly on SIGTERM. Start it from a shell, close the shell, and check that it still runs with ps.

Done when: the log keeps growing after the shell is closed, and kill PID stops it with a final line in the log.

Hint

Use a flag set by the SIGTERM handler, and sleep(2) in the loop.

Hard 2: Why the second fork (60 minutes)

Read man 2 setsid and the part of the manual on controlling terminals. Write one paragraph that explains what could go wrong if the daemon skipped its second fork, and draw the process, session and terminal relationships before and after each fork in the recipe.

Done when: a paragraph that says a session leader without a terminal can acquire one when it opens a terminal device, a diagram with the parent, session leader and daemon, and one sentence on why the survivor is not a session leader.

Hint

Only a session leader can acquire a controlling terminal by opening one.

Hard 3: Count a process tree (60 minutes)

Write a program that forks a binary tree of depth 3 (each process forks two children, down to depth 3), has every process print its PID and PPID, and have every parent wait for its children. Draw the tree and count the processes.

Done when: 15 processes (1 + 2 + 4 + 8), a drawn tree matching the PID and PPID lines, and no zombies.

Hint

The sum is 2⁴ − 1 for depth 3.