Write a program that simulates Round Robin CPU scheduling algorithm.
#include<stdio.h>
int main() {
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
printf("Enter number of processes:\n");
scanf("%d",&n);
printf("Enter burst time for sequences:\n");
for(i=0;i<n;i++) {
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:\n");
scanf("%d",&tq);
do {
for(i=0,count=0;i<n;i++) {
temp=tq;
if(st[i]==0) {
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else if(st[i]>=0) {
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
} while(n!=count);
for(i=0;i<n;i++) {
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn Around Time\n");
for(i=0;i<n;i++)
printf("%d \t%d \t%d \t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Average waiting time is %f \nAverage turn around time is %f",awt,atat);
return 0;
}
int main() {
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
float awt=0.0,atat=0.0;
printf("Enter number of processes:\n");
scanf("%d",&n);
printf("Enter burst time for sequences:\n");
for(i=0;i<n;i++) {
scanf("%d",&bt[i]);
st[i]=bt[i];
}
printf("Enter time quantum:\n");
scanf("%d",&tq);
do {
for(i=0,count=0;i<n;i++) {
temp=tq;
if(st[i]==0) {
count++;
continue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else if(st[i]>=0) {
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
} while(n!=count);
for(i=0;i<n;i++) {
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=(float)swt/n;
atat=(float)stat/n;
printf("Process_no \t Burst time \t Wait time \t Turn Around Time\n");
for(i=0;i<n;i++)
printf("%d \t%d \t%d \t%d\n",i+1,bt[i],wt[i],tat[i]);
printf("Average waiting time is %f \nAverage turn around time is %f",awt,atat);
return 0;
}
Enter number of processes:
3
Enter burst time for sequences:
24 3 3
Enter time quantum:
4
Average waiting time is 5.666667
Average turn around time is 15.666667
3
Enter burst time for sequences:
24 3 3
Enter time quantum:
4
Process_no | Burst time | Wait time | Turn Around Time |
1 | 24 | 6 | 30 |
2 | 3 | 4 | 7 |
3 | 3 | 7 | 10 |
Average turn around time is 15.666667
No comments:
Post a Comment