-
Notifications
You must be signed in to change notification settings - Fork 368
/
priority_scheduling.cpp
91 lines (78 loc) · 2.65 KB
/
priority_scheduling.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <bits/stdc++.h>
using namespace std;
int curr = 0;
double avgTurnAroundTime = 0, avgwaitingTime = 0, avgresponseTime = 0;
class priorityScheduling
{
public:
string name;
int completionTime, arrivalTime, burstTime, turnAroundTime, responseTime, waitingTime, priority;
};
bool cmp(const priorityScheduling &o1, const priorityScheduling &o2)
{
if (curr >= o2.arrivalTime && curr >= o1.arrivalTime)
{
return o1.priority < o2.priority;
}
return false;
}
int main()
{
cout << "Enter Number of Processes: ";
int n;
cin >> n;
vector<priorityScheduling> process(n);
cout << "Enter Name of Processes: ";
for (int i = 0; i < n; i++)
{
cin >> process[i].name;
}
cout << "Enter Arrival Time of Processes: ";
for (int i = 0; i < n; i++)
{
cin >> process[i].arrivalTime;
}
cout << "Enter Priority of Processes: ";
for (int i = 0; i < n; i++)
{
cin >> process[i].priority;
}
cout << "Enter Burst Time of Processes: ";
for (int i = 0; i < n; i++)
{
cin >> process[i].burstTime;
}
for (int i = 0; i < n; i++)
{
sort(process.begin(), process.end(), cmp);
process[i].responseTime = curr - process[i].arrivalTime;
process[i].completionTime = curr + process[i].burstTime;
curr = process[i].completionTime;
process[i].turnAroundTime = process[i].completionTime - process[i].arrivalTime;
process[i].waitingTime = process[i].turnAroundTime - process[i].burstTime;
avgTurnAroundTime += process[i].turnAroundTime;
avgresponseTime += process[i].responseTime;
avgwaitingTime += process[i].waitingTime;
}
cout << " AT\tP\tBT\tCT\tTAT\tWT\tRT" << endl;
for (int i = 0; i < n; i++)
{
cout << process[i].name << ": ";
cout << process[i].arrivalTime << "\t" << process[i].priority << "\t" << process[i].burstTime << "\t" << process[i].completionTime << "\t" << process[i].turnAroundTime << "\t" << process[i].waitingTime << "\t" << process[i].responseTime;
cout << endl;
}
cout << endl;
cout << setprecision(2) << fixed;
cout << "Average TurnAroundTime: " << avgTurnAroundTime / n << "\n";
cout << "Average WaitingTime: " << avgwaitingTime / n << "\n";
cout << "Average ResponseTime: " << avgresponseTime / n << "\n";
cout << endl;
cout << "Gantt Chart" << endl;
cout << process[0].arrivalTime << " |" << process[0].name << "| ";
for (int i = 0; i < n - 1; i++)
{
cout << process[i].completionTime << " |" << process[i + 1].name << "| ";
}
cout << process[n - 1].completionTime;
cout << endl;
}