Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Order the layers (20 minutes)
Put in order: block layer, system call, device driver, file system, page cache, controller.
Done when: system call, file system, page cache, block layer, device driver, controller.
Hint
the call goes down the layers, and the controller is last.
Easy 2: Which kind (20 minutes)
Say if each is a block or a character device: a hard disk, a keyboard, /dev/null, a USB flash drive.
Done when: block, character, character, block.
Hint
think of "can I ask for block number N", which a keyboard cannot.
Easy 3: Speed-up (20 minutes)
A cold read took 0.374 s and a warm one 0.146 s. Find the speed-up.
Done when: 2.56 (about 2.6).
Hint
divide the cold time by the warm time.
Medium
Medium 1: Read the listing (45 minutes)
Run ls -l /dev on your machine. Find one block device and two character devices, and report their major and minor numbers. Look up each major number in /proc/devices.
Done when: three lines with kind, major, minor and the driver name from /proc/devices.
Hint
the kind is the first letter of the line. The numbers are the two before the date.
Medium 2: Run the cache test (45 minutes)
Create a 1 GiB file with dd if=/dev/urandom of=big bs=1M count=1024, compile the program of section 4 and run it twice.
Done when: six timings, and a sentence that says whether the warm read is quicker and by how much.
Hint
if the first run is very slow, ignore it and note why.
Medium 3: Non-blocking and a loop (45 minutes)
Extend the program of section 5. Fork a child that sleeps for one second and writes "late" to the pipe. In the parent, poll the non-blocking read every 100 ms and count how many times it returns EAGAIN.
Done when: a count of about 10 and the word "late" printed once.
Hint
use usleep(100000) in a loop and count the failures.
Hard
Hard 1: Write-back risk (90 minutes)
Write a program that writes 100 MB to a file with ordinary write calls and measures the time. Then repeat with fsync after every 1 MB. Report both times and explain the difference.
Done when: two times, the second larger, and a paragraph on what fsync forces and why a crash after the first run could lose data.
Hint
use fsync(fd) after each megabyte. Time with clock_gettime.
Hard 2: Does O_DIRECT help (90 minutes)
Read the file of Medium 2 in requests of 4 KiB with O_DIRECT and without, both with a cold cache. Compare the times and explain why the buffered read can win or lose.
Done when: a table of two times, and an explanation that mentions read-ahead, system call count and the copy from the page cache.
Hint
use posix_fadvise with POSIX_FADV_DONTNEED before each run.
Hard 3: A tiny server (90 minutes)
Write a program that makes two pipes, sets both read ends to non-blocking, and uses poll to wait until either has data. Write to each pipe from a child at different times and print which one was ready.
Done when: the program prints the ready pipe each time, and uses poll and no busy loop.
Hint
call poll with two struct pollfd entries and test revents.