THINK FIRST·CODE LATER

← All labs

Nested interrupts: a priority interrupt controller

Problem

Simulate an interrupt controller with priorities and nesting. Time advances in whole microseconds.

Input: lines name arrival priority duration until the end of input (priority 1 is the highest). At every microsecond the CPU runs the handler of the highest-priority interrupt that has arrived and is not finished (ties: earlier arrival, then input order). A newly arrived interrupt with higher priority therefore preempts (nests over) a running lower-priority handler; the preempted handler continues later. When no interrupt is pending, the CPU runs the user program.

Print one line per interrupt in input order:

name: arrival A, start S, finish F, latency L, preempted K times

where S is the first microsecond it runs, F the time it finishes, L = S − A (interrupt latency) and K the number of times it was preempted by another handler. Then print User program time lost: X us (the total time spent in handlers, from the first arrival to the last finish) and Max latency: name (L us) (first in input order on a tie).

Input:

disk 0 3 5
timer 2 1 1
network 3 2 2

Output:

disk: arrival 0, start 0, finish 8, latency 0, preempted 1 times
timer: arrival 2, start 2, finish 3, latency 0, preempted 0 times
network: arrival 3, start 3, finish 5, latency 0, preempted 0 times
User program time lost: 8 us
Max latency: disk (0 us)

(Here the disk handler runs 0–2, is preempted by the timer at 2, then by the network at 3 — that counts as one preemption, because between 2 and 5 the disk handler never ran — and finishes 5–8.)

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.