-
Notifications
You must be signed in to change notification settings - Fork 0
/
PriorityScheduling.cpp
65 lines (60 loc) · 1.64 KB
/
PriorityScheduling.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <algorithm>
using namespace std;
struct Process
{
int pid;
int bt;
int tat;
int wt;
int priority;
};
bool compare(Process P1, Process P2)
{
return P1.priority < P2.priority;
}
void findWaitingTime(Process processes[], int n)
{
sort(processes, processes + n, compare);
processes[0].wt = 0;
for (int i = 1; i < n; i++)
processes[i].wt = processes[i - 1].wt + processes[i - 1].bt;
}
void findTurnaroundTime(Process processes[], int n)
{
for (int i = 0; i < n; i++)
processes[i].tat = processes[i].bt + processes[i].wt;
}
void findAvgTime(Process processes[], int n)
{
int awt = 0, atat = 0;
findWaitingTime(processes, n);
findTurnaroundTime(processes, n);
cout << "Process\t\tPriority\tBurst-Time\tWaiting-Time\tTurnAround-Time\n";
for (int i = 0; i < n; i++)
{
cout << processes[i].pid << "\t\t" << processes[i].priority << "\t\t" << processes[i].bt << "\t\t" << processes[i].wt << "\t\t" << processes[i].tat << endl;
awt += processes[i].wt;
atat += processes[i].tat;
}
cout << "Average Waiting Time: " << (float)awt / (float)n << endl;
cout << "Average Turn-Around Time: " << (float)atat / (float)n << endl;
}
int main()
{
int n;
cout << "Enter number of process: ";
cin >> n;
Process processes[n];
cout << "Enter burst time and priority for each process:\n";
for (int i = 0; i < n; i++)
{
cin >> processes[i].bt;
cin >> processes[i].priority;
processes[i].pid = i + 1;
processes[i].wt = 0;
processes[i].tat = 0;
}
findAvgTime(processes, n);
return 0;
}