This content originally appeared on Level Up Coding - Medium and was authored by shivam bhatele

What is Round-Robin Scheduling, and how does it work?
The round-robin concept is a method of assigning a task to the CPU. In this algorithm, each individual gets an equal amount of something, in turn, inspired by the name of this method. It is the simplest and oldest scheduling method, and it is mostly used for multitasking.
Round-robin scheduling allows each ready job to run in a cyclic queue for a certain amount of time. This technique also allows for process execution without hunger.
In this article, the following topics are covered:
- Introduction to Round-Robin Algorithm
- Pros and Cons of Round-Robin Algorithm
- Computation of the algorithm
- Java and C++ Code implementation
- Conclusion
Introduction to Round-Robin Algorithm
- It is a preemptive algorithm — This means that a process can be forced (preempted) from the CPU by the operating system anytime, either to free up the CPU for other, higher-priority tasks or because the time slice has come to an end. The preempted/forced process gets moved to the end of the queue.
- It is developed for use in time-sharing systems.
- The maximum time period allocated to the process is termed ‘Quantum’.
- Each task in the ready queue is allocated a one-time slice at a time by the CPU scheduler.
- This algorithm uses the first-in, first-out (FIFO) queuing system.
- The present CPU burst may affect processes assigned to the CPU:-
a) the same as the time slice — The procedure will release the CPU on its own
b) less than the interval of time — The procedure will release the CPU on its own
c) more than the interval of time — The existing process is preempted
7. A minimum time slice should be set to a specific job that has to be processed. It may, however, differ from one OS to the next. After a defined interval of time, the CPU is transferred to the next process, which is known as time quantum/time slice.
8. The round-robin model is a clock-driven hybrid model.
9. It’s a real-time algorithm that reacts to an event within a set amount of time.
Pros and Cons of Round-Robin Algorithm
ADVANTAGES
- Simple and straightforward to implement.
- Each process is given an equal chance to run.
- Managing all processes in a non-prioritized manner.
- There will be no starvation.
- In this scheduling, each process gets a chance to reschedule after a certain quantum time.
- Each work is given an equal amount of CPU time.
- It handles all processes without regard to priority.
- You can estimate the worst-case response time for the same process if you know the total number of processes in the run queue.
- This type of scheduling is independent of burst time. As a result, it is simple to integrate into the system.
- In terms of average response time, it performs the best.
DISADVANTAGES
- Depending on how long the time-slice is.
- If the time slice is infinitely big, it’s the same as FCFS.
- Due to frequent context changes, a little temporal slice will degenerate.
- If quantum time is less for scheduling, the Gantt chart appears to be overly large. For example, for large scheduling, 1 ms is a good starting point.)
- Scheduling a little quantum(Time period) takes a long time.
- Processes cannot have priorities defined for them.
- Round-robin scheduling does not give more priority to special tasks.
- Sometimes, finding the right time quantum is a challenging task.
Computation of the algorithm

Now that you have been familiarised with the Round-Round algorithm, it’s time to understand how exactly it works.
Let us learn about some terms that would be coming across -
- Completion Time — Time it takes for a procedure to finish it’s execution.
- Turn-around time — The time difference between completion and arrival is known as turn-around time.
Turnaround Time = Completion Time — Arrival Time
3. Waiting Time (W.T) — the difference between the turn-around time and bursting time.
Waiting Time = Turnaround Time — Burst Time
Consider the following example:
In this example, 3 processes are given along with their burst times. Also, it is mentioned that the duration of the time slice is 4 milliseconds, which means the maximum amount of time that a process runs, is 4 ms. Hence, for every process that requires more than 4 ms, we will first run it for 4 ms and then for the next 4 ms and so on and so forth until its burst time gets over.
The table shown above is called the ‘Gantt Chart’. Let’s see how to make one.
- The arrival time for each process is not given, so consider it as 0 ms. So, the completion time and turnaround time are the same.
- Starting with process 1, its burst time is 24 ms. But as we know, we can only run the process for at most 4 ms. So the P1 process will go for 0 to 4 ms.
- Next is the turn for P2, which only needs 3 ms. So P2 will run for 4 to 7 ms. Next P3 will run for 7 to 10 ms since its burst time is also 3 ms.
- Now every process has been completed once. So again start from P1 and run it for the next 4 ms, i.e from 10 to 14. Now move to the next process.
- The burst times of P2 and P3 have been completed in the first round. So we will continue running the P1 for rounds of at most 4 ms until its burst time reaches to 24 ms.
Now, let’s find the waiting time of each process.
P1 started at 0 ms so it didn’t wait in the beginning. But it had to wait for the time process P2 and P3 were running, i.e from 4 ms to 10 ms. So its waiting time = 10–4 = 6 ms.
P2 waited for 4 ms in the beginning when P1 was running. So its waiting time = 4 ms.
P3 waited for 4 ms for P1 and 3 ms for P2. So its waiting time is 4+3=7 ms.
Java and C++ Code implementation
//C++ program
//Java program
You can run the program on Interviewbit
Conclusion
Round robin is one of the oldest, fairest, and most extensively utilized scheduling algorithms in conventional operating systems. The most significant benefit of the round-robin scheduling algorithm is that all the jobs get an unbiased allocation of CPU, and it deals with all processes without any priority. This technique takes more time to transition between contexts.
Introduction to Round Robin Scheduling Algorithm (C++ and Java Code) was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by shivam bhatele

shivam bhatele | Sciencx (2022-04-29T00:39:27+00:00) Introduction to Round Robin Scheduling Algorithm (C++ and Java Code). Retrieved from https://www.scien.cx/2022/04/29/introduction-to-round-robin-scheduling-algorithm-c-and-java-code/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.