Part 8 of 8 · 3 min read
Exercises
Easy
Easy 1: First five minutes (20 minutes)
A process shows 0% processor use, and its threads show futex_do_wait in wchan. List the three checks of section 1 and say what each result means.
Done when: three checks, each with a conclusion.
Hint
Processor use separates a deadlock from a livelock.
Easy 2: Find the cycle (20 minutes)
Thread 10 holds lock A and waits for B. Thread 11 holds B and waits for C. Thread 12 holds C and waits for A. Write the waits_for array and say which threads form the cycle.
Done when: an array with three entries, and all three threads named.
Hint
Index the threads from 0: the array is 1, 2, 0 when each waits for the next thread.
Easy 3: Address to account (20 minutes)
A structure has 48 bytes. The stuck threads wait at acc+96 and acc+192. Which accounts do they wait for?
Done when: accounts 2 and 4.
Hint
Divide the offset by the size of one structure and add 1 if you number from 1.
Medium
Medium 1: Reproduce the freeze (45 minutes)
Build the transfer program with no lock order, 4 accounts, 4 threads, and a 1 s timeout on the second lock. Run it five times and report when it got stuck.
Done when: five lines of "stuck after N transfers".
Hint
A watchdog that calls pthread_mutex_timedlock with a 1 s limit is enough.
Medium 2: Get the thread dump (60 minutes)
Run the program under gdb with an alarm after 2 s, print the owner of each mutex, and fill in a table like the one in section 3.
Done when: a table with two threads, the locks they hold and wait for, and the owner of each.
Hint
Use handle SIGALRM stop nopass so that the debugger stops instead of ending the program.
Medium 3: Run the sanitizer (45 minutes)
Compile the unordered program with -fsanitize=thread. Use plain pthread_mutex_lock for both locks, and count the lock-order reports in three runs.
Done when: three counts and the first report copied with its two lines.
Hint
The data-race reports of your monitoring code are separate; count only lock-order-inversion.
Hard
Hard 1: Fix and prove (60 minutes)
Apply lock ordering with the a == b check. Run five stress runs of 3 s, print the total money, and run TSan three times.
Done when: a table with before and after, as in section 5.
Hint
Remember to keep the same random seeds for the before and after runs.
Hard 2: Three locks (90 minutes)
Write a program with a Customer, an Order and an Inventory lock. Two functions take them in different orders. Reproduce the freeze, take the thread dump, find the cycle, and fix it by one global order.
Done when: a freeze, a thread dump table, a fixed run of 10 s, and a TSan run with no lock-order reports.
Hint
Choose the order Customer, then Order, then Inventory, and write it in a comment.
Hard 3: Write the postmortem (60 minutes)
Write a postmortem in the format of section 6 for the program of Hard 2, with a real number in each of the Evidence and Proof rows.
Done when: a seven-row table, every row filled in, with measured numbers.
Hint
Use the numbers you measured, not the numbers of this page.