-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
220 lines (179 loc) · 5.17 KB
/
scheduler.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include<iostream>
using namespace std;
float fcfs_avg_wt = 0;
float fcfs_avg_ta = 0;
float sjf_avg_wt = 0;
float sjf_avg_ta = 0;
float srtf_avg_wt = 0;
float srtf_avg_ta = 0;
int p[10]; // Processes
int bt[10]; // Burst Times
int wt[10]; // Waiting Times
int tat[10]; // Turnaround Times
void clear()
{
memset(p, 0, sizeof(int) * 10);
memset(bt, 0, sizeof(int) * 10);
memset(wt, 0, sizeof(int) * 10);
memset(tat, 0, sizeof(int) * 10);
}
void fcfs(int n)
{
clear();
cout << "\nEnter Burst Time:";
for (int i = 0; i < n; ++i)
{
cout << "\np" << i + 1 << ":";
cin >> bt[i];
p[i] = i + 1;
}
wt[0] = 0;
for (int i = 1; i < n; ++i)
wt[i] = bt[i - 1] + wt[i - 1];
for (int i = 0; i < n; ++i)
tat[i] = bt[i] + wt[i];
cout << "Processes " << " Burst time "
<< " Waiting time " << " Turn around time\n";
int total_wt = 0, total_tat = 0;
for (int i = 0; i < n; ++i)
{
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << i + 1 << "\t\t" << bt[i] << "\t "
<< wt[i] << "\t\t " << tat[i] << "\n";
}
fcfs_avg_wt = (float)total_wt / (float)n;
fcfs_avg_ta = (float)total_tat / (float)n;
cout << "\nAverage waiting time = " << fcfs_avg_wt;
cout << "\nAverage turn around time = " << fcfs_avg_ta;
}
void sjf(int n)
{
int total = 0, pos, temp;
clear();
cout << "\nEnter Burst Time:";
for (int i = 0; i < n; ++i)
{
cout << "\np" << i + 1 << ":";
cin >> bt[i];
p[i] = i + 1;
}
for (int i = 0; i < n; ++i)
{
pos = i;
for (int 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;
for (int i = 1; i < n; ++i)
{
wt[i] = 0;
for (int j = 0; j < i; ++j)
wt[i] += bt[j];
total += wt[i];
}
total = 0;
cout << "\nProcess Burst Time Waiting Time Turnaround Time";
for (int i = 0; i < n; ++i)
{
tat[i] = bt[i] + wt[i];
total += tat[i];
cout << "\np" << p[i] << "\t\t" << bt[i] << "\t\t" << wt[i] << "\t\t" << tat[i];
}
sjf_avg_ta = (float)total / (float)n;
sjf_avg_wt = (float)total / (float)n;
cout << "\nAverage Waiting Time = " << sjf_avg_wt;
cout << "\nAverage Turnaround Time = "<< sjf_avg_ta;
}
void srtf(int n)
{
int a[10], x[10];
int completion[10];
int smallest, count = 0, time;
double avg = 0, tt = 0, end;
clear();
for (int i = 0; i < n; ++i)
{
cout << "\nEnter arrival time of process: ";
cin >> a[i];
}
for (int i = 0; i < n; ++i)
{
cout << "\nEnter burst time of process: ";
cin >> bt[i];
}
for (int i = 0; i < n; ++i)
{
x[i] = bt[i];
}
bt[9] = 9999;
for (time = 0; count != n; ++time)
{
smallest = 9;
for (int i = 0; i < n; ++i)
{
if (a[i] <= time && bt[i] < bt[smallest] && bt[i]>0)
smallest = i;
}
bt[smallest]--;
if (bt[smallest] == 0)
{
count++;
end = time + 1;
completion[smallest] = end;
wt[smallest] = end - a[smallest] - x[smallest];
tat[smallest] = end - a[smallest];
}
}
cout << "Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\tCompletion Time\n";
for (int i = 0; i < n; ++i)
{
cout << "p" << i + 1 << "\t\t" << x[i] << "\t\t" << a[i] << "\t\t" << wt[i] << "\t\t" << tat[i] << "\t\t" << completion[i] << endl;
avg += wt[i];
tt += tat[i];
}
srtf_avg_wt = avg / n;
srtf_avg_ta = tt / n;
cout << "\n\nAverage waiting time = " << srtf_avg_wt << "\n";
cout << "Average Turnaround time = " << srtf_avg_ta << "\n";
}
int main()
{
int n = 0;
cout << "\nEnter number of processes: ";
cin >> n;
if (n > 10 || n <= 0)
{
cout << "\nInvalid number of processes (valid = 1...10)\n Setting it to 3\n";
n = 3;
}
cout << "\n--------------------------------------\n";
cout << " FCFS \n";
cout << "--------------------------------------\n";
fcfs(n);
cout << "\n--------------------------------------\n";
cout << " SJF \n";
cout << "--------------------------------------\n";
sjf(n);
cout << "\n--------------------------------------\n";
cout << " SRTF \n";
cout << "--------------------------------------\n";
srtf(n);
cout << "\n--------------------------------------\n";
cout << " Results \n";
cout << "--------------------------------------\n";
cout << "\n\t\tAVG WT\t\tAVG TA";
cout << "\nFCFS\t\t" << fcfs_avg_wt << "\t\t" << fcfs_avg_ta;
cout << "\nSJF\t\t" << sjf_avg_wt << "\t\t" << sjf_avg_ta;
cout << "\nSRTF\t\t" << srtf_avg_wt << "\t\t" << srtf_avg_ta;
return 0;
}