Search code examples
processoperating-systemschedulerround-robin

Processes sharing a CPU (scheduler)


Question A scheduler attempts to share the CPU between multiple processes. Two processes, P1 and P2, are running. P1 does many I/O operation, while P2 does very few.

Explain what happens if a simple ‘round robin’ pre-emptive scheduling algorithm is used to schedule P1 and P2.

My Attempt From my understanding, a scheduler is said to be pre-emptive when it has the ability to be invoked by an interrupt and move a process from running state to another and then moving another process to the running state. Round-robin means that each process, P1 and P2, would get an equal time with the CPU but if P1 is performing many I/O operations while P2 performs fewer, wouldn't P1 get more time on with the CPU as it has many more operations? If each Process was given for example 1 second, if P1 had to perform 50 I/O operations (each taking 1 second, for simplicity) while P2 had to perform 3 I/O operations, would I be correct in assuming that the order would go: P1,P2,P1,P2,P1,P2,P1,P1 (continuing with P1 till the operations are complete).

That is my understanding hopefully some of you guys can provide more insight. Thank You.


Solution

  • Your understanding is pretty close to the mark.

    Round robin means that the scheduler picks each process in turn. So if there are only two processes, the scheduler will pick one and then the other (assuming both are ready).

    As to your first question, process P2 actually gets more CPU time. Here is an example where P1 is scheduled first and does an I/O after .5 seconds:

    Time(seconds)     What
        0             P1 starts
       .5             P1 does I/O; P2 is scheduled
      1.5             P2's time is up; P1 is scheduled because its I/O has finished
      2.0             P1 does I/O; P2 is scheduled
      3.0             P2's time is up, P1 is scheduled because its I/O has completed
    
      Total P1 time: 1 second
      Total P2 time: 2 seconds
    

    You can see that because P1 does more I/O, it gets less total CPU time because the scheduler doesn't take into account the fact that P1 doesn't use all of its allocated time.

    If both P1 and P2 do I/O, the schedule will still be:

    P1, P2, P1, P2, P1, P2, etc.
    

    because if P1 yields the CPU, P2 is ready and vice versa.