-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.cpp
More file actions
112 lines (76 loc) · 1.77 KB
/
sjf.cpp
File metadata and controls
112 lines (76 loc) · 1.77 KB
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
#include<iostream>
using namespace std;
int main()
{
int n,temp,tt=0,min,d,i,j;
float avg_tat=0,avg_wt=0,t_tat=0,t_wt=0;
cout<<"Enter number of processes: ";
cin>>n;
int p[n], a[n] ,b[n] ,c[n] ,tat[n] , wt[n];
cout<< "\nEnter process sequence:";
for(i=0;i<n; i++)
{
cin>> p[i];
}
cout<< "\nEnter the arrival times :\n";
for(i=0;i<n; i++)
{
cin>> a[i];
}
cout<< "Enter the CPU times :\n";
for(i=0; i<n; i++)
{
cin>> b[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
temp=p[i];
p[i]=p[j];
p[j]=temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
min=a[0];
for(i=0;i<n;i++)
{
if(min>a[i])
{
min=a[i];
d=i;
}
}
tt=min;
c[d]=tt+b[d];
tt=c[d];
for(i=0;i<n;i++)
{
if(a[i]!=min)
{
c[i]=b[i]+tt;
tt=c[i];
}
}
for(i=0;i<n;i++)
{
tat[i]=c[i]-a[i];
t_tat=t_tat+tat[i];
wt[i]=tat[i]-b[i];
t_wt +=wt[i];
cout<<"\nProcess : "<<p[i]<<" Waiting time : " << wt[i]<<" Turnaround Time: "<<tat[i]<<endl;
}
avg_tat=t_tat/n;
avg_wt=t_wt/n;
cout<< "\nAverage Turnaround Time: "<<avg_tat<<endl;
cout<<"\nAverage Waiting Time: "<<avg_wt<<endl;
return 0;
}