Part 8 of 8 · 3 min read
Exercises
Easy
Easy 1: Sort the events (20 minutes)
Classify each as interrupt, system call or exception: a key is pressed; a program calls open(); a program reads address 0; the timer expires.
Done when: interrupt, system call, exception, interrupt.
Hint
Only the program's own instruction makes a system call or exception.
Easy 2: Handler cost (20 minutes)
A handler costs 8 microseconds. 5,000 interrupts arrive each second. Give the share of the processor used.
Done when: you write 40,000 µs, or 4 per cent.
Hint
5,000 × 8 µs, divided by 1,000,000 µs.
Easy 3: Timer rate (20 minutes)
With HZ = 1,000, give the time between ticks. With HZ = 100, how many ticks occur in 20 seconds?
Done when: you write 1 millisecond and 2,000.
Hint
Divide 1 second by HZ, and multiply HZ by 20.
Medium
Medium 1: Polling or interrupts (30 minutes)
A device produces 50,000 events a second. An interrupt costs 6 µs. A poll costs 1.5 µs and runs every 100 µs. Give the processor share of each and say which to use.
Done when: you write 30 per cent for interrupts and 1.5 per cent for polling, and choose polling.
Hint
Interrupts 50,000 × 6 µs. Polling 10,000 polls a second × 1.5 µs.
Medium 2: Run the timer demo (30 minutes)
Compile the timer program and change the period to 250 ms. Predict the run time for 10 ticks, then measure it with time.
Done when: a prediction of 2.5 seconds and a measurement close to it.
Hint
10 ticks of 250 ms.
Medium 3: Read the table (30 minutes)
Run cat /proc/interrupts before and after typing in a terminal or copying a file. Find the row that grew and say which device it is.
Done when: two snapshots, the changed row, and a guess at the device.
Hint
The eth or virtio rows grow with network traffic.
Hard
Hard 1: Trace a read (60 minutes)
Write a numbered trace of a program calling read() on a file, naming the system call, the disk interrupt, the handler, the scheduler and the return. Mark each step user or kernel mode.
Done when: a trace of at least 8 steps with a mode label on each.
Hint
Alternate between user mode and kernel mode at each crossing.
Hard 2: Lost interrupts (60 minutes)
A device raises an interrupt every 2 milliseconds, at 2, 4, 6, 8 and 10 ms, and the hardware holds only one pending request. The kernel keeps interrupts disabled from 3 ms to 7 ms. Show on a time line which events are served at once, which wait, and which are lost.
Done when: a time line from 0 to 10 ms with the events at 2, 8 and 10 served at once, the event at 4 held and served at 7, and the event at 6 lost.
Hint
Events during the 5 ms disabled period compete for one pending slot.
Hard 3: Handle SIGSEGV (60 minutes)
Write a C program that installs a handler for SIGSEGV, writes through a null pointer, and prints a message from the handler before exiting.
Done when: the program prints its message and exits with a status of your choosing, with a note on why the handler must not simply return.
Hint
Returning from a fault handler re-runs the faulty instruction.