## Saturday, December 3, 2011

### Round Robin Process Sheduling Algorithm and Example Source Code in C

Round robin is the most widely used process scheduling algorithm .The basic strategy for round robin scheduling is that if there are n process,each of the process will receive 1/n CPU Execution Time.Each process is allotted a  time quanta, for which its is executed.The incoming processes are kept in a ready list while another one is executing.If the time quanta allotted for a process is over,then that process is moved to ready and the next process in the ready list is executed for the allotted time quanta.The Complete Example Implementation Source Code in C of Round Robin Algorithm to schedule N Processes and to calculate the Execution,wait time and turn around time is given below.
`````` #include<stdio.h>
void main()
{
int n,i,k,x=0,s=0,r=0,q=0,x=0,a[30],e[30],t[30];
float m,p=0;
printf("Enter the number of process:");
scanf("%d",&n);
printf("Enter the execution time:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
e[i]=a[i];
}
printf("Enter the quanta:");
scanf("%d",&q);
printf("After round robin sheduling:");
for(i=0;i<n;i++)
{
if(x<a[i])
{
x=a[i];
}
}
k=x/q;
while(s<=k)
{
for(i=0;i<n;i++)
{
if(a[i]>0)
{
if(a[i]>q)
{
r=r+q;
a[i]=a[i]-q;
printf("P%d\t",i+1);
}
else
{
r=r+a[i];
a[i]=a[i]-q;
printf("P%d ",i+1);
t[i]=r;
}
}
}
s++;
}
printf("\nPROCESS EXECUTION TIME  WAIT TIME  TURN AROUND TIME\n");
for(i=0;i<n;i++)
{
printf(" %d \t\t %d\t\t %d\t\t %d\t\t \n",i,e[i],x,t[i]);
x=x+q;
}
m=x/n;
printf("Average waiting time=%f",m);
printf("Average turn around time=");
for(i=0;i<n;i++)
p=p+t[i];
p=p/n;
printf("%f",p);
}
``````

• Namratha Reddy says:
March 21, 2012 at 7:06 PM

we want this code in java

• Techman says:
March 23, 2012 at 8:04 PM

Why don't you guys Join the Blog.Code Requests are fulfilled only for members.

• kanimozhi elango says:
February 10, 2013 at 4:55 PM

we need the output for this program

• bushra gesm says:
November 14, 2013 at 9:42 AM

this program have compiler error x variable is defined twice !

• Falesh shelke says:
February 5, 2014 at 1:56 AM

# include
# include
# include
void main(){
clrscr();
int slice,i,j,m,wt[10],st[10],et[10],n,tat[10],pt[10],a[10],twt=0,ttat=0;
float avgwt,avgtat;
char p[10][10];
cout<<"enter no of process";
cin>>n;
for(i=0;i> p[i];
cout<<"\n Enter the burst time:";
cin>>pt[i];
}
cout<<"Enter the time-slice:";
cin>>slice;
st[0]=0;
i=0;
do{
if(pt[i]<=slice)
{et[i]=st[i]+pt[i];
pt[i]=0;
tat[i]=et[i];}
else
{et[i]=st[i]+slice;
pt[i]=pt[i]-slice;}
wt[i]=st[i];
st[i+1]=et[i];
i=i+1;
}while(i<n);
m=n-1;
j=0;
a[m]=1234;
do{ for(i=0;i<n;i++){
if(pt[i]==0)
{tat[i]=tat[i];}
else
{st[m+1]=et[m];
a[m+1]=i;
if(pt[i]<=slice)
{et[m+1]=st[m+1]+pt[i];
pt[i]=0;
tat[i]=et[m+1];}
else
{et[m+1]=st[m+1]+slice;
pt[i]=pt[i]-slice;}
if(a[m]==a[m+1])
{wt[i]=wt[i]+0;}
else
{wt[i]+=st[m+1]-et[i];}
m=m+1;} }
if(pt[j]!=0)continue;
j=j+1;
}while(j<n);
cout<<"\n name"<<"\t waiting time"<<"\t turn aroun time";
for(i=0;i<n;i++)
{cout<<"\n"<<p[i]<<"\t\t"<<wt[i]<<"\t\t\t"<<tat[i]<<"\n";
twt+=wt[i];
ttat+=tat[i];
}
avgwt=(float) (twt)/n;
avgtat= (float)(ttat)/n;
cout<<"\n Average waiting time:"<<avgwt;
cout<<"\n Average turn around time:"<<avgtat;
getch();
}

• haritha heri says:
August 6, 2014 at 5:55 AM

i want reply as soon as posible

• haritha heri says:
August 6, 2014 at 5:57 AM

#include
#include=ts)
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=ts;
}
need[i]-=ts;
if(need[i]==0)
{
flag[i]=0;
n--;
}
}
else
{
for(j=1;j<=n;j++)
{
if((i!=j)&&(flag[i]==1)&&(need[j]!=0))
wt[j]+=need[i];
}
need[i]=0;
n--;
flag[i]=0;
}
}
}
for(i=1;i<=n1;i++)
{
tat[i]=wt[i]+bt[i];
twt=twt+wt[i];
ttat=ttat+tat[i];
}
awt=(float)twt/n1;
atat=(float)ttat/n1;
printf("\n\n ROUND ROBIN SCHEDULING ALGORITHM \n\n");
printf("\n\n Process \t Process ID \t BurstTime \t Waiting Time \t TurnaroundTime \n ");
for(i=1;i<=n1;i++)
{
printf("\n %5d \t %5d \t\t %5d \t\t %5d \t\t %5d \n", i,pid[i],bt[i],wt[i],tat[i]);
}
printf("\n The average Waiting Time=4.2f",awt);
printf("\n The average Turn around Time=4.2f",atat);
getch();
}

• haritha heri says:
August 6, 2014 at 5:58 AM

is the above code correct

• Nur Haque says:
September 8, 2015 at 8:47 AM

Thanks for nice post.know details

• Anna says:
December 31, 2015 at 3:14 AM