Part 6 of 6 · 3 min read
Exercises
Easy
Easy 1: Name the signal (20 minutes)
Give the signal for: Ctrl+C, a bad memory access, a child ending, a timer from alarm, and the one that cannot be caught and ends a process.
Done when: SIGINT, SIGSEGV, SIGCHLD, SIGALRM and SIGKILL.
Hint
kill -l prints the list.
Easy 2: Which can be caught (20 minutes)
Of SIGTERM, SIGKILL, SIGINT, SIGSTOP and SIGUSR1, say which cannot be caught.
Done when: SIGKILL and SIGSTOP.
Hint
The kernel keeps two for itself.
Easy 3: Pick the type (20 minutes)
A handler sets a flag that the main loop reads. Write the declaration of the flag and explain each of the two type words.
Done when: static volatile sig_atomic_t flag; with one sentence for volatile and one for sig_atomic_t.
Hint
volatile prevents caching in a register, and sig_atomic_t is read and written in one piece.
Medium
Medium 1: Ignore Ctrl+C (30 minutes)
Write a program that ignores SIGINT with SIG_IGN and then loops, printing a dot each second. Press Ctrl+C and show that it continues, and stop it with kill -TERM from another terminal.
Done when: dots keep appearing after Ctrl+C, and the process ends after SIGTERM.
Hint
signal(SIGINT, SIG_IGN) or sigaction with SIG_IGN.
Medium 2: A graceful stop (30 minutes)
Write a loop that works until a SIGTERM handler sets a flag, then prints "cleaning up" and exits with status 0. Send it SIGTERM from a shell.
Done when: the output contains "cleaning up" and echo $? after it prints 0.
Hint
The loop condition is while (!flag).
Medium 3: Report how a child died (30 minutes)
Fork three children: one exits with 4, one calls abort(), one waits with pause() and is killed with SIGTERM by the parent. Print for each whether it exited or was signalled and which number.
Done when: three lines such as "exited 4", "killed by 6" and "killed by 15".
Hint
Use WIFEXITED, WEXITSTATUS, WIFSIGNALED and WTERMSIG.
Hard
Hard 1: Count signals (60 minutes)
Write a program that counts SIGUSR1 signals in a handler and prints the total after 5 seconds. Send 1,000 signals from a shell loop as fast as possible and compare the count with 1,000. Explain any difference.
Done when: a count below 1,000 and an explanation that standard signals of the same kind merge while one is pending.
Hint
Signals of the same number are not queued.
Hard 2: Timeout with alarm (60 minutes)
Write a program that reads a line from the keyboard with fgets, but gives up after 3 seconds using alarm and a handler. Handle the interrupted read properly.
Done when: typing within 3 seconds prints the line and waiting prints "timeout", with no crash.
Hint
fgets returns NULL when interrupted; without SA_RESTART the read is not restarted.
Hard 3: Add Ctrl+C handling to the shell (60 minutes)
Change the shell from page 4 so that Ctrl+C does not end the shell but ends the running child. Test with sleep 100.
Done when: Ctrl+C stops sleep and the shell prints the next prompt. Hint on the design: the shell ignores SIGINT while the child restores the default action before exec.
Hint
In the child, signal(SIGINT, SIG_DFL) before execvp.