This is a C program which implements one of the CPU Scheduling algorithm called Round Robin(RR).
Round robin algorithm is mainly used in time sharing systems , it is also similar to First Come First Served(FCFS) algorithm but FCFS does not have that time slicing switch.
All the jobs gets executed in this scheduling algorithm , so the advantage here is , its Starvation free. |
|
Download CodePROGRAM :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int bt_c[10],bt[10],i,j,n,m[50],r,q,e=0;
float f,avg=0;
printf("\nEnter how many jobs ?\t");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
printf("Enter burst time for job %d :\t",i);
scanf("%d",&bt[i]);
bt_c[i]=bt[i]; //stores job has how much burst time in array i
}
printf("\nEnter Quantum (time slice value) :\t");
scanf("%d",&q);
int max=0;
max=bt[1];
for(j=1; j<=n; j++)
if(max<=bt[j])
max=bt[j];
if((max%q)==0)
r=(max/q);
else
r=(max/q)+1;
for(i=1; i<=r; i++)
{
printf("\n\nRound %d",i);
for(j=1; j<=n; j++)
{
if(bt[j]>0)
{
bt[j]=bt[j]-q;
if(bt[j]<=0)
{
bt[j]=0;
printf("\njob %d is completed",j);
}
else
printf("\njob %d remaining time is %d",j,bt[j]);
}
}
}
for(i=1; i<=n; i++)
{
e=0;
for(j=1; j<=r; j++)
{
if(bt_c[i]!=0)
{
if(bt_c[i]>=q)
{
m[i+e]=q;
bt_c[i]-=q;
}
else
{
m[i+e]=bt_c[i];
bt_c[i]=0;
}
}
else
m[i+e]=0;
e=e+n;
}
}
for(i=2; i<=n; i++)
for(j=1; j<=i-1; j++)
avg=avg+m[j];
for(i=n+1; i<=r*n; i++)
{
if(m[i]!=0)
{
for(j=i-(n-1); j<=i-1; j++)
avg=m[j]+avg;
}
}
f=avg/n;
printf("\n\n\nTOTAL WATING TIME:%f\t",avg);
printf("\n\nAVERAGE WAITING TIME:%f\t\n",f);
return 0;
}
Download CodeOUTPUT :
|
C Program - CPU Scheduling Algorithm : Round Robin |