forked from subho57/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPreemptive.cpp
231 lines (204 loc) · 6.72 KB
/
Preemptive.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
About:In priority scheduling, a number is assigned to each process that indicates its priority level.
Lower the number, higher is the priority. In this type of scheduling algorithm, if a newer process arrives, that is having a higher
priority than the currently running process, then the currently running process is preempted.
Program for preemptive priority
#include <bits/stdc++.h>
using namespace std;
struct Process {
int processID;
int burstTime;
int tempburstTime;
int responsetime;
int arrivalTime;
int priority;
int outtime;
int intime;
};
// It is used to include all the valid and eligible
// processes in the heap for execution. heapsize defines
// the number of processes in execution depending on
// the current time currentTime keeps a record of
// the current CPU time.
void insert(Process Heap[], Process value, int* heapsize,int* currentTime)
{
int start = *heapsize, i;
Heap[*heapsize] = value;
if (Heap[*heapsize].intime == -1)
Heap[*heapsize].intime = *currentTime;
++(*heapsize);
// Ordering the Heap
while (start != 0 && Heap[(start - 1) / 2].priority > Heap[start].priority) {
Process temp = Heap[(start - 1) / 2];
Heap[(start - 1) / 2] = Heap[start];
Heap[start] = temp;
start = (start - 1) / 2;
}
}
// It is used to reorder the heap according to
// priority if the processes after insertion
// of new process.
void order(Process Heap[], int* heapsize, int start)
{
int smallest = start;
int left = 2 * start + 1;
int right = 2 * start + 2;
if (left < *heapsize && Heap[left].priority <Heap[smallest].priority)
smallest = left;
if (right < *heapsize && Heap[right].priority <Heap[smallest].priority)
smallest = right;
// Ordering the Heap
if (smallest != start) {
Process temp = Heap[smallest];
Heap[smallest] = Heap[start];
Heap[start] = temp;
order(Heap, heapsize, smallest);
}
}
// This function is used to find the process with
// highest priority from the heap. It also reorders
// the heap after extracting the highest priority process.
Process extractminimum(Process Heap[], int* heapsize,int* currentTime)
{
Process min = Heap[0];
if (min.responsetime == -1)
min.responsetime = *currentTime - min.arrivalTime;
--(*heapsize);
if (*heapsize >= 1) {
Heap[0] = Heap[*heapsize];
order(Heap, heapsize, 0);
}
return min;
}
// Compares two intervals according to staring times.
int compare(Process p1, Process p2)
{
if (p1.arrivalTime < p2.arrivalTime)
return 1;
else
return 0;
}
// This function is responsible for executing
// the highest priority extracted from Heap[].
void scheduling(Process Heap[], Process array[], int n,int* heapsize, int* currentTime)
{
if (heapsize == 0)
return;
Process min = extractminimum(Heap, heapsize, currentTime);
min.outtime = *currentTime + 1;
--min.burstTime;
printf("process id = %d\tcurrent_time = %d\n",min.processID, *currentTime);
// If the process is not yet finished
// insert it back into the Heap*/
if (min.burstTime > 0) {
insert(Heap, min, heapsize, currentTime);
return;
}
for (int i = 0; i < n; i++)
if (array[i].processID == min.processID) {
array[i] = min;
break;
}
}
// This function is responsible for
// managing the entire execution of the
// processes as they arrive in the CPU
// according to their arrival time.
void priority(Process array[], int n)
{
sort(array, array + n, compare);
int totalwaitingtime = 0, totalbursttime = 0,
totalturnaroundtime = 0, i, insertedprocess = 0,
heapsize = 0, currentTime = array[0].arrivalTime,
totalresponsetime = 0;
Process Heap[4 * n];
// Calculating the total burst time
// of the processes
for (int i = 0; i < n; i++) {
totalbursttime += array[i].burstTime;
array[i].tempburstTime = array[i].burstTime;
}
// Inserting the processes in Heap
// according to arrival time
do {
if (insertedprocess != n) {
for (i = 0; i < n; i++) {
if (array[i].arrivalTime == currentTime) {
++insertedprocess;
array[i].intime = -1;
array[i].responsetime = -1;
insert(Heap, array[i], &heapsize, ¤tTime);
}
}
}
scheduling(Heap, array, n, &heapsize, ¤tTime);
++currentTime;
if (heapsize == 0 && insertedprocess == n)
break;
} while (1);
for (int i = 0; i < n; i++) {
totalresponsetime += array[i].responsetime;
totalwaitingtime += (array[i].outtime - array[i].intime -
array[i].tempburstTime);
totalbursttime += array[i].burstTime;
}
printf("\n\nAverage waiting time = %f\n",((float)totalwaitingtime / (float)n));
printf("\nAverage response time =%f\n",((float)totalresponsetime / (float)n));
printf("\nAverage turn around time = %f\n",((float)(totalwaitingtime + totalbursttime) / (float)n));
}
// Driver code
int main()
{
int n, i;
Process a[5];
printf("Enter the no.of Process:-");
cin>>n;
printf("Arival_Time Burst_Time Priority\n");
for(int i=0;i<n;i++)
{
a[i].processID=i+1;
cin>>a[i].arrivalTime;
cin>>a[i].burstTime;
cin>>a[i].priority;
}
priority(a, n);
return 0;
}
Output:-
Enter the no.of Process:-5
Arival_Time Burst_Time Priority
1 4 3
3 7 2
0 9 5
4 3 4
2 5 1
process id = 3 current_time = 0
process id = 1 current_time = 1
process id = 5 current_time = 2
process id = 5 current_time = 3
process id = 5 current_time = 4
process id = 5 current_time = 5
process id = 5 current_time = 6
process id = 2 current_time = 7
process id = 2 current_time = 8
process id = 2 current_time = 9
process id = 2 current_time = 10
process id = 2 current_time = 11
process id = 2 current_time = 12
process id = 2 current_time = 13
process id = 1 current_time = 14
process id = 1 current_time = 15
process id = 1 current_time = 16
process id = 4 current_time = 17
process id = 4 current_time = 18
process id = 4 current_time = 19
process id = 3 current_time = 20
process id = 3 current_time = 21
process id = 3 current_time = 22
process id = 3 current_time = 23
process id = 3 current_time = 24
process id = 3 current_time = 25
process id = 3 current_time = 26
process id = 3 current_time = 27
Average waiting time = 9.600000
Average response time =3.400000
Average turn around time = 15.200000