8. To write a program in C to calculate waiting time, turn around time and average waiting time when non preemptive SJF is used
#include <stdio.h>
#include <conio.h>
void main() {
clrscr();
int bt[20], p[20], wt[20], tat[20], i, j, n;
int total = 0, pos, temp;
float awt, atat;
printf("enter the no. of processes: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("enter the burst time for process: ");
scanf("%d", &bt[i]);
p[i] = i + 1;
}
// SJF sorting
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;
// Calculate waiting time
for (i = 1; i < n; i++) {
wt[i] = 0;
for (j = 0; j < i; j++)
wt[i] += bt[j];
total += wt[i];
}
awt = (float)total / n;
total = 0;
printf("\nProcess:\n");
printf("\t\t");
for (i = 0; i < n; i++) {
printf("P%d\t", p[i]);
}
printf("\n\t\t");
for (i = 0; i < n; i++) {
printf("%d\t", bt[i]);
}
printf("\n\t\t");
for (i = 0; i < n; i++) {
printf("%d\t", wt[i]);
}
// Calculate turnaround time
printf("\n");
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
total += tat[i];
}
atat = (float)total / n;
printf("\nAverage waiting time = %f", awt);
printf("\nAverage turnaround time = %f", atat);
getch();
}
Comments
Post a Comment