diff --git a/CPU SCHEDULING/PRIORITY.cpp b/CPU SCHEDULING/PRIORITY.cpp index 06a7e4f..81c2049 100644 --- a/CPU SCHEDULING/PRIORITY.cpp +++ b/CPU SCHEDULING/PRIORITY.cpp @@ -1,134 +1,92 @@ -#include -using namespace std; - -class Process -{ -public: - int PID; +#include +#include +#include + +struct Process { + int id; + int arrival_time; + int burst_time; int priority; - 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; - cout << "Priority : "; - cin >> priority; - starting_time = 0; - waiting_time = 0; - } - - void Print() - { - cout << "|" << PID << "\t|\t" << priority << "\t|\t" << arrival_time << "\t|\t" << execution_time << "\t|\t" << starting_time << "\t|\t" << waiting_time << "\t|" << endl; - } + int completion_time; + int turnaround_time; + int waiting_time; }; -bool operator<(const Process &p1, const Process &p2) -{ - return p1.priority > p2.priority; +bool compareArrivalTime(const Process &a, const Process &b) { + return a.arrival_time < b.arrival_time; } -bool comparator(Process a, Process b) -{ - return (a.arrival_time < b.arrival_time); +bool comparePriority(const Process &a, const Process &b) { + return a.priority < b.priority; } -void Output(vector p) -{ - cout << "\n\n-----------------------------------------------------------------------------------------\n"; - cout << "|PID\t| Priority \t|Arrival Time\t|Execution Time\t|Starting Time\t|Waiting Time\t|\n"; - cout << "-----------------------------------------------------------------------------------------\n"; - for (auto i : p) - { - i.Print(); +int main() { + int n; + std::cout << "Enter the number of processes: "; + std::cin >> n; + + std::vector processes(n); + for (int i = 0; i < n; ++i) { + processes[i].id = i + 1; + std::cout << "Enter arrival time, burst time, and priority for process " << i + 1 << ": "; + std::cin >> processes[i].arrival_time >> processes[i].burst_time >> processes[i].priority; } - cout << "-----------------------------------------------------------------------------------------\n"; -} -int main() -{ - - freopen("input_PRIORITY.txt", "r", stdin); - // freopen("output.txt","w",stdout); + std::sort(processes.begin(), processes.end(), compareArrivalTime); + int current_time = processes[0].arrival_time; + + while (!processes.empty()) { + std::vector ready_queue; + for (const auto &p : processes) { + if (p.arrival_time <= current_time) { + ready_queue.push_back(p); + } else { + break; + } + } - vector p; + if (!ready_queue.empty()) { + std::sort(ready_queue.begin(), ready_queue.end(), comparePriority); + Process &selected_process = ready_queue.front(); + selected_process.completion_time = current_time + selected_process.burst_time; + selected_process.turnaround_time = selected_process.completion_time - selected_process.arrival_time; + selected_process.waiting_time = selected_process.turnaround_time - selected_process.burst_time; + current_time = selected_process.completion_time; + + processes.erase(std::remove_if(processes.begin(), processes.end(), + [&](const Process &p) { return p.id == selected_process.id; }), + processes.end()); + std::cout << "P" << selected_process.id << "\t" << selected_process.arrival_time << "\t" + << selected_process.burst_time << "\t" << selected_process.priority << "\t" + << selected_process.completion_time << "\t" << selected_process.turnaround_time << "\t" + << selected_process.waiting_time << std::endl; + } else { + current_time++; + } + } - int n; - cout << "Enter the number of processes : "; - cin >> n; + // Calculate performance metrics + double avg_turnaround_time = 0; + double avg_waiting_time = 0; + double throughput = 0; + double cpu_utilization = 0; - for (int i = 0; i < n; i++) + for (const auto &p : processes) { - Process temp; - temp.Scan(); - p.push_back(temp); + avg_turnaround_time += p.turnaround_time; + avg_waiting_time += p.waiting_time; } + avg_turnaround_time /= n; + avg_waiting_time /= n; - sort(p.begin(), p.end(), comparator); - // Output(p); + throughput = double(n) / current_time; - priority_queue pq; - vector Final; + cpu_utilization = (current_time - processes.front().arrival_time) / double(current_time)*100; - double st = 0, wt = 0; - pq.push(p[0]); - int i = 1; - while (!pq.empty()) - { - // cout<<"\n"; - // 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<