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
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
Complete source code
Makefile
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:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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
- 1Implement aging to prevent starvation in Priority scheduling.
- 2Add non-preemptive Priority and compare with the preemptive version.
- 3Generate random workloads and report which algorithm wins most often.
Built and tested it yourself? Mark it to update your dashboard.