In this tutorial you will learn about round robin scheduling program in C.
Process scheduling is an important component for process management. In a multi-user and a time-sharing system, response time is one of the most important objective to be accomplished.
There are many scheduling algorithms in C for process management such as:
1. First Come First Serve
2. Shortest Job First
3. Priority Scheduling
4. Round Robin Scheduling
However, this tutorial will get you clear with understanding of Round Robin Scheduling program in C.
Round Robin Scheduling Algorithm
1. The queue structure in ready queue is of First In First Out (FIFO) type.
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.
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.
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.
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.
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.
Example
Lets take one example to understand it.
Time Quantum = 2
Process | Burst Time | Execution Time |
P1 | 0 | 9 |
P2 | 1 | 5 |
P3 | 2 | 3 |
P4 | 3 | 4 |
Process | Arrival Time | Burst Time (x) | Turnaround Time(t) | Normalized Turnaround Time(t/x) | Waiting Time |
P1 | 0 | 9 | 21 | 2.34 | 12 |
P2 | 1 | 5 | 17 | 3.4 | 12 |
P3 | 2 | 3 | 11 | 3.67 | 8 |
P4 | 3 | 4 | 12 | 3 | 8 |
Average Turnaround Time = 15.25
Average Normalized Turnaround Time = 3.10
Average Waiting Time = 10
Round Robin Scheduling Program in C
#include<stdio.h> int main() { int count,j,n,time,remain,flag=0,time_quantum; int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10]; printf("Enter Total Process:\t "); scanf("%d",&n); remain=n; for(count=0;count<n;count++) { printf("Enter Arrival Time and Burst Time for Process Process Number %d :",count+1); scanf("%d",&at[count]); scanf("%d",&bt[count]); rt[count]=bt[count]; } printf("Enter Time Quantum:\t"); scanf("%d",&time_quantum); printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n"); for(time=0,count=0;remain!=0;) { if(rt[count]<=time_quantum && rt[count]>0) { time+=rt[count]; rt[count]=0; flag=1; } else if(rt[count]>0) { rt[count]-=time_quantum; time+=time_quantum; } if(rt[count]==0 && flag==1) { remain--; printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-at[count],time-at[count]-bt[count]); wait_time+=time-at[count]-bt[count]; turnaround_time+=time-at[count]; flag=0; } if(count==n-1) count=0; else if(at[count+1]<=time) count++; else count=0; } printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n); printf("Avg Turnaround Time = %f",turnaround_time*1.0/n); return 0; }
Output
If you found anything incorrect or have any doubts regarding above round robin scheduling program in C then comment below.
The post Round Robin Scheduling Program in C appeared first on The Crazy Programmer.