-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathPriority-Non-Preemtive-Code.cpp
119 lines (93 loc) · 3.82 KB
/
Priority-Non-Preemtive-Code.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Priority Non Preemptive with Arrival Time
// Mounish Sai (https://github.com/pvsmounish)
#include<iostream>
#include<limits>
using namespace std;
class Process{
public:
string processName;
int arrivalTime;
int burstTime;
int priority;
int remainingTime;
int responseTime;
int completionTime;
int waitingTime;
int turnAroundTime;
void initialize(){
remainingTime = burstTime;
}
};
int main(){
int numOfProcesses;
cout << "Enter no. of processes: ";
cin >> numOfProcesses;
Process processes[numOfProcesses];
for(int n=0;n<numOfProcesses;n++){
cout << "\nEnter Process Name for " << (n+1) << ": ";
cin >> processes[n].processName;
cout << "Enter Arrival Time for Process " << (n+1) << ": ";
cin >> processes[n].arrivalTime;
cout << "Enter Burst Time for Process " << (n+1) << ": ";
cin >> processes[n].burstTime;
cout << "Enter Priority for Process " << (n+1) << ": ";
cin >> processes[n].priority;
processes[n].initialize();
}
cout << "\n" << endl;
for(int i=0;i<numOfProcesses-1;i++){
for(int j=i+1;j<numOfProcesses;j++){
if(processes[j].arrivalTime < processes[i].arrivalTime){
Process temp = processes[j];
processes[j] = processes[i];
processes[i] = temp;
}
}
}
int currentTime = 0;
while(true){
int currentHighestPriorityIndex = -1;
int currentHighestPriority = numeric_limits<int>::max();
bool isAllCompleted = true;
for(int i=0;i<numOfProcesses;i++){
if(processes[i].remainingTime > 0){
isAllCompleted = false;
if(processes[i].arrivalTime <= currentTime){
if(processes[i].priority < currentHighestPriority){
currentHighestPriority = processes[i].priority;
currentHighestPriorityIndex = i;
}
}
}
}
if(isAllCompleted){
break;
}
processes[currentHighestPriorityIndex].responseTime = currentTime;
processes[currentHighestPriorityIndex].remainingTime = 0;
currentTime += processes[currentHighestPriorityIndex].burstTime;
processes[currentHighestPriorityIndex].completionTime = currentTime;
}
int sumResponseTime = 0;
int sumCompletionTime = 0;
int sumWaitingTime = 0;
int sumTurnAroundTime = 0;
for(int n=0;n<numOfProcesses;n++){
cout << "\nProcess " << processes[n].processName << ":\n";
cout << "Response Time: " << processes[n].responseTime << endl;
cout << "Completion Time: " << processes[n].completionTime << endl;
processes[n].turnAroundTime = processes[n].completionTime - processes[n].arrivalTime;
processes[n].waitingTime = processes[n].turnAroundTime - processes[n].burstTime;
cout << "Waiting Time: " << processes[n].waitingTime << endl;
cout << "Turn Around Time: " << processes[n].turnAroundTime << "\n" << endl;
sumResponseTime += processes[n].responseTime;
sumCompletionTime += processes[n].completionTime;
sumWaitingTime += processes[n].waitingTime;
sumTurnAroundTime += processes[n].turnAroundTime;
}
cout << "\n\nAverage Response Time for " << (numOfProcesses) << " Processes: " << (float) sumResponseTime/numOfProcesses;
cout << "\n\nAverage Completion Time for " << (numOfProcesses) << " Processes: " << (float) sumCompletionTime/numOfProcesses;
cout << "\n\nAverage Waiting Time for " << (numOfProcesses) << " Processes: " << (float) sumWaitingTime/numOfProcesses;
cout << "\n\nAverage Turn Around Time for " << (numOfProcesses) << " Processes: " << (float) sumTurnAroundTime/numOfProcesses;
return 0;
}