Round Robin Scheduling Algorithm Implementation in C
#include <stdio.h>
int main() { int n, q, i, j, count = 0, time = 0, sum = 0; int wt[10], tat[10], at[10], bt[10], rt[10], flag[10];
printf('Enter the number of processes to schedule: ');
scanf('%d', &n);
for (i = 0; i < n; i++) {
printf('Enter the burst time of process %d: ', i+1);
scanf('%d', &bt[i]);
rt[i] = bt[i];
at[i] = 0;
flag[i] = 0;
}
printf('Enter the length of the time quantum q: ');
scanf('%d', &q);
while (1) {
int done = 1;
for (i = 0; i < n; i++) {
if (rt[i] > 0) {
done = 0;
if (rt[i] > q) {
time += q;
rt[i] -= q;
} else {
time += rt[i];
wt[i] = time - bt[i];
rt[i] = 0;
}
}
}
if (done == 1) {
break;
}
}
for (i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
sum += tat[i];
}
printf('Schedule sequence: ');
for (i = 0; i < n; i++) {
for (j = 0; j < bt[i]; j++) {
printf('%d ', i+1);
}
}
printf('
');
float awt = (float) sum / n;
printf('AWT: %f ', awt);
float att = (float) sum / n;
printf('ATT: %f ', att);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/m62N 著作权归作者所有。请勿转载和采集!