THINK FIRST·CODE LATER

← All labs

A mini kernel: dual mode and a system-call table

Problem

Simulate how a CPU with dual-mode operation executes a user program that uses system calls.

The kernel has this system-call table (like Linux x86-64):

Number Name
0 read
1 write
39 getpid
57 fork
60 exit
62 kill

The program's process has PID 1204. Input: one instruction per line until the end of input:

  • add, load, store — ordinary instructions (allowed in user mode);
  • hlt, cli, out, settimer — privileged instructions;
  • syscall NAME — trap into the kernel with the call's number.

Execute them in order, starting in user mode. Print one line per instruction:

  • ordinary: user: add
  • privileged in user mode: EXCEPTION: privileged instruction 'cli' in user mode -> process 1204 terminated, and stop (no further instructions run);
  • syscall NAME with a known name: trap -> kernel: syscall 1 (write) ... return to user except:
    • getpid prints trap -> kernel: syscall 39 (getpid) returns 1204 ... return to user;
    • fork prints trap -> kernel: syscall 57 (fork) returns child pid 1205 ... return to user (each fork creates the next PID: 1205, 1206…);
    • exit prints trap -> kernel: syscall 60 (exit) -> process 1204 exits and stops.
  • unknown system call: trap -> kernel: unknown syscall 'NAME' returns -ENOSYS ... return to user.

At the end print Mode switches: N (each trap into the kernel and each return to user mode counts as one switch; a trap that terminates the process counts only the entry) and Instructions executed: M (including the one that stopped the program).

Input:

load
syscall getpid
add
syscall write
syscall fork
cli
store

Output:

user: load
trap -> kernel: syscall 39 (getpid) returns 1204 ... return to user
user: add
trap -> kernel: syscall 1 (write) ... return to user
trap -> kernel: syscall 57 (fork) returns child pid 1205 ... return to user
EXCEPTION: privileged instruction 'cli' in user mode -> process 1204 terminated
Mode switches: 7
Instructions executed: 6

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os2-2-data-1.txt

load
syscall getpid
add
syscall write
syscall fork
cli
store
What your program must print for os2-2-data-1.txt
user: load
trap -> kernel: syscall 39 (getpid) returns 1204 ... return to user
user: add
trap -> kernel: syscall 1 (write) ... return to user
trap -> kernel: syscall 57 (fork) returns child pid 1205 ... return to user
EXCEPTION: privileged instruction 'cli' in user mode -> process 1204 terminated
Mode switches: 7
Instructions executed: 6

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os2-2-data-1.txt

Windows PowerShell does not understand <:

Get-Content os2-2-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.