Tuesday, September 27, 2011

Round Robin Scheduling: Round Robin Scheduling

Round Robin Scheduling: Round Robin Scheduling: Round-robin job scheduling may not be desirable if the sizes of the jobs or tasks are highly variable. A process that produces large jobs w...

Round Robin Scheduling


Round-robin job scheduling may not be desirable if the sizes of the jobs or tasks are highly variable. A process that produces large jobs would be favoured over other processes. This problem may be solved by time-sharing, i.e. by giving each job a time slot or quantum(its allowance of CPU time), and interrupt the job if it is not completed by then. The job is resumed next time a time slot is assigned to that process.

Sample Implementation source code (C)
#include<stdio.h>
struct process
{
int pid,etime,ftime,hasfin,hasstr,wtime;
}pr[20];
int main()
{
int i,j,timeslice,temp,count=0,tclock=0,limit;
printf("Enter the time slice  :  ");
scanf("%d",&timeslice);
printf("Enter the number of process : ");
scanf("%d",&limit);
for(i=0;i<limit;i++)
{
printf("Enter the execution time of P%d  :  ",i);
scanf("%d",&pr[i].etime);
pr[i].hasfin=0;
pr[i].hasstr=0;
pr[i].pid=i+1;
}
printf("Process number     Time Elapsed  ");
i=0;
do
{
temp=timeslice;
do
{
count=0;
if(pr[i].hasstr == 0) 
{
pr[i].hasstr=1;
pr[i].wtime=tclock;
}
if(pr[i].hasfin == 0 )
{
tclock++;
temp--;
pr[i].etime-=1;
if(pr[i].hasfin == 0 && pr[i].etime == 0)
pr[i].ftime=tclock;
}
if(pr[i].etime == 0) 
{
pr[i].hasfin =1;
break;
}

}while(temp !=0);
for(j=0;j<limit;j++)
{
if(pr[j].hasfin == 0)
printf("\n P%d\t\t\t%d",pr[j].pid,pr[j].etime);
}
i++;
while(pr[i].etime == 0 && i <= limit)
i=i+1;
if(i >= limit)
{
i=0;
while(pr[i].etime == 0 && i <= limit)
i=i+1;
}
for(j=0;j<limit;j++)
{
if(pr[j].etime ==0 )
count=count+1;
}
}while(count < limit);
printf("\n\nProcess Id \tWait Time \tTurn Around Time ");
for(j=0;j<limit;j++)
printf("\nP%d\t\t       %d \t\t  %d",pr[j].pid,pr[j].wtime,pr[j].ftime);
return 0;
}