#include <iostream>
#include <algorithm>
using namespace std;

struct Process {
    int pid; // process id
    int bt; // burst time
    int wt; // waiting time
    int tat; // turnaround time
    int priority; // process priority
};

bool compare(Process p1, Process p2) {
    return p1.priority > p2.priority;
}

void priorityScheduling(Process proc[], int n) {
    sort(proc, proc + n, compare);
    int wt[n], tat[n], total_wt = 0, total_tat = 0;
    wt[0] = 0;
    tat[0] = proc[0].bt;
    for(int i = 1; i < n; i++) {
        wt[i] = wt[i-1] + proc[i-1].bt;
        tat[i] = tat[i-1] + proc[i].bt;
    }
    for(int i = 0; i < n; i++) {
        proc[i].wt = wt[i];
        proc[i].tat = tat[i];
        total_wt += wt[i];
        total_tat += tat[i];
    }
    float avg_wt = (float) total_wt / n;
    float avg_tat = (float) total_tat / n;
    cout << "Average waiting time = " << avg_wt << endl;
    cout << "Average turnaround time = " << avg_tat << endl;
}

int main() {
    int n;
    cout << "Enter the number of processes: ";
    cin >> n;
    Process proc[n];
    for(int i = 0; i < n; i++) {
        cout << "Enter the burst time for process " << i << ": ";
        cin >> proc[i].bt;
        cout << "Enter the priority for process " << i << ": ";
        cin >> proc[i].priority;
        proc[i].pid = i;
    }
    priorityScheduling(proc, n);
    return 0;
}
JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.