-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority_preemptive.cpp
77 lines (70 loc) · 1.84 KB
/
priority_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
#include <stdio.h>
#include <iomanip>
#include <iostream>
#include <algorithm> //Use for the sort function
#include <bits/stdc++.h>
#include <chrono>
#include <fstream>
using namespace std;
int main(){
int m;
cout<<"Enter number of processes: ";
cin>>m;
int n,k=1,t,b=0,min,temp[m],bt[m],wt[m],at[m],pr[m],tat[m];
float awt=0,atat=0;
for(int i=0;i<m;i++){
cout<<"\nEnter burst time of process"<<i+1<<": ";
cin>>bt[i];
cout<<"\nEnter arrival time of process"<<i+1<<": ";
cin>>at[i];
cout<<"\nEnter priority number of process"<<i+1<<": ";
cin>>pr[i];
}
for(int i=0;i<m;i++){
for(int j=0;j<m;j++){
if(at[i]<at[j]){
t=at[j];
at[j]=at[i];
at[i]=t;
t=bt[j];
bt[j]=bt[i];
bt[i]=t;
}
}
}
for(int j=0;j<m;j++){
b+=bt[j];
min=bt[k];
for(int i=k;i<m;++i){
min=pr[k];
if(b>=at[i]){
if(pr[i]<min){
t=at[k];
at[k]=at[i];
at[i]=t;
t=bt[k];
bt[k]=bt[i];
bt[i]=t;
t=pr[k];
pr[k]=pr[i];
pr[i]=t;
}
}
}
k++;
}
temp[0]=0;
cout<<"\nProcess\tBurst Time\tArrival Time\tPriority\tWaiting Time\tTurnaround Time\n";
for(int i=0;i<m;i++){
wt[i]=0;
tat[i]=0;
temp[i+1]=temp[i]+bt[i];
wt[i]=temp[i]-at[i];
tat[i]=wt[i]+bt[i];
awt+=wt[i];
atat+=tat[i];
cout<<i+1<<"\t"<<bt[i]<<"\t"<<at[i]<<"\t"<<pr[i]<<"\n";
}
cout<<"\nAverage Waiting Time: "<<awt/m;
cout<<"\nAverage Turnaround Time: "<<atat/m;
}