# Round Robin Scheduling in Operating System

FCFS scheduling but preemption is added to switch between processes so this round-robin scheduling algorithm in one way is similar to the first come first serve scheduling but preemption is added to switch between the processes and we will be seeing how it is done as we move ahead so here a small unit of time called a time quantum or a time slice is defined which is generally from ten 200 milliseconds so in this round-robin scheduling what happens is that we define a small unit of time which is usually called the time quantum or a time slice and this time quantum or time slice is generally defined from 10 200 milliseconds.
So it will for between 10 and a hundred milliseconds and what happens is that each process will be assigned this time quantum for its execution so whether the CPU burst of the process is greater than the time quantum or not does matter but this specific time quantum is assigned to each process and a process will be allowed to execute only for that particular time quantum in the first round and after that, the CPU will be given to the next process that is waiting in the queue and that process also will be allowed to execute for that specific time quantum and then the CPU will be given to the next process and so on so what happens is the ready queue is treated as a circular queue.
CPU scheduler goes around the ready queue allocating the CPU to each process for a time interval of up to time quantum so that is what I just explained so here what will happen is the ready queue where the processes are waiting is treated as a circular queue so I have shown this using this diagram over here so think of this as a circular queue so this is the ready queue and the processes are waiting in this circular queue waiting for its turn to get the CPU for its execution so here we are having ten processes P 1 P 2 P 3 up to P 10 so they are waiting in this queue and when a new process comes they will be added to the end of the queue or at the tail of the queue so this is process P 10 which is the last process so if a new process arrives it will be added after this P 10 at the tail of the queue.
So you may not be able to visualize the tail of the queue because it is circular over here but actually it is not circular in shape but just to make you understand I have taken this diagram it is actually just a normal queue where the CPU scheduler will move from one end of the queue to the other end and then come back to the start and again move from the start to the end and it goes on like that so that is why we have taken today using a circular queue so we have processes P 1 2 P 10 waiting in this circular a ready queue waiting for getting the CPU for their execution so what happens is there is a CPU scheduler that will assign the CPU to this particular processes so first of all the CPU scheduler will come and assign the CPU to the first process that is there it is p1 so in this way it is going to start like an FCFS scheduling first-come-first-serve so whoever is first will be first given the CPU and for how long will it be given the CPU.
It will be given the CPU for a particular time quantum so there will be a particular quantum of time that will be defined and only for that amount or that period of time the CPU will be assigned to a particular process so let’s say that the time quantum is 5 milliseconds so what will happen is the CPU will be given to p1 for 5 milliseconds so p1 gets a CPU and uses it for 5 milliseconds that is one time quantum so after that the cpu scheduler will come and give the cpu to p2 which is a next process that is waiting in the circular queue so p2 will get the CPU and will also be allowed to execute for five milliseconds which is a particular quantum of time and after that p3 will be given the CPU and will be allowed to execute for five milliseconds and so on so it will go around like this in the circular queue and when it reaches the end of the queue what happened is that it will again come and check if p1 has completed its execution or not so if p1 has completed its execution then p1 does not have to get the CPU again but if p1 did not complete its execution in the first quantum of time that it has received then the cpu will again be given to p1 and it will again be allowed to execute for one quantum of time which is 5 milliseconds.