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