A command-line CPU Scheduling Simulator implemented in C++ that demonstrates and compares common CPU scheduling algorithms used in Operating Systems.
The simulator supports FCFS, Shortest Job First, Priority Scheduling, and Round Robin while calculating important scheduling metrics such as waiting time, turnaround time, response time, and completion time.
It also generates a process execution trace and a Gantt chart to visualize CPU allocation.
- First Come First Serve (FCFS)
- Shortest Job First (SJF) - Non-Preemptive
- Priority Scheduling - Non-Preemptive
- Round Robin - Preemptive
- Arrival Time support
- CPU idle-time handling
- Configurable Time Quantum for Round Robin
- Process execution trace
- Gantt chart visualization
- Completion Time calculation
- Turnaround Time calculation
- Waiting Time calculation
- Response Time calculation
- Average scheduling metrics
- Menu-driven command-line interface
The simulator calculates the following metrics for every process.
The time at which a process finishes its execution.
TAT = CT - AT
WT = TAT - BT
RT = First CPU Start Time - AT
Where:
- AT = Arrival Time
- BT = Burst Time
- CT = Completion Time
FCFS executes processes according to their arrival order.
It is a non-preemptive scheduling algorithm.
The simulator implements non-preemptive SJF.
Among all processes that have arrived, the process with the smallest burst time is selected.
The simulator implements non-preemptive Priority Scheduling.
Among available processes, the process with the highest priority is selected.
In this implementation:
Lower priority number = Higher priority
For example:
Priority 1 > Priority 2 > Priority 3
Round Robin is a preemptive scheduling algorithm.
Each process receives the CPU for a fixed amount of time called the Time Quantum.
If the process does not finish during its quantum, it is placed back into the ready queue.
CPU Scheduling Simulator
|
v
Process Input
|
v
Algorithm Selection
|
+---------------+---------------+
| | |
FCFS SJF Priority
| | |
+---------------+---------------+
|
Round Robin
|
v
CPU Execution
|
+-------+-------+
| |
Process IDLE
Execution CPU
| |
+-------+-------+
|
v
Execution Trace
|
+-----------+-----------+
| |
v v
Scheduling Metrics Gantt Chart
|
+------+------+------+
| | | |
CT TAT WT RT
std::vector is used to store and manage process information.
std::queue is used to implement the ready queue in Round Robin scheduling.
Processes are removed from the front of the queue and unfinished processes are inserted again at the rear.
An execution structure stores CPU execution intervals.
struct Execution
{
int pid;
int startTime;
int endTime;
};This information is used to generate the execution trace and Gantt chart.
Number of Processes: 4
Process Arrival Time Burst Time Priority
P1 0 5 2
P2 1 3 1
P3 2 8 3
P4 3 6 2
SELECT SCHEDULING ALGORITHM
1. First Come First Serve (FCFS)
2. Shortest Job First (SJF)
3. Priority Scheduling
4. Round Robin
5. Exit
For a time quantum of 2:
+---------+---------+---------+---------+---------+
|P1 |P2 |P3 |P1 |P4 |
+---------+---------+---------+---------+---------+
0 2 4 6 8 10
A process may appear multiple times because Round Robin is preemptive.
The simulator also handles situations where no process is available for execution.
Example:
0 -> 2 : IDLE
2 -> 5 : P1
5 -> 7 : P2
7 -> 8 : IDLE
8 -> 10 : P3
The idle intervals are also represented in the Gantt chart.
| Algorithm | Implementation Complexity |
|---|---|
| FCFS | O(n log n) |
| SJF | O(n²) |
| Priority | O(n²) |
| Round Robin | Depends on number of time slices |
FCFS includes sorting processes according to arrival time.
SJF and Priority Scheduling repeatedly search available processes.
Round Robin uses a queue for ready-process management.
Using g++:
g++ main.cpp -o schedulerOn Windows:
g++ main.cpp -o scheduler.exeLinux/macOS:
./schedulerWindows PowerShell:
.\scheduler.exe- C++
- C++ STL
- Operating Systems
- Data Structures and Algorithms
- CPU Scheduling
- Preemptive Scheduling
- Non-Preemptive Scheduling
- Ready Queue
- Process Arrival
- CPU Idle Time
- Process Execution
- Waiting Time
- Turnaround Time
- Response Time
- STL Vector
- STL Queue
- Sorting and Comparators
- Algorithm Complexity
The current implementation focuses on core CPU scheduling concepts.
It does not currently simulate:
- Context-switching overhead
- Preemptive Priority Scheduling
- Shortest Remaining Time First (SRTF)
- Aging
- Multicore CPU scheduling
- I/O burst behavior
Possible extensions include:
- SRTF scheduling
- Preemptive Priority Scheduling
- Aging to prevent starvation
- Context-switch overhead simulation
- Comparative performance analysis
- CPU utilization calculation
- Throughput calculation
- Multicore scheduling simulation
- Graphical visualization
Developed as an Operating Systems and C++ project to demonstrate CPU scheduling algorithms and their performance characteristics.