The CPU keeps a mode bit. Interrupts can arrive while the kernel is already running, so the kernel can be entered more than once, one level inside the other.
What to build
Follow a list of events on a CPU that starts in user mode, and report how often the kernel was entered, how deep it went, and how the run ended.
Requirements
- Keep a depth. Depth 0 is user mode; depth 1 or more is kernel mode.
user: an ordinary instruction, allowed in both modes, changes nothing.priv: a privileged instruction. In user mode the program is killed at this event; in kernel mode it is allowed.syscall: in user mode it enters the kernel (depth becomes 1, one kernel entry). In kernel mode it is ignored.irq: an interrupt in any mode; depth goes up by 1, and if the CPU was in user mode this is a kernel entry.ret: leaves one level (depth goes down by 1). Aretin user mode kills the program at this event. Events after a kill are not read.
Input
The first line has n (1 ≤ n ≤ 50), then n words, each one of user, priv, syscall, irq, ret.
Output
Three lines: entries: E, max depth: D, and then mode: user, mode: kernel or killed at event K (K counts events from 1).
Constraints
Depth never goes above 50.
Example
Input:
6
user
syscall
priv
ret
user
irq
Output:
entries: 2
max depth: 1
mode: kernel
Why it works
Only two things move the CPU from user mode to kernel mode: a system call and an interrupt. Both are counted as entries. A nested interrupt goes one level deeper, and every ret goes one level back, so the depth is a stack. A user program that executes a privileged instruction or a return from interrupt has no right to either, so it is killed.
Examples
6 user syscall priv ret user irq
entries: 2 max depth: 1 mode: kernel
3 user priv user
entries: 0 max depth: 0 killed at event 2
Hints
Hint 1
Keep three numbers: depth (0 means user mode), entries and max_depth.
Hint 2
A syscall in user mode sets depth to 1 and adds 1 to entries. An irq adds 1 to depth, and also to entries if the depth was 0 before.
Hint 3
When a rule says the program is killed, remember the event number, stop reading events, and print the killed line.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
#include <string.h>
int main(void) {
int n, depth = 0, entries = 0, max_depth = 0, killed = 0;
char word[16];
scanf("%d", &n);
for (int i = 1; i <= n && !killed; i++) {
scanf("%15s", word);
if (strcmp(word, "priv") == 0) {
if (depth == 0) killed = i;
} else if (strcmp(word, "syscall") == 0) {
if (depth == 0) {
depth = 1;
entries++;
}
} else if (strcmp(word, "irq") == 0) {
if (depth == 0) entries++;
depth++;
} else if (strcmp(word, "ret") == 0) {
if (depth == 0) killed = i;
else depth--;
}
if (depth > max_depth) max_depth = depth;
}
printf("entries: %d\n", entries);
printf("max depth: %d\n", max_depth);
if (killed) printf("killed at event %d\n", killed);
else printf("mode: %s\n", depth == 0 ? "user" : "kernel");
return 0;
}
▶ Open this solution in the compiler, then press Visualize to watch it run step by step