chit2
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
// Function to calculate waiting time
void findWaitingTime(vector<vector<int>>& processes, int n, vector<int>& wt) {
wt[0] = 0;
// Calculating waiting time
for (int i = 1; i < n; ++i) {
wt[i] = processes[i - 1][1] + wt[i - 1];
}
}
// Function to calculate turn around time
void findTurnAroundTime(vector<vector<int>>& processes, int n, vector<int>& wt, vector<int>& tat) {
// Calculating turnaround time by adding bt[i] + wt[i]
for (int i = 0; i < n; ++i) {
tat[i] = processes[i][1] + wt[i];
}
}
// Function to calculate average time
void findAvgTime(vector<vector<int>>& processes, int n) {
vector<int> wt(n, 0);
vector<int> tat(n, 0);
// Function to find waiting time of all processes
findWaitingTime(processes, n, wt);
// Function to find turn around time for all processes
findTurnAroundTime(processes, n, wt, tat);
// Display processes along with all details
cout << "\nProcesses Burst Time Waiting Time Turn-Around Time" << endl;
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; ++i) {
total_wt += wt[i];
total_tat += tat[i];
cout << " " << processes[i][0] << "\t\t"
<< processes[i][1] << "\t\t"
<< wt[i] << "\t\t"
<< tat[i] << endl;
}
cout << "\nAverage waiting time = " << (float)total_wt / n << endl;
cout << "Average turn around time = " << (float)total_tat / n << endl;
}
// Function to implement priority scheduling
void priorityScheduling(vector<vector<int>>& proc, int n) {
// Sort processes by priority (in descending order)
sort(proc.begin(), proc.end(), [](const vector<int>& a, const vector<int>& b) {
return a[2] > b[2];
});
cout << "Order in which processes gets executed: ";
for (int i = 0; i < n; ++i) {
cout << proc[i][0] << " ";
}
cout << endl;
// Calculate average time (waiting time, turn-around time)
findAvgTime(proc, n);
}
int main() {
// Process id's, Burst time, and Priority
vector<vector<int>> proc = {{1,4, 1},
{2, 5, 0},
{3, 8, 1},
{4,9,0}};
int n = 4;
// Call priority scheduling function
priorityScheduling(proc, n);
return 0;
}