-
Notifications
You must be signed in to change notification settings - Fork 1
/
sjf.cpp
78 lines (61 loc) · 1.6 KB
/
sjf.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
#include<iostream>
using namespace std;
int main()
{
int n,bt[20],at[20],wt[20],tat[20],ct[20],avwt=0,avtat=0,i,j,pos,temp,p[20];
cout<<"Enter total number of processes(maximum 20):";
cin>>n;
//enter the burst process time
cout<<"\nEnter Process Burst Time\n";
for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>bt[i];
}
//enter the arrival time
cout<<"\nEnter Process Arrival Time\n";
for(i=0;i<n;i++)
{
cout<<"P["<<i+1<<"]:";
cin>>at[i];
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is 0
//calculating waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
}
cout<<"\nProcess\t\tBurst Time\tWaiting Time\tTurnaround Time";
//calculating turnaround time
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
avwt+=wt[i];
avtat+=tat[i];
cout<<"\nP["<<i+1<<"]"<<"\t\t"<<bt[i]<<"\t\t"<<wt[i]<<"\t\t"<<tat[i];
}
avwt/=i;
avtat/=i;
cout<<"\n\nAverage Waiting Time:"<<avwt;
cout<<"\nAverage Turnaround Time:"<<avtat;
return 0;
}