-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprior.cpp
115 lines (98 loc) · 2.28 KB
/
prior.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
#include<iostream>
using namespace std;
void sort(int size, int priority[], int arrival[], int burst[]){
int var1,var2,var3;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(priority[i]>priority[j])
{
var1 = priority[i];
priority[i] = priority[j];
priority[j] = var1;
var2 = arrival[i];
arrival[i] = arrival[j];
arrival[j] = var2;
var3 = burst[i];
burst[i] = burst[j];
burst[j] = var3;
}
}
}
}
void driver(int n,int burst[50],int arrival[50],int priority[50])
{
int turn[50],bt[50],wait[50];
int check = 0;
int count = 0;
float avg_turn = 0.0;
float avg_wait = 0.0;
sort(n,priority,arrival,burst); //sorting arrival time and busrt time on the basis of priority
for(int i=0;i<n;i++)
{
bt[i] = burst[i];
}
while(true)
{
if(check==n)
break;
for(int i=0;i<n;i++)
{
if(bt[i]==0)
continue;
if((arrival[i]<=count))
{
count = count + bt[i];
bt[i] = 0;
turn[i] = count;
check = check + 1;
break;
}
}
}
for(int i=0;i<n;i++)
{
turn[i] = turn[i] - arrival[i];
}
for(int i=0;i<n;i++)
{
wait[i] = turn[i] - burst[i];
}
cout<<endl;
cout<<"Process\tPriority\tArrival time\tBurst time\tWaiting time\tTurnaround time"<<endl;
for(int i=0;i<n;i++)
{
avg_wait+=wait[i];
avg_turn+=turn[i];
cout<<"P("<<i+1<<")-\t\t"<<priority[i]<<"\t\t"<<arrival[i]<<"\t\t"<<burst[i]<<"\t\t"<<wait[i]<<"\t\t"<<turn[i]<<endl;
}
cout<<"The average wait time for all processes is: "<<(avg_wait/n)<<endl;
cout<<"The average turnaround time for all processes is: "<<(avg_turn/n)<<endl;
}
int main()
{
int processes,burst[50],arrival[50],priority[50];
cout<<"Enter the total number of processes:"<<endl;
cin>>processes;
cout<<"Enter the arrival time for all processes: "<<endl;
for(int i=0;i<processes;i++)
{
cout<<"Enter arrival time: ";
cin>>arrival[i];
}
cout<<"Enter the burst time for all processes: "<<endl;
for(int i=0;i<processes;i++)
{
cout<<"Enter burst time: ";
cin>>burst[i];
}
cout<<"Enter the priority for all processes: "<<endl;
for(int i=0;i<processes;i++)
{
cout<<"Enter priority: ";
cin>>priority[i];
}
driver(processes,burst,arrival,priority); //driver function to calculate wait and turnaround time
return 0;
}