From 31555c90ff59f17939556dced6f922e99b1ff304 Mon Sep 17 00:00:00 2001 From: Aniket Shahi <109075043+aniket0608@users.noreply.github.com> Date: Wed, 5 Apr 2023 15:01:32 +0530 Subject: [PATCH] Update SJN.cpp Privious code was not working --- CPU SCHEDULING/SJN.cpp | 173 +++++++++++++++++------------------------ 1 file changed, 71 insertions(+), 102 deletions(-) diff --git a/CPU SCHEDULING/SJN.cpp b/CPU SCHEDULING/SJN.cpp index 65ac77e..392ce97 100644 --- a/CPU SCHEDULING/SJN.cpp +++ b/CPU SCHEDULING/SJN.cpp @@ -1,129 +1,98 @@ -#include +#include +#include +#include using namespace std; -class Process +struct Process { -public: - int PID; - double arrival_time; - double execution_time; - double starting_time; - double waiting_time; - - Process() - { - } - - Process(int PID, double arrival_time, double execution_time) - { - this->PID = PID; - this->arrival_time = arrival_time; - this->execution_time = execution_time; - starting_time = 0; - waiting_time = 0; - } - - void Scan() - { - cout << "Enter PID : "; - cin >> PID; - cout << "Enter Arrival Time : "; - cin >> arrival_time; - cout << "Enter Execution Time : "; - cin >> execution_time; - starting_time = 0; - waiting_time = 0; - } - - void Print() - { - cout << "|" << PID << "\t|\t" << arrival_time << "\t|\t" << execution_time << "\t|\t" << starting_time << "\t|\t" << waiting_time << "\t|" << endl; - } + int id; + int arrival_time; + int burst_time; }; -bool operator<(const Process &p1, const Process &p2) +bool compare_arrival_time(Process a, Process b) { - return p1.execution_time > p2.execution_time; + return a.arrival_time < b.arrival_time; } -bool comparator(Process a, Process b) +bool compare_burst_time(Process a, Process b) { - return (a.arrival_time < b.arrival_time); + return a.burst_time < b.burst_time; } -void Output(vector p) +int main() { - cout << "\n\n-------------------------------------------------------------------------\n"; - cout << "|PID\t|Arrival Time\t|Execution Time\t|Starting Time\t|Waiting Time\t|\n"; - cout << "-------------------------------------------------------------------------\n"; - for (auto i : p) + int n; + cout << "Enter the number of processes: "; + cin >> n; + + vector processes(n); + + for (int i = 0; i < n; i++) { - i.Print(); + cout << "Enter the process ID, arrival time, and burst time for process " << i + 1 << ": "; + cin >> processes[i].id >> processes[i].arrival_time >> processes[i].burst_time; } - cout << "-------------------------------------------------------------------------\n"; -} -int main() -{ + // Sort processes based on arrival time + sort(processes.begin(), processes.end(), compare_arrival_time); - freopen("input_SJN.txt", "r", stdin); - // freopen("output.txt","w",stdout); + vector completion_times(n); + vector waiting_times(n); + vector turnaround_times(n); - vector p; + int current_time = 0; - int n; - cout << "Enter the number of processes : "; - cin >> n; + for (int i = 0; i < n; i++) + { + // Sort processes based on burst time among those that have arrived + sort(processes.begin() + i, processes.end(), compare_burst_time); + + // Calculate completion time of the current process + completion_times[i] = max(current_time, processes[i].arrival_time) + processes[i].burst_time; + // Calculate waiting time and turnaround time of the current process + waiting_times[i] = max(0, current_time - processes[i].arrival_time); + turnaround_times[i] = completion_times[i] - processes[i].arrival_time; + + // Update current time + current_time = completion_times[i]; + } + + // Calculate average waiting time and turnaround time + double avg_waiting_time = 0; + double avg_turnaround_time = 0; + for (int i = 0; i < n; i++) + { + avg_waiting_time += waiting_times[i]; + avg_turnaround_time += turnaround_times[i]; + } + avg_waiting_time /= n; + avg_turnaround_time /= n; + + // Print the Gantt chart + cout << "Gantt chart:" << endl; + current_time = processes[0].arrival_time; for (int i = 0; i < n; i++) { - Process temp; - temp.Scan(); - p.push_back(temp); + cout << "| P" << processes[i].id << " "; + current_time = completion_times[i]; } + cout << "|" << endl; - sort(p.begin(), p.end(), comparator); - // Output(p); - - priority_queue pq; - vector Final; - - double st = 0, wt = 0; - pq.push(p[0]); - int i = 1; - while (!pq.empty()) - { - // priority_queue tpq; - // while(!pq.empty()){ - // Process tp = pq.top(); - // tp.Print(); - // tpq.push(tp); - // pq.pop(); - // } - // while(!tpq.empty()){ - // Process tp = tpq.top(); - // pq.push(tp); - // tpq.pop(); - // } - // cout<