-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriority_P.cpp
160 lines (139 loc) · 4.27 KB
/
Priority_P.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
using namespace std;
struct Process
{
int id;
int arrival_time;
int burst_time;
int priority;
int turnaround_time;
int finish_time;
int wait_time;
int remaining_time;
bool executed;
Process()
{
turnaround_time = 0;
finish_time = 0;
wait_time = 0;
remaining_time = 0;
executed = false;
}
};
void swap(Process &a, Process &b)
{
Process temp = a;
a = b;
b = temp;
}
void bubbleSort(Process processes[], int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (processes[j].arrival_time > processes[j + 1].arrival_time)
{
swap(processes[j], processes[j + 1]);
}
}
}
}
int findProcessWithHighestPriority(Process processes[], int n, int currentTime)
{
int highestPriority = -1;
int processIndex = -1;
for (int i = 0; i < n; i++)
{
if (!processes[i].executed && processes[i].arrival_time <= currentTime)
{
if (highestPriority == -1 || processes[i].priority < highestPriority)
{
highestPriority = processes[i].priority;
processIndex = i;
}
}
}
return processIndex;
}
int main()
{
int n;
cout << "Enter the number of processes: ";
cin >> n;
Process processes[n];
int currentTime = 0;
for (int i = 0; i < n; i++)
{
processes[i].id = i + 1;
cout << "Enter arrival time for process " << i + 1 << ": ";
cin >> processes[i].arrival_time;
cout << "Enter burst time for process " << i + 1 << ": ";
cin >> processes[i].burst_time;
cout << "Enter priority for process " << i + 1 << ": ";
cin >> processes[i].priority;
processes[i].remaining_time = processes[i].burst_time;
}
bubbleSort(processes, n);
cout << "Process ID\tArrival Time\tBurst Time\tPriority\tTurnaround Time\tFinish Time\tWait Time\n";
int executedCount = 0;
while (executedCount < n)
{
int processIndex = findProcessWithHighestPriority(processes, n, currentTime);
if (processIndex != -1)
{
Process ¤tProcess = processes[processIndex];
if (currentProcess.remaining_time == currentProcess.burst_time)
{
currentProcess.wait_time = currentTime - currentProcess.arrival_time;
}
currentProcess.remaining_time--;
currentTime++;
if (currentProcess.remaining_time == 0)
{
currentProcess.finish_time = currentTime;
currentProcess.turnaround_time = currentProcess.finish_time - currentProcess.arrival_time;
currentProcess.executed = true;
executedCount++;
}
}
else
{
currentTime++;
}
}
for (int i = 0; i < n; i++)
{
Process ¤tProcess = processes[i];
cout << currentProcess.id << "\t\t" << currentProcess.arrival_time << "\t\t" << currentProcess.burst_time << "\t\t"
<< currentProcess.priority << "\t\t" << currentProcess.turnaround_time << "\t\t" << currentProcess.finish_time << "\t\t"
<< currentProcess.wait_time << endl;
}
// Gantt Chart
cout << "\nGantt Chart:" << endl;
int time = 0;
while (executedCount < n)
{
int processIndex = findProcessWithHighestPriority(processes, n, time);
if (processIndex != -1)
{
Process ¤tProcess = processes[processIndex];
cout << "| P" << currentProcess.id << " ";
currentProcess.remaining_time--;
time++;
if (currentProcess.remaining_time == 0)
{
currentProcess.finish_time = time;
currentProcess.turnaround_time = currentProcess.finish_time - currentProcess.arrival_time;
currentProcess.executed = true;
executedCount++;
}
}
else
{
time++;
}
}
cout << "|\n";
return 0;
}