Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: What survives (20 minutes)
Say what happens to the data after all processes have ended, for: an anonymous shared mapping; a POSIX shared memory object that was not unlinked; a MAP_SHARED mapping of a file.
Done when: gone; still in /dev/shm until unlinked or reboot; saved in the file (after write-back).
Hint
Think about who holds a reference to the pages.
Easy 2: Why ftruncate (20 minutes)
A program creates a shared memory object, maps one page and writes to it, but forgot ftruncate. What happens, and why?
Done when: the write ends in SIGBUS, since the object has size 0 and the page is wholly past its end (page 4).
Hint
A new object has length 0.
Easy 3: Ring size (20 minutes)
A ring has 32 slots of 8 KiB. Find its size in bytes, and the number of blocks the producer can write ahead of a stopped consumer.
Done when: 262,144 bytes (256 KiB) of data, and 32 blocks.
Hint
32 × 8,192 bytes.
Medium
Medium 1: Reproduce the race (45 minutes)
Write the counter program of section 2. Run it three times. Report the three counters.
Done when: a table in which the plain counter is below 2,000,000 in every run (about 1.95 to 1.96 million on the test machine) and the atomic and mutex counters equal 2,000,000.
Hint
Use atomic_long from <stdatomic.h> and -pthread.
Medium 2: Writer and reader (60 minutes)
Write the two programs of section 3. Run them in this order and list /dev/shm after each step. Run the reader twice.
Done when: the message is printed once, the name is gone after the reader, and the second reader prints a "No such file" error.
Hint
Use ls -l /dev/shm between the runs.
Medium 3: Four processes (60 minutes)
Extend the program of section 2 to four processes that each add 1,000,000 to a counter protected by a process-shared mutex. Time it with and without the mutex.
Done when: 4,000,000 with the mutex, and a smaller number without it, with both times shown.
Hint
Use fork four times from the parent, and wait for all.
Hard
Hard 1: Pipe or ring (90 minutes)
Write the ring benchmark of section 4, and the pipe version. Move 2 GiB in 64 KiB blocks and report both rates, three runs each.
Done when: a table like section 4, with the ring faster on the test machine, and a check that the consumer saw the right total.
Hint
Check the sums of both sides at the end.
Hard 2: A fair test (90 minutes)
Change the pipe version so that the producer fills each block with memset before it writes it, as the ring producer does. Add a version with 4 KiB blocks. Report all rates.
Done when: a table of four rates, and a sentence on how the gap changes with the block size.
Hint
A pipe has a capacity of 64 KiB by default, so 4 KiB blocks need more system calls.
Hard 3: A list that survives remapping (90 minutes)
Build a linked list of 10 nodes inside a shared memory object, using offsets as links. One process builds it, and another process opens the object, maps it at its own address and prints the list. Print the mapping address in both.
Done when: both processes print the same 10 values, and the two mapping addresses differ.
Hint
A node holds a value and the offset of the next node, with 0 as the end.