Round Robin Scheduling Program in C
Here you will learn Round Robin Scheduling Algorithm using gantt chart and the source code of Round Robin Scheduling Program in C language.
Round Robin Scheduling
Round Robin in operating system is a type of preemptive scheduling algorithm used in computer multitasking systems. It assigns CPU time in a cyclic way to each process in a queue. This ensures that every process is allocated a fair share of CPU time, and prevents the starvation of any process. It is particularly useful in time-sharing systems, where multiple users have access to the same computer.
Difference between FCFS and Round Robin
First Come First Served (FCFS) | Round Robin (RR) |
♦ First Come First Served (FCFS) scheduling algorithm is a scheduling algorithm where processes are scheduled according to their arrival time. | ♦ Round Robin scheduling algorithm is a scheduling algorithm where processes are scheduled in a round-robin fashion. |
♦ The process that arrives first will be scheduled first. | ♦ Each process is given a fixed time slice (quantum), and once a process has used up its quantum. It is preempted and moved to the bottom of the ready queue. |
♦ First Come First Served (FCFS) is non-preemptive scheduling algorithm. | ♦ Round Robin(RR) is the preemptive and added to the end of the ready queue. |
Working of Round Robin
Process P1 will be allocated for 20 time unit after then pre-empted to next process. This process will be continue till all process execution done.
Waiting time of process = (start time-arrival time)+(new time -old finish time)
Step 1. Waiting time P1 = (0-0)+(76-20)+(124-96) = 84.
Step 2. Waiting time P2 = (20-0) = 20.
Step 3. Waiting time P3 = (36-0)+(96-56)+(132-116) = 92.
Step 4. Waiting time P4 = (56-0)+(116-76) = 96.
Step 5. Average Waiting time = (292)/4 = 73.
Waiting Time :
Waiting Time = Turn around time – Burst Time.
Turn Around Time :
Turn Around Time = Completion Time – Arrival Time.
or
Turn around time = Burst time + Waiting time.
Example of Round Robin Scheduling Program in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | //Round Robin Scheduling Program in C #include<stdio.h> int main() { int n,i,j,temp1,temp2; printf("Input no. of processes: \n"); scanf("%d",&n); int bt[n],at[n],p[n],wt[n],tat[n],ct[n]; float avgwt=0,avgtat=0; printf("Input arrival time and burst time of process : \n"); for(i=0;i<n;i++) { printf("Process %d: \n",i+1); scanf("%d",&at[i]); scanf("%d",&bt[i]); p[i]=i+1; } // sort by arrival time for(i=0;i<n;i++) { for(j=0;j<n;j++) { if(at[i]<at[j]) { temp1=bt[j]; bt[j]=bt[i]; bt[i]=temp1; temp1=at[j]; at[j]=at[i]; at[i]=temp1; temp2=p[j]; p[j]=p[i]; p[i]=temp2; } } } //calculating completion time for(i=0;i<n;i++) { if(i==0) { ct[i]=at[i]+bt[i]; } else { if(at[i]>ct[i-1]) { ct[i]=at[i]+bt[i]; } else { ct[i]=ct[i-1]+bt[i]; } } tat[i]=ct[i]-at[i]; wt[i]=tat[i]-bt[i]; avgwt+=wt[i]; avgtat+=tat[i]; } avgwt=avgwt/n; avgtat=avgtat/n; printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n"); for(i=0;i<n;i++) { printf("%d\t%d\t%d\t%d\t%d\t%d\n",p[i],at[i],bt[i],ct[i],tat[i],wt[i]); } printf("Average Waiting Time: %f\n",avgwt); printf("Average Turn Around Time: %f\n",avgtat); return 0; } |
Read Also