This article contains the implementation of the Round Robin Scheduling in C Programming with the explanation, an example, it’s advantages and disadvantages.
If we consider large management or organization with multi-users and with a time-sharing system, in such cases response time plays a vital role in the achievements of various objectives. So, Process Scheduling is a key component for such management.
Let us understand the Round Robin Scheduling program in C
What is Round Robin Scheduling?
The Round robin algorithm is a pre-emptive process scheduling algorithm used by the machine for scheduling the CPU utilization. Here, each process is allotted to a fixed time called time slice or time quantum in a cyclic way. A process enables the job scheduler that saves the current progress of the job moves to the next job present in the queue. Each process must complete the job within the given time-quantum else an interrupt may occur. The context-switching saves the current state of the process.
What is Round Robin Scheduling Algorithm?
- Here, every job is arranged in a FIFO(First Come First Server) order.
- When the first process arrives, it is sent for execution. But if it couldn’t complete the execution in a given amount of time quantum that is allotted to it, then an automated timer generates an interrupt.
- After that, the process is stopped and then send back to the end of the queue. Although the state is saved and stored so that it can be resumed where it was interrupted.
- The process is resumed and the schedulers select the next process from the queue sent it to the processor for execution.
- Each process is executed in a similar way and checked for the time quantum.
- The steps are repeated for every process until the processes are over.
Compute Times involved in Round Robin Scheduling:
- Completion Time: Time at which process completes its execution.
- Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time - Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Advantages:
- It is simple, easy to use and the overhead in decision-making is low.
- Due to the fair share of CPU, Starvation issues or convoy effect do not occur.
- Due to the queue system, equal priority is given to all the jobs which cannot be seen in other algorithms.
- Each process can be rescheduled after a given fixed time slot or quantum period.
Disadvantages:
- Due to its dependency on quantum value, the efficiency of the system is decreased.
- An increase in the quantum value might make the system unresponsive.
- There is no special priority for the most important process, every process is scheduled.
Example: Round Robin Scheduling in C Programming
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 | #include <stdio.h> int main() { int i, total = 0, x, limit, counter = 0, t_quantum; int wait_time = 0, turnaround_time = 0, arrival_time[10], burst_time[10], temp[10]; float average_wait_time, average_turnaround_time; printf("\nEnter Total Number of Processes: "); scanf("%d", &limit); x = limit; for (i = 0; i < limit; i++) { printf("\nProvide the details for Process[%d]\n", i + 1); printf("Arrival Time:\t"); scanf("%d", &arrival_time[i]); printf("Burst Time:\t"); scanf("%d", &burst_time[i]); temp[i] = burst_time[i]; } printf("\nEnter Time Quantum:\t"); scanf("%d", &t_quantum); printf("\nProcess ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n"); for (total = 0, i = 0; x != 0;) { if (temp[i] <= t_quantum && temp[i] > 0) { total = total + temp[i]; temp[i] = 0; counter = 1; } else if (temp[i] > 0) { temp[i] = temp[i] - t_quantum; total = total + t_quantum; } if (temp[i] == 0 && counter == 1) { x--; printf("\nProcess[%d]\t\t%d\t\t %d\t\t\t %d", i + 1, burst_time[i], total - arrival_time[i], total - arrival_time[i] - burst_time[i]); wait_time = wait_time + total - arrival_time[i] - burst_time[i]; turnaround_time = turnaround_time + total - arrival_time[i]; counter = 0; } if (i == limit - 1) { i = 0; } else if (arrival_time[i + 1] <= total) { i++; } else { i = 0; } } average_wait_time = wait_time *1.0 / limit; average_turnaround_time = turnaround_time *1.0 / limit; printf("\n\nAverage Waiting Time:\t%f", average_wait_time); printf("\nAvg Turnaround Time:\t%f\n", average_turnaround_time); return 0; } |
Output of above Round Robin Algorithm in C: