Prerequisite: Round Robin Scheduling with arrival time as 0
Round-robin scheduling algorithm is used to schedule process fairly each job a time slot or quantum and the interrupting the job if it is not completed by then the job come after the other job which is arrived in the quantum time that makes these scheduling fairly.
- Round-robin is cyclic in nature, so starvation doesn’t occur
- Round-robin is a variant of first come, first served scheduling
- No priority, special importance given to any process or task
- RR scheduling is also known as Time slicing scheduling
- Each process is served by CPU for a fixed time, so priority is the same for each one
- Starvation does not occur because of its cyclic nature.
- Throughput depends on quantum time.
- If we want to give some process priority, we cannot.
|Process||Arrival Time||Burst Time||Completion time||Turn Around Time||Waiting time|
Quantum time is 2 this means each process is only executing for 2 units of time at a time.
How to compute these process requests:-
- Take the process which occurs first and start executing the process(for quantum time only).
- Check if any other process request has arrived. If a process request arrives during the quantum time in which another process is executing, then add the new process to the Ready queue
- After the quantum time has passed, check for any processes in the Ready queue. If the ready queue is empty then continue the current process. If the queue not empty and the current process is not complete, then add the current process to the end of the ready queue.
- Take the first process from the Ready queue and start executing it (same rules)
- Repeat all steps above from 2-5
- If the process is complete and the ready queue is empty then the task is complete
After all these we get the three times which are:
- Completion Time: the time taken for a process to complete.
- Turn Around Time: total time the process exists in the system. (completion time – arrival time).
- Waiting Time: total time waiting for their complete execution. (turn around time – burst time ).
How to implement in a programming language
1. Declare arrival, burst, wait, turn arrays and initialize them. Also declare a timer variable and initialize it to zero. To sustain the original burst array create another array (temp_burst) and copy all the values of burst array in it. 2. To keep a check we create another array of bool type which keeps the record of whether a process is completed or not. we also need to maintain a queue array which contains the process indicies (initially the array is filled with 0). 3. Now we increment the timer variable until the first process arrives and when it does, we add the process index to the queue array 4. Now we execute the first process until the time quanta and during that time quanta, we check whether any other process has arrived or not and if it has then we add the index in the queue (by calling the fxn. queueUpdation()). 5. Now, after doing the above steps if a process has finished, we store its exit time and execute the next process in the queue array. Else, we move the currently executed process at the end of the queue (by calling another fxn. queueMaintainence()) when the time slice expires. 6. The above steps are then repeated until all the processes have been completely executed. If a scenario arises where there are some processes left but they have not arrived yet, then we shall wait and the CPU will remain idle during this interval.
Below is the implementation of the above approach:
(For the sake of simplicity, we assume that the arrival times are entered in a sorted way)
Enter the time quanta : 2 Enter the number of processess : 4 Enter the arrival time of the processess : 0 1 2 3 Enter the burst time of the processess : 5 4 2 1 Program No. Arrival Time Burst Time Wait Time TurnAround Time 1 0 5 7 12 2 1 4 6 10 3 2 2 2 4 4 3 1 5 6 Average wait time : 5 Average Turn Around Time : 8
In case of any queries or a problem with the code, please write it in the comment section.
Note: A slightly optimized version of the above-implemented code could be by using Queue data structure.