elias rizik
elias rizik

Reputation: 263

how to implement round robin algorithm?

I watch a lot tutorials on youtube about RR scheduling and i had this question in exam but i don't know what i did wrong,the professor solution was different from my solution and i'm now confused.

Job       |    Arrival       |      Burst
P1        |       0          |        4
P2        |       2          |        5
P3        |       3          |        3
P4        |       8          |        4

QT = 1 

Her answer was : P1,P1,P2,P3,P1,P2,P3,P1,P4,P2,P3,P4,P2,P4,P2,P4

My answer was : P1,P1,P2,P1,P3,P2,P1,P3,P2,P4,P3,P2,P4,P2,P4,P4

So which one is the correct answer and if it was her then why ?

Upvotes: 3

Views: 1703

Answers (4)

Saurav Sahu
Saurav Sahu

Reputation: 13924

Highest priority goes to the new arrival, and the second priority is considered through FIFO in the waiting queue.

Queue 
front    P1     P2     P3      P4
-----------------------------------
0  P1 | [3]    
1  P1 | [2] >>
2  P2 |  2     [4] >>             <- P2 arrives
3  P3 |  2      4     [2]         <- P3 arrives
4  P1 | [1] >>  4      2 
5  P2 |  1     [3] >>  2 
6  P3 |  1      3     [1]
7  P1 | [0] >>  3      1  
8  P4 |         3      1     [3]  <- P4 arrives. FIFO disrupted.
9  P2 |        [2] >>  1      3   <- FIFO regained.
10 P3 |         2     [0] >>  3 
11 P4 |         2            [2]
12 P2 |        [1] >>         2 
13 P4 |         1            [1]
14 P2 |        [0] >>         1 
15 P4 |                      [0]

Upvotes: 3

Tony Tannous
Tony Tannous

Reputation: 14866

Your teacher has a different approach. Whenever a new process arrives in the system, it has a higher priority than processes that finished their burst.

Upvotes: 1

Shasha99
Shasha99

Reputation: 1916

Both are correct !!!

I will say that your solution looks correct( but you are not considering a priority scheduling queue). But it depends how your professor is approaching. Following are the approaches:

Your Approach:

You are following normal queue operations. Only enque() and deque(). So using these 2, your approach is correct !!!

Your Professor's approach:

Whenever a new process is coming, he is putting it on the top of the queue. He is considering a priority queue instead where every new process has top priority.

So better if you discuss it with your professor. I would say you are not wrong !!!

Upvotes: 3

brad
brad

Reputation: 559

Her answer was correct.

You can approach the problem like this:

  1. Open a new notebook page
  2. Number each line 0-15 (16 total lines; SUM(Burst) column)
  3. Start by writing the job on the line it arrives on (P1 on line numbered 0, P2 on line numbered 2, P3 on line numbered 3, P4 on line numbered 8)
  4. Now you know each job can only occur after that line.
  5. Next, start filling in the rest of the lines in sequential order where the lines have no jobs defined and where the jobs have appeared less than the number of Bursts defined for that job.

Hope this helps!

Upvotes: 2

Related Questions