C++ Program for Scheduling Algorithms Simulation
C++ Program for Scheduling Algorithms Simulation
ALGORITHMS SIMULATION
ASSIGNMENT 6
In this assignment, you will implement a C++ program that simulates various
CPU scheduling algorithms. The requirements are as follows:
OBJECTIVES
You are also required to calculate and display the waiting time for each
process, as well as the average waiting time for the entire system.
QUESTION 1
In this section, we develop a comprehensive C++ program that simulates
three popular CPU scheduling algorithms: preemptive priority scheduling,
non-preemptive priority scheduling, and Round Robin (RR) scheduling. The
program is designed to accept user input for 'n' processes, where each
process is defined by its CPU burst time, arrival time, and priority number. The
user is then prompted to select the desired algorithm for scheduling. After
processing, the program calculates and displays each process's waiting time,
the average waiting time for the overall system, and the final execution
sequence for the processes.
• Input Requirements:
The program must obtain from the user an integer representing the
number of processes. For each of these processes, the following
parameters will be collected:
◦ CPU Burst Time: The total time required by the process on CPU.
◦ Arrival Time: The time at which the process arrives and is ready to
be executed.
◦ Priority Number: A numerical value that indicates the priority of
the process in the scheduling queue for priority-based algorithms.
• Scheduling Options:
Based on the user’s selection, the program should execute one of the
following algorithms:
• Program Outputs:
After executing the selected scheduling algorithm, the program must
display:
◦ Process ID
◦ CPU Burst Time
◦ Arrival Time
◦ Priority
◦ Waiting Time
◦ Completion Time
◦ Remaining Burst Time (for preemptive and Round Robin
scheduling)
2. Algorithm-Specific Steps:
The implementation has specific function blocks or logical sections for
each scheduling algorithm:
3. User Interaction:
The program begins by prompting the user for the number of processes
and their respective details, and then requests the desired scheduling
algorithm. Robust error checking should be present to handle potential
input errors (e.g., negative times or priorities) though for clarity in the
sample implementation, we assume valid input.
Below is the complete C++ code that meets the assignment requirements.
The code is organized into functions to enhance readability and
maintainability. Each major scheduling algorithm is implemented in its own
function, and comments have been added to explain the logic in detail.
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
double avgWaitingTime =
static_cast<double>(totalWaitingTime) / processes.size();
cout << "\nAverage Waiting Time: " << avgWaitingTime
<< endl;
// If process completes
if (processes[idx].remainingTime == 0) {
completed++;
processes[idx].completionTime = currentTime +
1; // Completion time is the current time + 1
}
currentTime++;
}
processes[idx].remainingTime -= execTime;
int main() {
int numProcesses;
cout << "Enter the number of processes: ";
cin >> numProcesses;
vector<Process> processes(numProcesses);
int choice;
cout << "\nSelect Scheduling Algorithm:" << endl;
cout << "1. Preemptive Priority Scheduling" << endl;
cout << "2. Non-Preemptive Priority Scheduling" <<
endl;
cout << "3. Round Robin Scheduling" << endl;
cout << "Enter your choice (1/2/3): ";
cin >> choice;
return 0;
}
CODE WALKTHROUGH
• Process Structure:
We define a structure named Process which encapsulates all essential
data points such as burst time, arrival time, priority, waiting time,
completion time, and the remaining time (used in algorithms that
require preemption or are time-sliced).
• Logic:
In this algorithm, at each time unit, we inspect all processes that have
arrived and are pending execution. The process with the highest priority
(lowest numerical priority value) is chosen to run for one time unit. If no
process is available at the current time, the simulation time is
incremented to reflect idle CPU cycles.
• Waiting Time Calculation:
The waiting time for all ready processes (those that have arrived but are
not currently executing) is incremented during each time unit.
• Execution Sequence:
The program maintains a vector that records each process that got to
use the CPU during every time quantum. This sequence can help
visualize the CPU switching between processes.
• Logic:
This algorithm differs from the preemptive version as once a process is
chosen it is allowed to execute to completion without interruption. The
selection of the next process is based primarily on arrival time and
priority.
• Execution Sequence:
Instead of a detailed timeline, the sequence here captures the order in
which processes were executed fully.
• Logic:
With Round Robin, each process is allowed to execute for a fixed time
slice (time quantum). If a process’s CPU burst exceeds this quantum, it is
preempted and added back to the end of the ready queue to continue
later.
• Queue Management:
A queue is used to manage the order of execution. All processes that
have arrived and are pending execution are enqueued, and as new
processes enter the system (determined by arrival time), they get
inserted appropriately.
• Sequence Record:
The execution sequence captures each time slice allocation to a process
which is then eventually printed.
SAMPLE OUTPUT
Below is an example of what the user might observe when executing this
program. Note that input prompts and results are clearly printed, and each
scheduling algorithm provides an easily interpretable output.
User Input:
Program Output:
--- Preemptive Priority Scheduling Results ---
Execution Sequence: P1 P2 P2 P1 P1 P3 P3 P3 P3 P3 P3 P3
P3
User Input:
Execution Sequence: P2 P1 P3
User Input:
Program Output:
Execution Sequence: P1 P1 P2 P3 P1 P3 P3 P2 P3 P3 P3 P3
P3
• User-Friendly Interface:
The program prompts the user to enter the necessary process
information and scheduling algorithm selection in a step-by-step
manner.
• Clear Output:
Waiting times for individual processes, the average waiting time, and an
execution sequence are entirely printed to the console for easy
interpretation by educators and students alike.