CPU-SchedSim is a modern C++ CPU scheduling simulator for learning and comparing classic short-term operating-system scheduling algorithms.
The project is a pure simulator. It does not create real OS processes or threads. Instead, it models a single CPU, process arrivals, ready queues, optional I/O blocking, context-switch overhead, preemption, and per-process scheduling metrics.
fcfs)sjf)srtf)rr)priority)Priority values follow this convention:
lower number = higher priority
.
├── main.cpp
├── Process.h
├── Process.cpp
├── Workload.h
├── Workload.cpp
├── Scheduler.h
├── Scheduler.cpp
├── Metrics.h
├── Metrics.cpp
├── process_scheduling_simulator.cpp
├── sample_workload.txt
└── README.md
File roles:
| File | Purpose |
|---|---|
main.cpp |
Command-line interface, option parsing, self-test runner |
Process.* |
Process definition, runtime state, lifecycle validation |
Workload.* |
Random workload generation and custom input loading |
Scheduler.* |
Scheduling algorithms and simulation engine |
Metrics.* |
Per-process metrics, summaries, Gantt output, CSV export |
process_scheduling_simulator.cpp |
Compatibility single-file build wrapper |
sample_workload.txt |
Example custom workload |
Recommended modular build:
g++ -std=c++17 -Wall -Wextra -pedantic main.cpp Process.cpp Workload.cpp Scheduler.cpp Metrics.cpp -o cpu_schedsim
Windows PowerShell:
g++ -std=c++17 -Wall -Wextra -pedantic main.cpp Process.cpp Workload.cpp Scheduler.cpp Metrics.cpp -o cpu_schedsim.exe
Compatibility build using the original project filename:
g++ -std=c++17 -Wall -Wextra -pedantic process_scheduling_simulator.cpp -o cpu_schedsim
Run all algorithms with the default random workload:
./cpu_schedsim
Run one algorithm:
./cpu_schedsim --algorithm srtf
Run a smaller deterministic random workload:
./cpu_schedsim --processes 5 --seed 42
Print the generated workload before simulation:
./cpu_schedsim --print-workload
Show a Gantt/timeline view:
./cpu_schedsim --algorithm rr --quantum 100 --gantt
Export metrics to CSV:
./cpu_schedsim --csv results.csv
Run the built-in tests:
./cpu_schedsim --test
--algorithm all|fcfs|sjf|srtf|rr|priority
--processes N
--seed N
--lambda X
--max-arrival N
--with-io
--input PATH
--quantum N
--context-switch N
--aging-interval N
--no-aging
--gantt
--csv PATH
--print-workload
--test
--help
Defaults:
| Option | Default |
|---|---|
| Processes | 20 |
| Random seed | 1 |
| Exponential lambda | 0.001 |
| Max generated arrival | 8000ms |
| Round Robin quantum | 200ms |
| Context switch cost | 14ms |
| Priority aging interval | 500ms |
| Priority aging | enabled |
Use --input to load processes from a text file.
Each non-comment line has this format:
pid arrival priority cpu1 [io1 cpu2 [io2 cpu3 ...]]
Example:
1 0 2 500
2 20 1 300 80 200
3 50 0 1000
Meaning:
0ms, has priority 2, and needs one 500ms CPU burst.20ms, has priority 1, runs 300ms, waits for 80ms I/O, then runs another 200ms.50ms, has priority 0, and needs one 1000ms CPU burst.Run the sample workload:
./cpu_schedsim --input sample_workload.txt --algorithm priority --gantt
For each process:
Turnaround Time = Completion Time - Arrival Time
Response Time = First CPU Start Time - Arrival Time
Waiting Time = Total time spent in ready queues
For the whole simulation:
Throughput = Completed Processes / Total Simulation Time
CPU Utilization = CPU Busy Time / Total Simulation Time
The simulator also reports:
When I/O bursts are used, time spent blocked for I/O is not counted as ready-queue waiting time.
Processes run in arrival order. Once a process gets the CPU, it runs until its CPU burst ends or it blocks for I/O.
Among ready processes, the shortest next CPU burst is selected. The running process is not preempted.
The scheduler always prefers the process with the shortest remaining CPU time. If a shorter job arrives, it preempts the currently running process.
Processes rotate through a FIFO ready queue. Each process can run for at most the configured quantum before it goes to the back of the queue.
The highest-priority ready process runs first. Lower numeric priority means higher scheduling priority. A newly ready higher-priority process preempts the current process. Processes with equal priority use Round Robin. Aging can gradually improve the priority of long-waiting processes.
./cpu_schedsim --algorithm all --processes 5 --seed 42 --with-io --csv results.csv
This command:
42 for reproducibility.results.csv.