
Recall first from The written exam and timed practical
- Written answers earn credit by naming the relevant invariant and predicting a concrete result.
- A page-table edit and synchronization of cached translations are separate operations.
- A panic location identifies where a failure was detected and can differ from where the bad state was created.
Answer the same question twice
4 min readThe last lesson was the timed assessment. Now you answer the same question once as an xv6 answer and once as a Rust answer, defend the Rust one, and map yourself against the rubric.
The Linux kernel's spinlock documentation describes a problem that makes a good interview question. If ordinary kernel code holds a spinlock and a local interrupt handler tries to acquire it, the interrupted code cannot resume to release it. The same problem applies when you design a small teaching kernel. Linux kernel locking lessons
Use this prompt for both answers. An interrupt handler adds bytes to a bounded receive queue. A kernel task removes them. On a multicore system, how do you protect the queue, and what happens when it is full or empty?
State your assumptions before naming a primitive. Here, one shared queue has preallocated storage. The handler drops an incoming byte if the queue is full, and the task can try again later if the queue is empty. Neither operation waits while holding the lock. The a condition that must hold whenever other code can observe the data structure is that the indices and occupancy describe the same set of initialized, unread bytes. A consumer must never observe an entry as ready before its byte has been stored. Keep those rules fixed when you switch languages.
C/xv6 answer. “In RISC-V xv6, I would protect the array, indices, and occupancy with one spinlock. Both enqueue and dequeue take that lock before checking whether they can proceed. The handler stores the byte and updates the queue if there is space. The task checks for a byte, copies it out, and updates the queue before releasing the lock. Each exit path releases the lock. xv6's spinlock acquisition disables interrupts on the current hart before it attempts to take the lock, and its release path handles restoring the earlier interrupt state when the outermost interrupt-disable scope ends. If another hart arrives during the update, it waits for the lock.”
Rust answer. “I would put the same queue behind an interrupt-safe spin mutex in the freestanding kernel. Its safe enqueue and dequeue methods acquire a guard and perform the entire update under that guard. Dequeue returns the byte by value, so the caller has no reference into the queue after the guard is dropped. The mutex masks local interrupts before acquisition, and the guard releases the lock before the interrupt state can be restored. Nested acquisitions of distinct locks must preserve the outer interrupt mask. Full and empty have the same behavior as in the C answer.”
Defend the Rust design. Name a specific error the interface makes harder to write. For example, if the queue fields are private and access to the protected queue is tied to the guard's borrow, safe callers cannot retain a mutable reference to a slot after unlocking. The guard also releases the lock on ordinary early returns. C can implement the same protection with an opaque interface and disciplined cleanup. In the Rust design, the compiler checks the borrow rule for safe callers, assuming the lock implementation is sound.
Then name a failure that remains possible. If the lock implementation restores local interrupts before releasing the lock, a handler can enter in that interval, try to take the same lock, and spin indefinitely. The kernel must also provide the acquire and release ordering that makes writes by one hart visible to the next lock holder. Recursive acquisition of the same non-reentrant lock can still deadlock. A strong defense names both the guarantee and the obligation. For this example, the interface limits how safe callers access queue memory, and the implementation must make that access exclusive across the contexts that use it.
Explain why you chose the design
3 min readA useful interview defense gives the condition that drove the choice, the cost you accept, and the condition that would make you reconsider. Use the same reasoning in both languages. If you claim one design reduces latency or improves throughput, explain which delay or work changes and what you would measure to check it.
Going deeperExplore the mechanism
Async versus threads. For many I/O operations that spend most of their time waiting, I would consider async tasks so a small set of executor threads can run whichever tasks are ready. In Linux user space, epoll is one source of file descriptor readiness an executor can use. An async task must yield when it would block. A blocking operation on a shared executor thread can delay unrelated tasks assigned to that thread. I would consider dedicated threads for a small number of workers that call blocking interfaces, or use multiple execution threads when work needs CPU parallelism. Ask about the number of outstanding operations, how long they wait, and whether they can yield. epoll(7)
Polled versus interrupt-driven. I would poll for a short device operation during early initialization, when the interrupt path is unavailable and a bounded wait with a defined timeout is acceptable. Polling can also be reasonable with sustained work and a dedicated CPU budget because repeatedly arranging notifications has a cost. For sparse arrivals during normal operation, I would use interrupts so the CPU can do other work between arrivals. I accept interrupt delivery and handler overhead and keep the handler short. I would measure CPU time spent waiting, interrupt rate, and completion latency under the expected load.
Spinlock versus per-CPU data. The shared receive queue needs a consistent view of its indices and bytes, so I would start with a short critical section under one spinlock. That gives a single ordering for queue updates, with contention and spinning as possible costs. For a hot statistic such as a receive count, per-CPU counters can reduce shared writes when a reader can aggregate counts later. The update still needs a way to stay on or safely address the intended CPU, and local updates must account for interrupt reentry if both contexts can write. Aggregation takes work, and independently read counters do not automatically form a simultaneous snapshot.
Copy-on-write versus eager copy. If a child will soon replace its address space or touch few inherited pages, I would share private pages initially and copy on writes that require a private copy. Linux fork uses copy-on-write, so fork still has work such as duplicating page tables even when user pages are shared initially. A kernel design using copy-on-write must manage shared frame lifetimes and ensure stale writable translations cannot bypass write protection. If most inherited pages will be modified and predictable latency on later writes matters more than startup cost, I would evaluate eager copying. That moves copying work and memory demand to creation time. fork(2)
Score the evidence and choose what to restudy
3 min readUse your course rubric and its descriptors. For each criterion, write down the sentence, diagram, or execution trace in your answer that earns the mark. Give the C and Rust answers separate evidence columns. If the rubric includes a criterion specific to Rust, such as explaining an ownership guarantee, score it using the Rust answer and its defense. For a criterion both answers share, keep both marks visible so one good answer cannot hide an omission in the other.
If you need a quick scale for rehearsal, use 0 for absent or wrong, 1 for a correct claim with an incomplete explanation, and 2 for a correct explanation that accounts for the relevant failure case. These are rehearsal scores. Use the official descriptors and weights when you report an assessment mark. Score the words you said before consulting your notes. Put any correction in a separate column so you can see what you can recall under interview conditions.
For example, suppose you correctly describe the full and empty queue cases in both answers and trace which operations occur under the lock. You give yourself a 2 for the queue invariant in each answer. You also describe masking local interrupts before acquisition in both answers. But when asked what happens during release, you say only that the Rust guard drops and restores interrupts. Give the Rust explanation a 1 for interrupt handling because it omits the order and why it matters. If you explicitly said that interrupts are restored first, give that explanation a 0. The distinction matters because a wrong sequence calls for a different correction than an omitted detail.
Compare the criteria against their expected scores. When two gaps are equally large, fix the one that breaks correctness first. If the same gap appears in both answers, restudy the OS mechanism once and then rehearse both versions. If the gap appears only in the Rust defense, restudy the relevant interface or implementation contract. In the example, write the topic as “Unlock before restoring local interrupts, including nested interrupt masks.” That gives you a narrower target than “review Rust” or “review concurrency.”
Use a fresh prompt to check whether the restudy worked. For example, assume a task enters the queue operation with local interrupts already disabled by an outer scope. Explain what queue-guard destruction does, whether local interrupts are enabled afterward, and when the outer scope may restore its saved state. Then explain why a remote hart can still contend for the queue during that sequence. Rescore the same rubric row from this answer. If you still omit the ordering or confuse local masking with exclusion across harts, keep the criterion open and record the exact missing statement.
Before moving to the next module, write down the criterion, the evidence you missed, and the prompt you will use to retest it. Use the retest result to decide whether that topic still needs review.
Key takeaways
- Both answers must preserve the same queue invariant and handle the same interrupt interleaving.
- Local interrupt masking prevents same-hart reentry, while the spinlock serializes access across harts.
- A Rust lock guard can restrict access to protected data, while its implementation must uphold the interrupt and locking protocol.
- The weakest rubric area is the one with the least adequate evidence, with correctness gaps taking priority when scores tie.
Reflect
- Explain to a colleague, in two sentences: explain the same interrupt synchronization problem in C/xv6 and in freestanding Rust.
- Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
- What surprised you when you ran the commands yourself?
On a real board
Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.
References and further reading
- Kernel documentationLinux kernel locking lessons: interrupt-safe spinlocks
- Man pageepoll(7): the epoll interface
- Man pagefork(2): create a child process
Official documentation first. Links checked when the lesson was published.