Write a Program Using C to Implement Round Robin Scheduling

Aim:

To Write a Program Using C to Implement Round Robin Scheduling.

Algorithm:

  • STEP-1: The queue structure in ready queue is of First In First Out (FIFO) type.
  • STEP-2: A fixed time is allotted to every process that arrives in the queue. This fixed time is known as time slice or time quantum.
  • STEP-3: The first process that arrives is selected and sent to the processor for execution. If it is not able to complete its execution within the time quantum provided, then an interrupt is generated using an automated timer.
  • STEP-4: The process is then stopped and is sent back at the end of the queue. However, the state is saved and context is thereby stored in memory. This helps the process to resume from the point where it was interrupted.
  • STEP-5: The scheduler selects another process from the ready queue and dispatches it to the processor for its execution. It is executed until the time Quantum does not exceed.
  • STEP-6: The same steps are repeated until all the process are finished.
  • The round-robin algorithm is simple and the overhead in decision making is very low.
  • It is the best scheduling algorithm for achieving better and evenly distributed response time.

Description:

  • Suppose the ready list contains the processes as shown in table and time quantum is 4 with a negligible amount of time for context switching.

i            T(Pi)

0            24

1             3

2             3

Gantt Chart:

P0P1P2 P0P0P0P0P0

0          4           7           10         14         18         22         26          30

Turn Around Time:

TRND(P0)=30

TRND(P1)=7

TRND(P2)=10

Average Turn Around Time:

TRND =(30+7+10)/3=15.66

Waiting Time:

W(P0)=26-(3+3+4+4+4+4) = 26-20 =6

W(P1)=4

W(P2)=7

Average Waiting Time:

W=(6+4+7)/3=5.66667

Program:

#include<stdio.h>  
int main()  
#define max 10
{  
int i,at[max],bt[max],temp[max],NOP,sum=0,count=0,y,quant,wt=0,tat=0;  
float avg_wt, avg_tat;  
printf(" Enter The Total number of Process: ");  
scanf("%d", &NOP);  
y = NOP;  
for(i=0; i<NOP; i++)  
{  
printf("\n Enter the Arrival & Burst time of P[%d]: ", i+1);  
scanf("%d%d", &at[i],&bt[i]);  
temp[i] = bt[i]; 
}  
printf("\n Enter the Time Quantum for the process: \t");  
scanf("%d", &quant);  
printf("\n Process BT \tTAT \t WT ");  
for(sum=0, i = 0; y!=0; )  
{  
if(temp[i] <= quant && temp[i] > 0)
{  
sum = sum + temp[i];  
temp[i] = 0;  
count=1;  
}
else if(temp[i] > 0)  
{  
temp[i] = temp[i] - quant;  
sum = sum + quant;    
}  
if(temp[i]==0 && count==1)  
{  
y--; 
printf("\n P[%d] \t %d \t %d \t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);  
wt = wt+sum-at[i]-bt[i];  
tat = tat+sum-at[i];  
count =0;     
}  
if(i==NOP-1)  
{  
i=0;  
}  
else if(at[i+1]<=sum)  
{  
i++;  
}  
else  
{  
i=0;  
}  }
avg_wt = wt * 1.0/NOP;  
avg_tat = tat * 1.0/NOP;  
printf("\n Average WT : %.2f", avg_tat);  
printf("\n Average TAT: %.2f", avg_wt);  
}

Execution:

Input
3 0 4 1 6 2 9 3

Output
 Enter The Total number of Process: 
 Enter the Arrival & Burst time of P[1]: 
 Enter the Arrival & Burst time of P[2]: 
 Enter the Arrival & Burst time of P[3]: 
 Enter the Time Quantum for the process: 	
 Process BT 	TAT 	 WT 
 P[1] 	 4 	 10 	 6
 P[2] 	 6 	 12 	 6
 P[3] 	 9 	 17 	 8
 Average WT : 13.00
 Average TAT: 6.67

Result:

Thus round robin scheduling program was executed Successfully.

Leave a Comment