Part 7 of 7 · 2 min read
Exercises
Easy
Easy 1: Decode (20 minutes)
Decode exit statuses 130, 137, 139 and 143.
Done when: SIGINT, SIGKILL, SIGSEGV, SIGTERM.
Hint
subtract 128.
Easy 2: Catchable (20 minutes)
Which of SIGINT, SIGKILL, SIGTERM and SIGSTOP can a program catch?
Done when: SIGINT and SIGTERM.
Hint
the two that cannot are named in section 1.
Easy 3: Which signal (20 minutes)
Which signal does the out-of-memory killer send, and which does Ctrl-C send?
Done when: SIGKILL and SIGINT.
Hint
see the table of section 1.
Medium
Medium 1: Run the shutdown test (45 minutes)
Compile graceful. Stop it with SIGTERM, SIGKILL and SIGINT in turn. Report the output and the exit status each time.
Done when: three results matching section 2: 0, 137, 130.
Hint
use $! for the process ID of the last background job.
Medium 2: Add SIGINT (45 minutes)
Make the program also catch SIGINT with the same handler. Test with Ctrl-C and with kill -INT.
Done when: the clean message and status 0 for both signals.
Hint
a second sigaction call with SIGINT.
Medium 3: Debug the crash (45 minutes)
Compile crash.c with -g -O0, run it under gdb, and fix the bug. Show that it prints sum = 6.
Done when: a loop condition while (n), and the output sum = 6.
Hint
change while (1) to while (n), and return the total after the loop.
Hard
Hard 1: Core file (90 minutes)
Enable core dumps, crash the program, and read the core with gdb. Print the value of total in frame 0 and explain it.
Done when: total equal to 6 in frame 0, since three nodes were added before the fault.
Hint
frame 0 then print total.
Hard 2: Find a different crash (90 minutes)
Write a program that overflows a stack buffer with strcpy (Level 10, page 5), compile it with and without -fstack-protector-all, and compare the signals and exit statuses.
Done when: SIGSEGV (139) without the canary and SIGABRT (134) with it.
Hint
strcpy into an 8-byte buffer with a 48-byte string.
Hard 3: A service stop (90 minutes)
Write a program that receives SIGTERM, finishes the current work item (simulated by a 2 s sleep), writes a file with the count of finished items, and exits 0. Test that SIGKILL leaves the file missing.
Done when: a count file after SIGTERM, and no file after SIGKILL.
Hint
check a flag after each work item.