Chapter 14: Operating-System Design
Multiple Choice · Goals
Question 1: Which Is Not a Core Goal?
The core goals of an operating system are to define abstractions, provide primitive operations, ensure isolation and manage the hardware. Which of the following is not one of these goals?
B: compiling is the job of a toolchain (the compiler); the operating system loads and runs the compiled program. A is hardware management, C is abstraction and D is isolation; primitive operations are the system calls on top of these abstractions.
Multiple Choice · Interface Principles
Question 2: Which Principle?
“A programmer should be able to sense the approximate cost of a system call; a call that looks cheap must not be secretly expensive. A feature that cannot be implemented well is probably not worth adding.” Which principle of interface design does this statement describe?
D: the interface principles are simplicity (few but well-thought-out concepts), completeness (everything necessary must be possible) and efficiency. Example: lseek only changes a number and is cheap, whereas read may need a disk access. Backward compatibility, on the other hand, means published calls cannot be removed; it is a constraint.
Step-by-Step Trace · Event-Driven Code
Question 3: Result of the Event Loop
Events arrive in the application’s queue in this order: KEY CLICK KEY TIMER CLICK KEY KEY QUIT. What does the program print?
The program starts; there are eight events in the queue. The order of the events, not the program, determines the flow.
KEY: n increases by one → 1.
CLICK: n increases by 10 → 11.
KEY: n increases by one → 12.
TIMER: n = 12 > 10, so it is reset to zero.
CLICK: n increases by 10 → 10.
KEY: n increases by one → 11.
KEY: n increases by one → 12.
QUIT: get_message returns 0, the loop ends; 12 is printed. A long-running handler holds up all queued events and the interface freezes.
int n = 0; msg_t m; while (get_message(&m)) { /* QUIT → 0 */ switch (m.type) { case KEY: n++; break; case CLICK: n += 10; break; case TIMER: if (n > 10) n = 0; break; } } printf("%d\n", n);
Multiple Choice · Microkernel
Question 4: The Microkernel Trade-off
A team decides to move the network driver out of the kernel and run it as a separate server process in user space. What is the typical gain and cost?
A: one of the basic trade-offs in operating-system design is between performance and isolation. A bug in a user-space driver does not bring down the whole system (MINIX 3 restarts it); but every packet pays the cost of IPC, context switching and copying.
Multiple Choice · Mechanism and Policy
Question 5: Mechanism or Policy?
Which of the following is a policy (not a mechanism)?
D: a mechanism defines “how it is done”, a policy decides “what is to be done”. The page-replacement rule (LRU, clock…) is a policy; the MMU, context switching and LSM hooks are mechanisms independent of any policy (SELinux rules, in turn, are the policy of the LSM).
Classic Question · Naming
Question 6: Path Lookup Cost
In the call open("/home/ali/courses/notes.txt") every directory fits in a single block. (a) With nothing cached, how many disk reads are needed to reach the file’s i-node? (b) What if the name cache holds the mapping /home/ali → i-node and that i-node is in memory?
Show answer
For each directory, first its i-node (where its data is) and then its directory block (name → i-node number) are read.
(a) root i-node + root block, home i-node + block, ali i-node + block, courses i-node + block, and finally notes.txt’s i-node: 2 + 2 + 2 + 2 + 1 = 9 reads.
(b) The lookup continues from ali: ali’s block, courses i-node, courses block, notes.txt i-node = 4 reads. Even a partial hit is a gain; in Linux this is the dcache’s job. Counting only directory blocks, or forgetting the file’s own i-node, is a common mistake.
Output Question · Static and Dynamic Structures
Question 7: Searching a Static Table
The process table is a fixed array of 6 entries set at compile time; empty entries have pid 0. What does the program print? What happens if the table fills up?
struct proc { int proc_pid; }; struct proc proc_table[6]; /* 1 314 0 0 42 7 */ int find(int pid) { int n = 0; struct proc *p; for (p = proc_table; p < proc_table + 6; p++) { n++; if (p->proc_pid == pid) return n; } return -n; } /* main: */ printf("%d %d %d\n", find(42), find(7), find(99));
Show answer
Output: 5 6 -6
- 42 is in the fifth entry: 5 comparisons. 7 is in the last entry: 6. 99 is absent: the whole table was scanned, −6.
p++advances to the next structure (not the next byte); empty entries are scanned one by one too: O(n).- A static table is simple and needs no memory allocator; but when it is full no new process can be created (NPROC in old UNIX). Dynamic structures (hash table, xarray) remove this limit; the price is an allocator, fragmentation and complex locking.
Output Question · Hiding the Hardware
Question 8: Conditional Compilation
The code is compiled for 64-bit Linux (LP64); what does init() print? Which part changes when porting to a new processor?
#define X86_64 1 #define ARM64 2 #define CPU ARM64 #define WORD_LENGTH 64 #if (WORD_LENGTH == 32) typedef int Register; #elif (WORD_LENGTH == 64) typedef long Register; #endif void init(void) { #if (CPU == X86_64) puts("x86-64 init"); #endif #if (CPU == ARM64) puts("ARM64 init"); #endif printf("%zu\n", sizeof(Register)); }
Show answer
Output: ARM64 init and 8
- The preprocessor lets only the lines whose condition is true through to compilation: since CPU = 2, the x86-64 block is not even compiled.
- WORD_LENGTH 64 →
Register=long; in LP64 long is 8 bytes. (On 64-bit Windows long is 4 bytes: portable code prefersuintptr_t.) - When porting, only
config.hand the hardware-dependent files change (arch/ directories in Linux, the HAL in Windows).#ifs scattered everywhere make the code unreadable.
Multiple Choice · Reentrancy
Question 9: Reentrant?
strtok keeps its position in an internal static char * variable. What happens if two threads tokenize different strings with strtok at the same time?
B: a static variable lives not on the stack but in the data segment shared by all threads. Reentrant code must be able to run twice at the same time (two threads, or an interrupt handler calling the same function): no shared static state; if necessary the state is passed as a parameter or protected by a lock.
Step-by-Step Trace · Space-Time Trade-off
Question 10: The Bit-Counting Loop
What does bit_count(0x9D) return? How many bytes is a table that gives the same result with a single memory read?
0x9D = 1001 1101. In each round the byte is shifted right by i bits and the lowest bit is masked with 1.
(byte >> 0) & 1 = 1: count increases.
(byte >> 1) & 1 = 0: branch not taken.
(byte >> 2) & 1 = 1: count increases.
(byte >> 3) & 1 = 1: count increases.
(byte >> 4) & 1 = 1: count increases.
(byte >> 5) & 1 = 0: branch not taken.
(byte >> 6) & 1 = 0: branch not taken.
(byte >> 7) & 1 = 1: count increases.
The loop ran 8 rounds: it returns 5. A 256-entry char bits[256] table gives the same result with a single memory read: spend 256 B of space to save time.
int bit_count(int byte) { int i, count = 0; for (i = 0; i < 8; i++) if ((byte >> i) & 1) count++; return count; }
Classic Question · Space-Time Trade-off
Question 11: Trade-off with a Color Palette
An 800 × 600 image is stored (a) as 24-bit RGB per pixel, (b) as an 8-bit palette index per pixel plus a 256-color palette (3 bytes per color). Compute both sizes and the ratio. What does (b) sacrifice?
Show answer
(a) 800 × 600 × 3 = 1,440,000 B.
(b) 800 × 600 × 1 + 256 × 3 = 480,000 + 768 = 480,768 B. Ratio 1,440,000 / 480,768 ≈ 3 : 1.
Space drops to about a third; the cost is a table lookup for every pixel drawn (time) and a limit of at most 256 colors. GIF and PNG-8 use this method.
Multiple Choice · Hints
Question 12: Cache versus Hint
In a distributed file system the client keeps the information “this file is probably on server B”; it asks B before relying on it and consults the directory service if it is wrong. Why is this information a hint rather than a cache entry?
C: a value in a cache must always be correct: it is invalidated when the source changes (the cost of consistency). A hint may turn out wrong; its correctness is checked at the time of use with a cheap test. Other examples: branch prediction, the access hint given with madvise.
Classic Question · The Mythical Man-Month
Question 13: Brooks’s Law
Three people are added to a 5-person team on a late project. (a) If there is one communication path between every pair, how many paths are there before and after, and by what factor do they grow? (b) With Brooks’s suggested split (1/3 planning, 1/6 coding, 1/4 module testing, 1/4 system testing), how many months go to each activity in a 12-month project?
Show answer
(a) n(n − 1) / 2: 10 paths with 5 people, 28 with 8; a 2.8× increase (headcount grows only 1.6×). Training the newcomers also consumes the existing team’s time: “Adding manpower to a late software project makes it later.”
(b) Planning 4, coding 2, module testing 3, system testing 3 months. Writing code is one sixth of the work; half the time goes to testing.
Step-by-Step Trace · git bisect
Question 14: Hunting a Regression with git bisect
v1 (commit #0) is good, v2 (#600) is bad; the commits in between are numbered consecutively and the first bad commit is #222. At each step git tests the middle of the range (⌊(good + bad) / 2⌋). Which commits are tested, and how many tests are needed?
The first bad commit is somewhere in 1…600. At each step git checks out the commit in the middle of the candidate range.
#300 is tested: the bug is present, bad. The first bad commit is #300 or earlier. Candidates 600 → 300.
#150 is tested: no bug, good. The first bad commit is after #150. Candidates 300 → 150.
#225 is tested: the bug is present, bad. The first bad commit is #225 or earlier. Candidates 150 → 75.
#187 is tested: no bug, good. The first bad commit is after #187. Candidates 75 → 38.
git bisect run does the rest with a script: #206 good, #215 good, #220 good, #222 bad, #221 good. 9 tests in total: “#222 is the first bad commit”.
reset returns to the original branch. Worst case ⌈log₂ 600⌉ = 10 tests; even 20 for a million commits. The script returns 0 for good and 1–127 (except 125) for bad.
git bisect start git bisect bad v2 # commit #600 git bisect good v1 # commit #0 # build, boot, test: git bisect bad # or: good git bisect run ./test.sh git bisect reset
Multiple Choice · Dynamic Checkers
Question 15: Which Tool Catches What?
Kernel debugging tools are paired with the bugs they catch. Which pairing is wrong?
C: kmemleak finds memory leaks: it reports allocated blocks that no pointer refers to. Lock order is checked by lockdep and data races by KCSAN. KASAN checks every access using shadow memory; the cost is a slowdown of several times, so it is enabled in test kernels.
Classic Question · Debugging Strategy
Question 16: An Occasional Use-After-Free
A driver has an occasional use-after-free bug that disappears when printk is added. Which tools do you use, and in what order? Why is syzkaller run with a KASAN-enabled kernel? If the bug was introduced between v6.11 and v6.12 (12,000 commits), what do you do?
Show answer
- A test kernel with KASAN (and KCSAN) enabled is run in QEMU; the bug is triggered with syzkaller or a stress test.
- The KASAN report arrives at the first invalid access and gives the allocation and free stack traces of the object: its lifetime can be read from them. If necessary, step through it with QEMU + gdb.
- A fuzzer alone sees only crashes; the checker is what makes silent memory corruption visible.
- printk changes the timing (a heisenbug): low-overhead tracers such as ftrace/eBPF are preferred.
- git bisect: ⌈log₂ 12,000⌉ = 14 tests (213 = 8,192 < 12,000 ≤ 16,384).
Multiple Choice · Rust, eBPF and Unikernel
Question 17: Where Do Unikernels Come From?
Unikernels such as MirageOS and Unikraft compile a single application together with the operating-system libraries it needs into one image. Which system structure is this idea the cloud version of?
A: in the exokernel the abstractions live in a library operating system (libOS) linked into the application; a unikernel runs this libOS together with the application in a single address space on a hypervisor. The cost: debugging and multi-application use are hard.