# Selfish Round Robin CPU Scheduling

Prerequisite – Program for Round Robin scheduling

In the traditional Round Robin scheduling algorithm all processes were treated equally for processing. The objective of the Selfish Round Robin is to give better service to processes that have been executing for a while than to newcomers. Its a more logical and superior implementation compared to the normal Round Robin algorithm.

Implimentation :-

• Processes in the ready list are partitioned into two lists: NEW and ACCEPTED.
• The New processes wait while Accepted processes are serviced by the Round Robin.
• Priority of a new process increases at rate ‘a’ while the priority of an accepted process increases at rate ‘b’.
• When the priority of a new process reaches the priority of an accepted process, that new process becomes accepted.
• If all accepted processes finish, the highest priority new process is accepted.

Let’s trace out the general working of this algorithm :-

STEP 1 : Assume that initially there are no ready processes, when the first one, A, arrives. It has priority 0 to begin with. Since there are no other accepted processes, A is accepted immediately.
STEP 2 : After a while another process, B, arrives. As long as b / a < 1, B’s priority will eventually catch up to A’s, so it is accepted; now both A and B have the same priority.
STEP 3 : All accepted processes share a common priority (which rises at rate b ); that makes this policy easy to implement i.e any new process’s priority is bound to get accepted at some point. So no process has to experience starvation.
STEP 4 : Even if b / a > 1, A will eventually finish, and then B can be accepted.

```Adjusting the parameters a and b :
-> If b / a >= 1, a new process is not accepted
until all the accepted processes have finished, so SRR becomes FCFS.
-> If b / a = 0, all processes are accepted immediately, so SRR becomes RR.
-> If 0 < b / a < 1, accepted processes are selfish, but not completely.
```

Example on Selfish Round Robin –

Solution (where a = 2 and b = 1) –

Explanation –

Process A gets accepted as soon as it comes at time t = 0. So its priority is increased only by ‘b’ i.e ‘1’ after each second. B enters at time t = 1 and goes to the waiting queue. So its priority gets increased by ‘a’ i.e. ‘2’ at time t = 2. At this point priority of A = priority of B = 2.

So now both process A & B are in the accepted queue and are executed in a round robin fashion. At time t = 3 process C enters the waiting queue. At time t = 6 the priority of process C catches up to the priority of process B and then they start executing in a Round Robin manner. When B finishes execution at time t = 10, D is automatically promoted to the accepted queue.

Similarly when D finishes execution at time t = 15, E is automatically promoted to the accepted queue.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.