-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSJF.cpp
121 lines (103 loc) · 2.07 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
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
#include<iostream>
using namespace std;
void sort(int size, int burst[], int arrival[]){
int var1, var2;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(burst[i]>burst[j])
{
var1 = burst[i];
burst[i] = burst[j];
burst[j] = var1;
var2 = arrival[i];
arrival[i] = arrival[j];
arrival[j] = var2;
}
}
}
}
void sjf(int n,int burst[50],int arrival[50])
{
int turn[50],bt[50],at[50],wait[50];
int check = 0;
float avg_turn = 0.0;
float avg_wait = 0.0;
int var1,var2,temp;
for(int i=0;i<n;i++)
{
at[i] = arrival[i];
}
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(at[i]>at[j])
{
temp = at[i];
at[i] = at[j];
at[j] = temp;
}
}
}
int count = at[0];
sort(n,burst,arrival); //sorting on the basis of burst time
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((arrival[i]<=count) && (bt[i]!=0))
{
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\t\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<<")-\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];
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];
}
sjf(processes,burst,arrival);
}