arjun ex4
arjun ex4
953623205004
Experiment – 4
To Implement various CPU scheduling alogrithms.
1) FCFS :
#include <stdio.h>
int main() {
int n = 3, bt[] = {5, 8, 12}, wt[3] = {0}, tat[3], total_wt = 0, total_tat = 0;
for (int i = 1; i < n; i++) wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i];
printf("P BT WT TAT\n");
for (int i = 0; i < n; i++) {
printf("%d %d %d %d\n", i + 1, bt[i], wt[i], tat[i]);
total_wt += wt[i], total_tat += tat[i];
}
printf("Avg WT: %.2f, Avg TAT: %.2f\n", (float)total_wt / n, (float)total_tat / n);
}
Output:
ARJUN G
953623205004
2) SJF :
#include <stdio.h>
int main() {
int n = 3, p[] = {1, 2, 3}, bt[] = {1, 4, 7}, temp, wt[3] = {0}, tat[3], total_wt = 0,
total_tat = 0;
for (int i = 0; i < n - 1; i++) for (int j = i + 1; j < n; j++)
if (bt[i] > bt[j]) temp = bt[i], bt[i] = bt[j], bt[j] = temp, temp = p[i], p[i] = p[j], p[j] =
temp;
for (int i = 1; i < n; i++) wt[i] = wt[i - 1] + bt[i - 1];
for (int i = 0; i < n; i++) tat[i] = bt[i] + wt[i];
printf("P BT WT TAT\n");
for (int i = 0; i < n; i++) {
printf("%d %d %d %d\n", p[i], bt[i], wt[i], tat[i]);
total_wt += wt[i], total_tat += tat[i];
}
printf("Avg WT: %.2f, Avg TAT: %.2f\n", (float)total_wt / n, (float)total_tat / n);
}
OUTPUT :
3) Priority Scheduling :
#include <stdio.h>
ARJUN G
953623205004
int main() {
int n = 3, t = 0, p[] = {2, 1, 3}, a[] = {0, 1, 2}, b[] = { 3 ,4 , 8}, c[3], tt[3], wt[3],
pid[] = {1, 2, 3};
OUTPUT :
ARJUN G
953623205004
4) ROUND-ROBIN :
#include <stdio.h>
int main() {
int n, tq, t = 0, c = 0;
scanf("%d%d", &n, &tq);
int p[n], a[n], b[n], r[n], ct[n], tt[n], wt[n];
while (c < n) {
for (int i = 0; i < n; i++) {
if (r[i] > 0) {
if (r[i] <= tq) {
t += r[i];
r[i] = 0;
ct[i] = t;
c++;
} else {
t += tq;
r[i] -= tq;
}
}
ARJUN G
953623205004
}
}
return 0;
}
OUTPUT :