Skip to content
C project #25Advanced

CPU Scheduling Simulator

Simulate FCFS, SJF, SRTF, preemptive Priority and Round Robin with Gantt charts, per-process metrics and a comparison table.

Estimated time
~10 hours to build
Files
7 files · 447 lines
Technologies
C11

Introduction

CPU scheduling is a core operating-systems topic and a favourite exam question. This simulator runs five algorithms on the same processes so you can see, not just memorise, how each one trades waiting time against responsiveness.

Problem statement

Read processes (name, arrival, burst, priority) and a Round Robin quantum. Simulate FCFS, non-preemptive SJF, SRTF, preemptive Priority and Round Robin, including idle periods. For each print a Gantt chart and per-process completion, turnaround, waiting and response times with averages, then a comparison naming the algorithm with the lowest average waiting time.

Objectives

  • Implement five scheduling algorithms precisely
  • Handle arrivals, idle CPU time and tie-breaking
  • Compute standard scheduling metrics
  • Present results as text Gantt charts

Features

  • Five algorithms on identical input
  • Idle periods shown in the Gantt chart
  • Merged slices for readable charts
  • Turnaround, waiting and response metrics
  • Comparison table

Concepts demonstrated

  • Process scheduling
  • Simulation
  • Queues
  • Tie-breaking rules

System architecture

scheduler.c shares a pick function that selects the best ready process by a key (arrival, burst, remaining time or priority). Non-preemptive algorithms run the chosen process to completion; preemptive ones re-decide every time unit. Round Robin uses a FIFO queue where newly arrived processes join before the preempted one. run records slices and merges consecutive ones.

Folder structure

TextProject tree
cpu-scheduler-simulator/
├── README.md
├── Makefile
├── src/main.c
├── src/scheduler.c
├── src/scheduler.h
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/scheduler.h
Process, slice and schedule types and the algorithm API.
src/scheduler.c
The five algorithms, the Gantt chart and the metric calculations.
src/main.c
Input, running every algorithm and the comparison table.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Number of processes (1-32):
Enter name arrival burst priority for each process:
Round Robin time quantum:
=== FCFS ===
Gantt chart:
| P1 | P2 | P3 | P4 | P5 |
0 8 12 21 26 28
Proc Arrive Burst Pri Complete Turnaround Waiting Response
P1 0 8 3 8 8 0 0
P2 1 4 1 12 11 7 7
P3 2 9 4 21 19 10 10
P4 3 5 2 26 23 18 18
P5 20 2 1 28 8 6 6
Average waiting 8.20 | turnaround 13.80 | response 8.20
=== SJF (non-preemptive) ===
Gantt chart:
| P1 | P2 | P4 | P3 | P5 |
0 8 12 17 26 28
Proc Arrive Burst Pri Complete Turnaround Waiting Response
P1 0 8 3 8 8 0 0
P2 1 4 1 12 11 7 7
P3 2 9 4 26 24 15 15
P4 3 5 2 17 14 9 9
P5 20 2 1 28 8 6 6
Average waiting 7.40 | turnaround 13.00 | response 7.40
=== SRTF (preemptive SJF) ===
Gantt chart:
| P1 | P2 | P4 | P1 | P3 | P5 | P3 |
0 1 5 10 17 20 22 28
Proc Arrive Burst Pri Complete Turnaround Waiting Response
P1 0 8 3 17 17 9 0
P2 1 4 1 5 4 0 0
P3 2 9 4 28 26 17 15
P4 3 5 2 10 7 2 2
P5 20 2 1 22 2 0 0
Average waiting 5.60 | turnaround 11.20 | response 3.40
=== Priority (preemptive) ===
Gantt chart:
| P1 | P2 | P4 | P1 | P3 | P5 | P3 |
0 1 5 10 17 20 22 28
Proc Arrive Burst Pri Complete Turnaround Waiting Response
P1 0 8 3 17 17 9 0
P2 1 4 1 5 4 0 0
P3 2 9 4 28 26 17 15
P4 3 5 2 10 7 2 2
P5 20 2 1 22 2 0 0
Average waiting 5.60 | turnaround 11.20 | response 3.40
=== Round Robin ===
(quantum 3)
Gantt chart:
| P1 | P2 | P3 | P4 | P1 | P2 | P3 | P4 | P1 | P3 | P5 |
0 3 6 9 12 15 16 19 21 23 26 28
Proc Arrive Burst Pri Complete Turnaround Waiting Response
P1 0 8 3 23 23 15 0
P2 1 4 1 16 15 11 2
P3 2 9 4 26 24 15 4
P4 3 5 2 21 18 13 6
P5 20 2 1 28 8 6 6
Average waiting 12.00 | turnaround 17.60 | response 3.60
=== Comparison ===
Algorithm Avg wait Avg turnar. Avg resp.
FCFS 8.20 13.80 8.20
SJF (non-preemptive) 7.40 13.00 7.40
SRTF (preemptive SJF) 5.60 11.20 3.40
Priority (preemptive) 5.60 11.20 3.40
Round Robin 12.00 17.60 3.60
Lowest average waiting time: SRTF (preemptive SJF)
Input
5
P1 0 8 3
P2 1 4 1
P3 2 9 4
P4 3 5 2
P5 20 2 1
3

Testing

  • Diff with tests/expected-output.txt.
  • Work out the FCFS chart by hand for the sample and compare.
  • Add a process arriving after all others finish to see an idle slot.

Common errors

Round Robin order differs from the textbook

Convention matters: here processes that arrive during a time slice are queued before the preempted process.

Response time negative

Response = first time on the CPU − arrival; record first_run only once per process.

Possible improvements

  • Multilevel feedback queue scheduling
  • Context-switch overhead
  • Read processes from a file

Additional challenges

  1. 1Implement aging to prevent starvation in Priority scheduling.
  2. 2Add non-preemptive Priority and compare with the preemptive version.
  3. 3Generate random workloads and report which algorithm wins most often.

Built and tested it yourself? Mark it to update your dashboard.