THINK FIRST·CODE LATER

← All labs

CPU scheduling simulator (FCFS, SJF, SRTF, priority, RR)

Problem

Build the simulator every OS student wishes they had before the midterm.

Input: first line: the algorithm — FCFS, SJF (non-preemptive), SRTF, PRIO-NP (non-preemptive priority), PRIO-P (preemptive priority) or RR q (Round Robin with quantum q). Then processes until the end of input: name arrival burst priority (integers; a smaller priority number = higher priority).

Rules:

  • Ties (same burst, remaining time or priority) are broken by earlier arrival, then by input order. FCFS orders by arrival, then input order.
  • Preemptive algorithms (SRTF, PRIO-P) re-decide at every time unit.
  • RR: processes that have arrived join the tail of the ready queue in input order; a process that did not finish goes to the tail after any processes that arrived during (or at the end of) its slice.
  • When no process is ready, the CPU is idle until the next arrival.

Output:

Gantt: | A 0-3 | B 3-9 | ... |
A: finish F, turnaround T, waiting W, response R
...
Average turnaround: x.xx
Average waiting: x.xx
Average response: x.xx
Throughput: 0.2500 per time unit
Context switches: N

Consecutive slices of the same process are merged in the Gantt chart; idle periods appear as idle s-e. Processes are listed in input order. Throughput = number of processes ÷ (last finish − first arrival), four decimals. Context switches = number of times the running process changes between consecutive non-idle Gantt segments (an idle gap between two different processes counts as one switch; the same process before and after an idle gap counts as none).

Input:

SRTF
A 0 3 2
B 2 6 1
C 4 4 3
D 6 5 2
E 8 2 1

Output:

Gantt: | A 0-3 | B 3-4 | C 4-8 | E 8-10 | B 10-15 | D 15-20 |
A: finish 3, turnaround 3, waiting 0, response 0
B: finish 15, turnaround 13, waiting 7, response 1
C: finish 8, turnaround 4, waiting 0, response 0
D: finish 20, turnaround 14, waiting 9, response 9
E: finish 10, turnaround 2, waiting 0, response 0
Average turnaround: 7.20
Average waiting: 3.20
Average response: 2.00
Throughput: 0.2500 per time unit
Context switches: 5

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.

os4-1-data-1.txt

SRTF
A 0 3 2
B 2 6 1
C 4 4 3
D 6 5 2
E 8 2 1
What your program must print for os4-1-data-1.txt
Gantt: | A 0-3 | B 3-4 | C 4-8 | E 8-10 | B 10-15 | D 15-20 |
A: finish 3, turnaround 3, waiting 0, response 0
B: finish 15, turnaround 13, waiting 7, response 1
C: finish 8, turnaround 4, waiting 0, response 0
D: finish 20, turnaround 14, waiting 9, response 9
E: finish 10, turnaround 2, waiting 0, response 0
Average turnaround: 7.20
Average waiting: 3.20
Average response: 2.00
Throughput: 0.2500 per time unit
Context switches: 5

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 < os4-1-data-1.txt

Windows PowerShell does not understand <:

Get-Content os4-1-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.