Part 8 of 8 · 3 min read
Exercises
Easy
Easy 1: Modes (20 minutes)
List three privileged instructions (by purpose) and say what the hardware does if user code tries one.
Done when: three purposes (such as I/O control, timer, interrupt control) and the trap behaviour.
Hint
Think of what devices, the timer and interrupts need.
Easy 2: Timer arithmetic (20 minutes)
With HZ = 1,000, give the time between timer interrupts. With HZ = 100, how many interrupts happen in 5 seconds?
Done when: you write 1 millisecond and 500.
Hint
Divide 1 second by HZ, then multiply HZ by 5.
Easy 3: Chunk count (20 minutes)
A 300-byte file is copied with a 128-byte buffer. Give the number of read and write calls, including the final read.
Done when: you write 4 reads and 3 writes (128 + 128 + 44, then the end).
Hint
Divide 300 by 128 and round up, then add the empty read.
Medium
Medium 1: Run strace (30 minutes)
Compile the copy program, create a 200-byte in.txt, and run strace ./copy. Find the read and write lines.
Done when: you list the five read lines and four write lines with their return values.
Hint
Use strace ./copy 2>&1 | grep -E "read\(3|write\(4".
Medium 2: Handle errors (30 minutes)
Write a C program that opens a file given on the command line, prints a clear message with strerror if it fails, and otherwise prints its size using lseek.
Done when: it prints a message for a missing file and a size for an existing one.
Hint
lseek(fd, 0, SEEK_END) returns the file size.
Medium 3: Buffering cost (30 minutes)
A program writes 2,000,000 bytes. Compare the number of write calls for a 1-byte, a 512-byte and a 4,096-byte buffer. At 200 ns per call (made up), give the time in calls for each.
Done when: 2,000,000, 3,907 and 489 calls, and 0.4 s, about 0.78 ms and about 0.098 ms.
Hint
2,000,000 / 512 is 3,906.25, so 3,907 calls.
Hard
Hard 1: Explain a call to a friend (60 minutes)
Write a one-page explanation of what happens between write(1, "hi", 2) and the text appearing on screen. Use the eight steps and name the mode at each.
Done when: a page with the eight steps and a mode label at each.
Hint
The wrapper runs in user mode, and the service routine in kernel mode.
Hard 2: Count your own calls (60 minutes)
Run strace -c on ls in a small directory and on a program of your own. List the five most frequent calls of each and guess why.
Done when: two tables of five calls with counts and a one-line reason for each.
Hint
Use strace -c ls.
Hard 3: Design the guard (60 minutes)
Suppose a new processor has no mode bit. Describe two things that could go wrong, and what hardware support you would add to fix them.
Done when: two failure scenarios and a proposal that includes a mode bit and privileged instructions.
Hint
A program could turn off the timer or rewrite the OS memory.