2
1
SystemsProgrammingAndOperat.../Codes/priority.cpp
2024-08-21 01:16:26 +05:30

54 lines
1.3 KiB
C++
Executable File

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Process {
int id;
int burstTime;
int priority;
int waitingTime;
int turnaroundTime;
};
bool comparePriority(Process a, Process b) {
return a.priority > b.priority;
}
void priorityScheduling(vector<Process> &processes) {
int n = processes.size();
sort(processes.begin(), processes.end(), comparePriority);
processes[0].waitingTime = 0;
processes[0].turnaroundTime = processes[0].burstTime;
for (int i = 1; i < n; ++i) {
processes[i].waitingTime = 0;
for (int j = 0; j < i; ++j) {
processes[i].waitingTime += processes[j].burstTime;
}
processes[i].turnaroundTime = processes[i].waitingTime + processes[i].burstTime;
}
cout << "Process ID\tBurst Time\tPriority\tWaiting Time\tTurnaround Time\n";
for (const auto &p : processes) {
cout << p.id << "\t\t" << p.burstTime << "\t\t" << p.priority
<< "\t\t" << p.waitingTime << "\t\t" << p.turnaroundTime << '\n';
}
}
int main() {
vector<Process> processes = {
{1, 6, 2, 0, 0},
{2, 8, 1, 0, 0},
{3, 7, 3, 0, 0},
{4, 3, 4, 0, 0}
};
priorityScheduling(processes);
return 0;
}