Question: In C + + Programming, I need help in simulating various CPU scheduling algorithms. Here's the scheduling algorithms I need to simulate: First - Come,

In C++ Programming, I need help in simulating various CPU scheduling algorithms.
Here's the scheduling algorithms I need to simulate:
First-Come, First-Serve (FCFS)
Shortest Job First (SJF)
Shortest Remaining Time (SRT)
Round Robin (RR)
The Output for each simulated algorithm needs to display the following for each of the above processes:
o Process ID
o Arrival Time
o Burst Time
o Average Waiting Time (The time a process spends in the ready queue before it starts
execution.)
o Average Turnaround Time (The total time taken for a process to complete from the
time of its arrival.)
Output Example
Process | Arrival Time | Burst Time | Waiting Time | Turnaround Time
P1|0|8|0|8
P2|1|4|7|11
P3|2|9|10|19
P4|3|5|18|23
Average Waiting Time: 8.75
Average Turnaround Time: 15.25
Here's what I have so far:
#include
#include
#include
using namespace std;
// Process Definitions
struct Process {
int pid;
int arrival_time;
int burst_time;
int waiting_time;
int turnaround_time;
int remaining_time;
}
Process(int id, int at, int bt){
pid = id;
arrival_time = at;
burst_time = bt;
waiting_time =0;
turnaround_time =0;
remaining_time = bt;
}
};
// input data
vector processes ={
Process(1,0,8),
Process(2,1,4),
Process(3,2,9),
Process(4,3,5)
};
// Scheduling Algorithms:
// First-Come, First-Serve (FCFS)
void fcfs_scheduling(vector& processes){
sort(processes.begin(), processes.end(),[](Process a, Process b){
return a.arrival_time < b.arrival_time;
});
int current_time =0;
for (auto& process : processes){
if (current_time < process.arrival_time){
current_time = process.arrival_time;
}
process.waiting_time = current_time - process.arrival_time;
process.turnaround_time = process.waiting_time +
process.burst_time;
current_time += process.burst_time;
}
}
// Shortest Job First (SJF)
void sjf_scheduling(vector& processes){
// Sort processes based on arrival time, then burst time
sort(processes.begin(), processes.end(),[](Process& a,
Process& b){
return (a.arrival_time == b.arrival_time)? a.burst_time <
b.burst_time : a.arrival_time < b.arrival_time;
});
int current_time =0;
vector ready_queue;
vector completed_processes;
// Add processes to ready queue and select the shortest job
// Execute the shortest job and continue the loop
}
// Shortest Remaining Time (SRT)
void srt_scheduling(vector& processes){
int current_time =0;
vector ready_queue;
// Continue to pick processes based on remaining time
// Preempt the process when needed and update the
remaining time
}
// Round Robin (RR)
void round_robin_scheduling(vector& processes, int
time_quantum){
int current_time =0;
vector ready_queue;
// Loop through processes, run for the time quantum, and
rotate
}
// Display Output
void display_results(const string& algorithm_name, const
vector& processes){
cout << "Results for "<< algorithm_name <<":"<< endl;
// Display results
for (const auto& process : processes){
cout << "Process ID: "<< process.pid <<", Arrival Time: "<<
process.arrival_time <<", Burst Time: "<< process.burst_time <<", Waiting Time:
"<< process.waiting_time
<<", Turnaround Time: "<< process.turnaround_time <<
endl;
}
// Calculate and display the average waiting and turnaround
times (to be implemented)
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!