Sunday, February 6, 2011

CPU scheduling : Round Robin


OBJECTIVE: Write a program in c to implement Round Robin scheduling.
THEORY: 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.
Example: The time slot could be 100 milliseconds. If job1 takes a total time of 250ms to complete, the round-robin scheduler will suspend the job after 100ms and give other jobs their time on the CPU. Once the other jobs have had their equal share (100ms each), job1 will get another allocation of CPU time and the cycle will repeat. This process continues until the job finishes and needs no more time on the CPU.

PROGRAM:
#include<stdio.h> 
#include<conio.h> 
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; 
clrscr(); 
printf("Enter number of processes:"); 
scanf("%d",&n); 
printf("Enter burst time for sequences:"); 
for(i=0;i<n;i++) 
{ 
scanf("%d",&bt[i]); 
st[i]=bt[i]; 
} 
printf("Enter time quantum:"); 
scanf("%d",&tq); 
while(1) 
{ 
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; 
} 
if(n==count) 
break; 
} 
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 Burst time Wait time Turn around time 
"); 
for(i=0;i<n;i++) 
printf("%d %d %d %d 
",i+1,bt[i],wt[i],tat[i]); 
printf("Avg wait time is %f 
Avg turn around time is %f",awt,atat); 
getch(); 
}

5 comments: