# Round Robin Scheduling with different arrival times

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 are arrived in the quantum time that make these scheduling fairly
Note:

• Round robin is cyclic in nature so starvation doesn’t occur
• Round robin is 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 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.

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:-

1. Take the process which occurs first and start executing the process.(for quantum time only)
2. 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
3. After quantum time has passed, check for any processes in Ready queue. If ready queue is empty continue current process. If queue not empty and current process is not complete, then add current process to the end of the ready queue.
4. Take the first process from the Ready queue and start executing it (same rules)
5. Repeat all steps above from 2-5
6. If process is complete and the ready queue is empty then task is complete

After all these we get the three times which are:

1. Completion Time: the time taken for a process to complete.
2. Turn Around Time: total time the process exists in system.(completion time – arrival time).
3. Waiting Time: total time the waiting for there complete execution.(turn around time – burst time ).

How to implement in programming language

```1. Create two arrays of burst time res_b[] and of
arrival time res_a[] and copy the value of
the b[] and a[] array for calculate the
remaining time.(b[] is burst time, a[] arrival time).
2. Create an another array for wt[] to store waiting time.
3. Initialize Time : t=0;
4. Keep traversing the all process while all
process are not done.
Do following for i'th process if it is not done yet.
a- if res_a[i]<= q   (quantum time :- q)
1. if res_b[i]>q
a. t=t+q
b. res_b[i]-=q;
c. a[i]+=q;
2. else res_b[i]<=q(for last to execute)
a. t=t+b[i];
b. wt[i]=t-b[i]-a[i];
c.res_b[i]=0;
b- else res_a[i]<q
1. Initialize j=0 to number of process
if a[j]<a[i] (compare is there any
other process come before these process)
1. if res_b[j]>q
a. t=t+q
b. res_b[j]-=q;
c. a[j]+=q;
2. else res_b[j]<=q
a. t=t+b[j];
b. wt[j]=t-b[j]-a[j];
c.res_b[j]=0;
2. now we executing the i'th process
1. if res_b[i]>q
a. t=t+q
b. res_b[i]-=q;
c. a[i]+=q;
2. else res_b[i]<=q
a. t=t+b[i];
b. wt[i]=t-b[i]-a[i];
c.res_b[i]=0;

```

Below is the implementation of the above approach:

## Java

 `// Java program to implement Round Robin` `// Scheduling with different arrival time` `import` `java.util.*;` `//import java.util.concurrent.CompletableFuture;` `public` `class` `RoundRobin {` `    ``public` `static` `void` `findWaitingTime(``int` `process[],``int` `wt_time[],``int` `n ,``int` `brusttime[],``int` `quantum,``int` `completion_time[],``int` `arrival_time[]){` `         ``// copy the value of brusttime array into wt_time array.` `        ``int` `rem_time[] = ``new` `int``[n];` `        `  `        ``for``(``int` `i=``0``;i``0``){` `                     ``done =``false``;` `                    ``if``(rem_time[i]>quantum && arrival_time[i]<=arrival){` `                        ``t +=quantum;` `                        ``rem_time[i]-=quantum;` `                        ``arrival++;` `                    ``}` `                    ``else``{` `                    ``if``(arrival_time[i]<=arrival){` `                        ``arrival++;` `                        ``t+=rem_time[i];` `                        ``rem_time[i]=``0``;` `                        ``completion_time[i]=t; }` `                    ``}` `                 ``}` `              ``}` `            `  `            ``if``(done==``true``)    ` `            ``{ ` `                ``break``;` `                ``}` `        ``}    ` `        ``}` `    ``public` `static` `void` `findTurnAroundTime(``int` `process[] ,``int` `wt_time[],``int` `n,``int` `brusttime[],``int` `tat_time[],``int` `completion_time[],``int` `arrival_time[]){` `        ``for``(``int` `i=``0``;i

## C#

 `// C# program to implement Round Robin` `// Scheduling with different arrival time` `using` `System;` `    `  `class` `GFG ` `{` `    ``public` `static` `void` `roundRobin(String []p, ``int` `[]a,` `                                     ``int` `[]b, ``int` `n)` `    ``{` `        ``// result of average times` `        ``int` `res = 0;` `        ``int` `resc = 0;`   `        ``// for sequence storage` `        ``String seq = ``""``;`   `        ``// copy the burst array and arrival array` `        ``// for not effecting the actual array` `        ``int` `[]res_b = ``new` `int``[b.Length];` `        ``int` `[]res_a = ``new` `int``[a.Length];`   `        ``for` `(``int` `i = 0; i < res_b.Length; i++)` `        ``{` `            ``res_b[i] = b[i];` `            ``res_a[i] = a[i];` `        ``}`   `        ``// critical time of system` `        ``int` `t = 0;`   `        ``// for store the waiting time` `        ``int` `[]w = ``new` `int``[p.Length];`   `        ``// for store the Completion time` `        ``int` `[]comp = ``new` `int``[p.Length];`   `        ``while` `(``true``)` `        ``{` `            ``Boolean flag = ``true``;` `            ``for` `(``int` `i = 0; i < p.Length; i++) ` `            ``{`   `                ``// these condition for if` `                ``// arrival is not on zero`   `                ``// check that if there come before qtime` `                ``if` `(res_a[i] <= t)` `                ``{` `                    ``if` `(res_a[i] <= n) ` `                    ``{` `                        ``if` `(res_b[i] > 0)` `                        ``{` `                            ``flag = ``false``;` `                            ``if` `(res_b[i] > n) ` `                            ``{`   `                                ``// make decrease the b time` `                                ``t = t + n;` `                                ``res_b[i] = res_b[i] - n;` `                                ``res_a[i] = res_a[i] + n;` `                                ``seq += ``"->"` `+ p[i];` `                            ``}` `                            ``else` `                            ``{`   `                                ``// for last time` `                                ``t = t + res_b[i];`   `                                ``// store comp time` `                                ``comp[i] = t - a[i];`   `                                ``// store wait time` `                                ``w[i] = t - b[i] - a[i];` `                                ``res_b[i] = 0;`   `                                ``// add sequence` `                                ``seq += ``"->"` `+ p[i];` `                            ``}` `                        ``}` `                    ``}` `                    ``else` `if` `(res_a[i] > n) ` `                    ``{`   `                        ``// is any have less arrival time` `                        ``// the coming process then execute them` `                        ``for` `(``int` `j = 0; j < p.Length; j++) ` `                        ``{`   `                            ``// compare` `                            ``if` `(res_a[j] < res_a[i]) ` `                            ``{` `                                ``if` `(res_b[j] > 0) ` `                                ``{` `                                    ``flag = ``false``;` `                                    ``if` `(res_b[j] > n) ` `                                    ``{` `                                        ``t = t + n;` `                                        ``res_b[j] = res_b[j] - n;` `                                        ``res_a[j] = res_a[j] + n;` `                                        ``seq += ``"->"` `+ p[j];` `                                    ``}` `                                    ``else` `                                    ``{` `                                        ``t = t + res_b[j];` `                                        ``comp[j] = t - a[j];` `                                        ``w[j] = t - b[j] - a[j];` `                                        ``res_b[j] = 0;` `                                        ``seq += ``"->"` `+ p[j];` `                                    ``}` `                                ``}` `                            ``}` `                        ``}`   `                        ``// now the previous porcess according to` `                        ``// ith is process` `                        ``if` `(res_b[i] > 0) ` `                        ``{` `                            ``flag = ``false``;`   `                            ``// Check for greaters` `                            ``if` `(res_b[i] > n) ` `                            ``{` `                                ``t = t + n;` `                                ``res_b[i] = res_b[i] - n;` `                                ``res_a[i] = res_a[i] + n;` `                                ``seq += ``"->"` `+ p[i];` `                            ``}` `                            ``else` `                            ``{` `                                ``t = t + res_b[i];` `                                ``comp[i] = t - a[i];` `                                ``w[i] = t - b[i] - a[i];` `                                ``res_b[i] = 0;` `                                ``seq += ``"->"` `+ p[i];` `                            ``}` `                        ``}` `                    ``}` `                ``}`   `                ``// if no process is come on thse critical` `                ``else` `if` `(res_a[i] > t) ` `                ``{` `                    ``t++;` `                    ``i--;` `                ``}` `            ``}` `            `  `            ``// for exit the while loop` `            ``if` `(flag) ` `            ``{` `                ``break``;` `            ``}` `        ``}`   `        ``Console.WriteLine(``"name   ctime   wtime"``);` `        ``for` `(``int` `i = 0; i < p.Length; i++) ` `        ``{` `            ``Console.WriteLine(``" "` `+ p[i] + ``"\t"` `+ ` `                                 ``comp[i] + ``"\t"` `+ w[i]);`   `            ``res = res + w[i];` `            ``resc = resc + comp[i];` `        ``}`   `        ``Console.WriteLine(``"Average waiting time is "` `+ ` `                               ``(``float``)res / p.Length);` `        ``Console.WriteLine(``"Average compilation time is "` `+ ` `                                  ``(``float``)resc / p.Length);` `        ``Console.WriteLine(``"Sequence is like that "` `+ seq);` `    ``}`   `    ``// Driver Code` `    ``public` `static` `void` `Main(String []args)` `    ``{` `        ``// name of the process` `        ``String []name = { ``"p1"``, ``"p2"``, ``"p3"``, ``"p4"` `};`   `        ``// arrival for every process` `        ``int` `[]arrivaltime = { 0, 1, 2, 3 };`   `        ``// burst time for every process` `        ``int` `[]bursttime = { 10, 4, 5, 3 };`   `        ``// quantum time of each process` `        ``int` `q = 3;`   `        ``// cal the function for output` `        ``roundRobin(name, arrivaltime, bursttime, q);` `    ``}` `}`   `// This code is contributed by Rajput-Ji`

Output:

```Processes  Arrival Time      Burst time  completion time Turn Around Time  Waiting time
1        0        5     12        12         7
2        1        4     11        10         6
3        2        2     6        4         2
4        3        1     7        4         3
Average waiting time = 4.5
Average turn around time = 7.5

```

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.