An interrupt handler can itself be interrupted by one that is more urgent. The CPU then keeps a stack of unfinished handlers, one level deeper each time. A less urgent interrupt waits until the more urgent work is done.
What to build
Simulate handlers that arrive at different times, and report when each one finishes and how deep the nesting went.
Requirements
- Interrupt
Iiarrives at a given time, has a priority (smaller means more urgent) and needs a given number of time units. At every time unit the CPU runs, for one unit, the arrived and unfinished interrupt with the smallest priority number. Equal priority: the earlier arrival, then the one listed first. If there is none, the CPU idles for that unit (the main program runs). - After each unit, count the interrupts that have started (run at least one unit) but are not finished. The max depth is the largest such count seen.
- Print
Ii finishes at Tfor each interrupt in the order listed, whereTis the time at which its last unit ends. Then printmax depth: D.
Input
The first line has n (1 ≤ n ≤ 10). Each of the next n lines has the arrival time (0 to 50), the priority (0 to 15) and the duration (1 to 10) of I1, I2, ...
Output
n lines Ii finishes at T, then max depth: D.
Constraints
The whole simulation takes at most a few hundred time units.
Example
Input:
3
0 5 4
1 3 2
2 1 1
Output:
I1 finishes at 7
I2 finishes at 4
I3 finishes at 3
max depth: 2
Why it works
Picking the most urgent interrupt at every unit lets an urgent arrival interrupt a handler already running, which pushes the old one down the stack. The handlers that are started but not finished are exactly the stack, so its largest size is the nesting depth. Hardware limits this depth, and kernels keep handlers short so the stack stays small.
Examples
3 0 5 4 1 3 2 2 1 1
I1 finishes at 7 I2 finishes at 4 I3 finishes at 3 max depth: 2
2 0 1 3 1 2 3
I1 finishes at 3 I2 finishes at 6 max depth: 1
Hints
Hint 1
Simulate one time unit at a time. Each unit, pick among the interrupts that have arrived and are not finished the one with the smallest priority number (tie: earlier arrival, then listed first).
Hint 2
Run the chosen one for one unit: mark it started and subtract 1 from its time left. If it reaches 0, record the clock plus 1 as its finish time.
Hint 3
After each unit, count the interrupts that are started but not finished; the largest count seen is the nesting depth.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int n, arrival[10], pri[10], left[10], finish[10], started[10] = {0};
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d %d %d", &arrival[i], &pri[i], &left[i]);
finish[i] = 0;
}
int time = 0, done = 0, max_depth = 0;
while (done < n) {
int pick = -1;
for (int i = 0; i < n; i++) {
if (left[i] == 0 || arrival[i] > time) continue;
if (pick < 0 || pri[i] < pri[pick] || (pri[i] == pri[pick] && arrival[i] < arrival[pick])) pick = i;
}
if (pick >= 0) {
started[pick] = 1;
left[pick]--;
if (left[pick] == 0) {
finish[pick] = time + 1;
done++;
}
}
time++;
int depth = 0;
for (int i = 0; i < n; i++)
if (started[i] && left[i] > 0) depth++;
if (depth > max_depth) max_depth = depth;
}
for (int i = 0; i < n; i++) printf("I%d finishes at %d\n", i + 1, finish[i]);
printf("max depth: %d\n", max_depth);
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step