Question

If we have three processes P1,P2,P3 in ready Queue. Suppose Time Slice is 4 sec and P1 has CPU burst 2 sec and then it has an I/O burst of 5 sec. Now CPU will execute P1 for 2 sec and here comes two scenarios

  1. Will CPU give 2 sec to P1 and because time slice was 4 sec so CPU will remember that P1 has consumed 2 sec and when it'll return from I/O for its next CPU burst 2 sec will be given to P1 instead of 4 secs of time slice? (if next CPU burst is lets say greater than 2 secs)

  2. Will CPU give 2 sec to P1 and because its CPU burst is over and it has voluntarily left CPU and has gone for I/O burst so CPU don't have to remember anything and when it'll return from I/O it'll be given 4 secs of time slices BUT there is a problem here.... Suppose P1 has CPU burst 5 secs. CPU gives 4 sec to P1 and moves it to the tail of queue and when its P1's turn again it gives 1 sec to it because it has 1 sec of CPU burst remaining and it'll go for I/O and now CPU will remember that it has consumed 1 sec of time slice and whenever it'll return from I/O, 3 secs will be given to it but isn't it the same scenario? P1 is given 4 secs of time slice, it consumes 1 and CPU burst ends and it voluntarily leaves CPU same as above case and go for I/O and CPU shouldn't remember that it'll be given only 3 secs after it returns from I/O? and in this case, CPU don't have to remember anything in any case and will give 4 secs to every process when it returns from I/O.

Which Scenario is correct? Can you explain ?

Was it helpful?

Solution

Short answer: It makes sense that P1 is given 4 seconds when it comes back, but that's up to the OS design. It could be given 17 seconds but that's not related to round robin.

Long answer: Let's start with the most basic scenario: Without I/O. Each proccess is given 4 seconds to dance until it's the turn of the next one. This is until one is finished dancing so only the rest of the processes are given turns.

Now, add I/O to it, but suppose I/O is NEVER busy and always instantaneous. Reading from I/O USES CPU time. So process 1 starts dancing for 2 seconds, and then uses I/O for 2 seconds. Then CPU says FREEZE! and process 2 starts dancing. On next P1 turn, it will read I/O for 4 seconds, and on last turn it will only consume 1 second and then say I'M DONE! So it will exit the dancing room.

Let's now take a look at the scenario in which I/O can be busy. Process 1 starts dancing for two seconds and then wants to read from I/O, but it's busy! So it crosses its arms and waits doing nothing for his remaining two seconds. Then CPU works on process 2, then 3 and then goes back to 1. I/O still isn't ready for another 1 second, so process 1 still waits for that second, and then starts using it for 3 seconds. And so on.

Now this "waiting for 3 seconds" is terrible, since it implies the CPU is not doing anything while it could be working on the rest of the processes. To avoid this situatiation, the things go a little different: Process 1 starts dancing for two seconds and then says "I can't do nothing but wait until I/O is ready". So CPU says "no probs, I tell you when it's ready" and goes to next process having P1 only danced for 2 seconds. And works on 2 and 3 until it's interrupted by I/O: "READY BOSS". At this instant it gives control to P1, which uses I/O for 4 seconds, and then the algorithm continues as always.

Now this is the most simple implementation I can think of. Could you write down the time P1 didn't use because it was waiting and then compensate it giving more time when it can run? Well yeah, you can. Round robin doesn't really have much to do with it. It's more about how you decide to treat your interruptions.

And what happens if two processes need I/O? Well, then they can BOTH be put on hold.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top