-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path6_PRIORITY_PREAMPTIVE.cxx
229 lines (191 loc) · 4.83 KB
/
6_PRIORITY_PREAMPTIVE.cxx
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
#include<iostream>
using namespace std;
//defining the process as structure
struct process
{
int id; //Process Id
int pr; //Process Priority
int bt; //Process Burst Time
int bti; //Process initial burst time
int at; //Process arrival time
int wt; //Process Waiting Time
int ct; //Process Completion time
int tat; //Process Turn Aroung Time
int rt; //Process Response Time
int flag; //Flag to check wheather a process is executed or not
};
//Global Variables
int sum_bt=0;
int ct=0;
int nopa=0; //number of process which is aririved till
int nopwhp=0; //number of process with highest priority
int nopmat=0;
struct process * arrived[20]; //will contain all of the process arrived till time
struct process * highest[20]; //will contains all the process with highest priority and arrived and incomplete
struct process *executable[20]; //will contain elements of same priority and same minimum arrival time
//showdata() function shows the process data
void showdata(struct process * array[],int n)
{
struct process *p;
cout<<"\nPro\t"<<"Arr\t"<<"Pri\t"<<"Bur\tCom\tTurn\tWait\tRes\t\n";
for(int i=0;i<n;i++)
{
p=array[i];
cout<<p->id<<"\t"<<p->at<<"\t"<<p->pr<<"\t"<<p->bti<<"\t"<<p->ct<<"\t"<<p->tat<<"\t"<<p->wt<<"\t"<<p->rt<<"\t"<<"\n";
}
}
//To find sum of burst times
void sbt(struct process *a[],int n)
{
int sum=0;
struct process *p;
for(int i=0;i<n;i++)
{
p=a[i];
sum=sum+p->bti;
}
sum_bt=sum;
}
//To list all the process which is arrived and not completed yet
void update_arrival(struct process *a[],int n)
{
struct process *p;
int k=0; //number of terms in arrived array
for(int i=0;i<n;i++)
{
p=a[i];
if(p->at<=ct && p->flag!=1)
{
arrived[k]=p;
k++;
}
}
nopa=k;
}
//To get the array of processes which are arrived,not completed and is of highest priority all together
void update_highest_priority()
{
int min;
int k=0;
struct process *p;
p=arrived[0];
min=p->pr;
for(int i=0;i<nopa;i++)
{
p=arrived[i];
if(p->pr<min)
min=p->pr;
}
for(int i=0;i<nopa;i++)
{
p=arrived[i];
if(p->pr==min )
{highest[k]=p;
k++;}
}
nopwhp=k;
}
//If highest priority array contains many elements then select process on basis of FCFS
void update_min_arrival_time()
{
int min;
int k=0;
struct process *p;
p=highest[0];
min=p->at;
for(int i=0;i<nopwhp;i++)
{
p=highest[i];
if(p->at<min)
min=p->at;
}
for(int i=0;i<nopwhp;i++)
{
p=highest[i];
if(p->at==min)
{executable[k]=p;
k++;}
}
nopmat=k;
}
//Main priority preamption function
void prior_preamp(struct process *a[],int n)
{
struct process *exec;
while(ct!=sum_bt)
{
update_arrival(a,n);
update_highest_priority();
update_min_arrival_time();
exec=executable[0];
cout<<exec->id<<"->";
//Increase time by one unit and reduce burst time by one unit
exec->bt=exec->bt-1;
//Process flag is zero means process is executing first time so update the response time and change flag to 2 flag 2 means process executed once but not fully executed
if(exec->flag==0)
{
exec->rt=ct-exec->at;
exec->flag=2;
}
ct=ct+1;
//If burst time of executable is zero means process is fully executed hence update its completiontime,turn around time,waiting time,flag=1(means process executed fully)
if(exec->bt==0)
{
exec->flag=1;
exec->ct=ct;
exec->tat=exec->ct-exec->at;
exec->wt=exec->tat-exec->bti;
}
}
}
//To find average waiting,turnaround and response time
void awt_atat_art(struct process *a[],int n)
{
struct process *p;
float awt=0,atat=0,art=0;
for(int i=0;i<n;i++)
{
p=a[i];
awt=awt+p->wt;
atat=atat+p->tat;
art=art+p->rt;
}
awt=awt/n;
atat=atat/n;
art=art/n;
cout<<"\nAverage Response Time is "<<art<<"\nAverage Turn Around Time is "<<atat<<"\nAverage Waiting Time is "<<awt;
}
int main()
{
int n;
cout<<"Enter number of process ";
cin>>n;
//Take input of process
struct process * array[20];
for(int i=0;i<n;i++)
{
int b,a,pr,ct=0;
struct process *p;
p=(struct process *) malloc(sizeof(struct process));
array[i]=p;
p->id=i+1;
cout<<"\nEnter arrival time and priority and burst time for process"<<i+1<<" ";
cin>>a;
cin>>pr;
cin>>b;
p->at=a;
p->pr=pr;
p->bt=b;
p->bti=b;
p->flag=0;
p->wt=0;
p->tat=0;
p->rt=0;
p->ct=ct;
}
sbt(array,n);
cout<<"\nProcess execution order is ";
prior_preamp(array,n);
showdata(array,n);
awt_atat_art(array,n);
}