Repository navigation
Expand file tree
/
Copy pathpriority.cpp
More file actions
79 lines (62 loc) · 2.43 KB
/
Copy pathpriority.cpp
File metadata and controls
79 lines (62 loc) · 2.43 KB
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
#include <iostream>
using namespace std;
void priorityScheduling(int at[], int burst_time[], int priority[], int n) {
int completion_time[n];
int waiting_time[n];
int turnaround_time[n];
bool completed[n] = {false};
int total_waiting_time = 0;
int total_turnaround_time = 0;
int current_time = 0;
while (true) {
int min_priority = 999;
int min_index = -1;
// Find the process with the highest priority that has arrived
for (int i = 0; i < n; ++i) {
if (!completed[i] && priority[i] < min_priority && current_time >= at[i]) {
min_priority = priority[i];
min_index = i;
}
}
// If no process is found, break the loop
if (min_index == -1)
break;
// Update completion time
completion_time[min_index] = current_time + burst_time[min_index];
// Calculate turnaround time
turnaround_time[min_index] = completion_time[min_index] - at[min_index];
// Calculate waiting time
waiting_time[min_index] = turnaround_time[min_index] - burst_time[min_index];
// Update total waiting and turnaround time
total_waiting_time += waiting_time[min_index];
total_turnaround_time += turnaround_time[min_index];
// Mark the process as completed
completed[min_index] = true;
// Update current time
current_time = completion_time[min_index];
}
// Calculate average waiting time and average turnaround time
float avg_waiting_time = (float)total_waiting_time / n;
float avg_turnaround_time = (float)total_turnaround_time / n;
cout << "Average Waiting Time: " << avg_waiting_time << endl;
cout << "Average Turnaround Time: " << avg_turnaround_time << endl;
}
int main() {
int n;
cout << "Enter the number of processes: ";
cin >> n;
int at[n];
int burst_time[n];
int priority[n];
cout << "Enter arrival time, burst time, and priority for each process:\n";
for (int i = 0; i < n; ++i) {
cout << "Process " << i + 1 << " Arrival Time: ";
cin >> at[i];
cout << "Process " << i + 1 << " Burst Time: ";
cin >> burst_time[i];
cout << "Process " << i + 1 << " Priority: ";
cin >> priority[i];
}
priorityScheduling(at, burst_time, priority, n);
return 0;
}