Part 7 of 7 · 3 min read
Exercises
Easy
Easy 1: Choose the flag (20 minutes)
Say which of MAP_SHARED and MAP_PRIVATE fits each job: (a) edit a file in place by memory writes; (b) read a data file that other programs also read; (c) give a child its own scratch copy of data that starts from a file.
Done when: (a) shared (with PROT_WRITE); (b) either, private is enough for read-only; (c) private.
Hint
Both sharing and writing need MAP_SHARED.
Easy 2: Is the offset legal (20 minutes)
With 4,096-byte pages, say which offsets are legal for mmap: 0, 100, 4096, 6000, 8192.
Done when: 0, 4096 and 8192 are legal, and 100 and 6000 fail with EINVAL.
Hint
Test whether the offset divides by 4,096.
Easy 3: Past the end (20 minutes)
A file has 5,000 bytes and three pages are mapped. What happens for the byte indexes 4,999, 5,000, 8,191 and 8,192?
Done when: the letter of the file; 0; 0 (the tail of the second page, which still holds part of the file); SIGBUS.
Hint
The second page of the file holds bytes 4,096 to 4,999.
Medium
Medium 1: Count lines (45 minutes)
Write the program of section 1, and run it on a file made with seq 1 100000. Check it against wc -l. Make it print an error for an empty file.
Done when: 100,000 lines in 588,895 bytes, equal to wc, and a message for the empty file.
Hint
Use fstat to get the size, and test it before mmap.
Medium 2: Shared and private (60 minutes)
Reproduce the table of section 2 on a small file of your own. Add a third case in which the file is opened O_RDONLY and mapped with PROT_WRITE and MAP_SHARED.
Done when: the results of section 2, and mmap failing with "Permission denied" for the third case.
Hint
The open mode and the protection bits must agree.
Medium 3: Edit a file in place (60 minutes)
Write a program that maps a text file with MAP_SHARED and changes every lowercase letter to uppercase. Check the result with cat. Repeat with MAP_PRIVATE.
Done when: the file is changed after the shared run and unchanged after the private run.
Hint
Use toupper on each byte.
Hard
Hard 1: Read against mmap (90 minutes)
Make a 256 MiB file, read it once to warm the cache, and time a sum of its 8-byte words with read and with mmap. Run each three times.
Done when: a table of six timings, with the two methods within about 20% of each other on a warm cache, and a sentence on the reasons.
Hint
Use clock_gettime, and read the file once with cat before the first run.
Hard 2: Random access (90 minutes)
On the same file, read one byte from each page in a random order with pread and with mmap. Add a second mmap pass.
Done when: a table of time per page for the three cases, with mmap first touch faster than pread and the second pass much faster still (on the test machine 143.5, 484.5 and 14.5 ns).
Hint
Shuffle the page numbers first, and time only the access loop.
Hard 3: Shorten a mapped file (90 minutes)
Map a file of 8,192 bytes. From the program, or a second terminal, shorten it to 4,096 bytes with truncate. Read a byte of the second page and catch the signal.
Done when: the program catches SIGBUS, and prints a message from its handler, which ends the process.
Hint
Use sigsetjmp and siglongjmp, or _exit in the handler.