You are on page 1of 4

Scheduling Algorithm

Linux:
Completely fair Scheduler (CFS) and Brain Fuck Scheduler (BFS) are two different process schedulers
currently used in Linux.

Process Scheduling:
As any program is loaded as process in RAM and then CPU executes the process according to the priority
of the process.

1. Completely Fair Scheduler (CFS):


 It is based on Rotating Staircase Deadline Scheduler (RSDL).
 It is the default scheduling process since version 2.6.23.
 Elegant handling of I/O and CPU bound process.

As the name suggests it fairly or equally divides the CPU time among all the processes. Before
understanding the CFS let’s look at the Ideal Fair Scheduling (IFS) of N processes. If there are N processes
in the ready queue then each process receives (100/N)% of CPU time according to IFS.

Process Burst Time


A 10
B 6
C 14
D 6

Take a time quantum of say 4ms. Initially, there is four processes waiting in the ready queue to be
executed and according to Ideal fair scheduling, each process gets equally fair time for it’s execution (Time
quantum/N).
So 4/4=1 each process gets 1ms to execute in first quantum. After the completion of six quantum process
B and D are completely executed and remaining are A and C, which are already executed for 6ms and their
remaining time is A=4ms and C=8ms).
In the seventh quantum of time A and C will execute (4/2=2ms as there are only two process remaining).
This is ideal fair scheduling in which each process gets equally share of time quantum no matter what
priority it is of.
Below is table description of the IFS.
Process Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9
A 1 1 1 1 1 1 2 2 -
B 1 1 1 1 1 1 - - -
C 1 1 1 1 1 1 2 2 4
D 1 1 1 1 1 1 - - -

CFS is quite simple algorithm for the process scheduling and it is implemented using RED BLACK Trees and
not queues.
So all the process which are on main memory are inserted into Red Black trees and whenever a new
process comes it is inserted into the tree. As we know that Red Black trees are self Balancing binary Search
trees.
In C++, we can use maps in STL as they are implemented using red black trees.

Time Complexity:
1. Insertion in red black trees takes O(logn).
2. Finding the node with lowest virtual time is O(1) as we can maintain a pointer.(In maps we can
use auto it=map.begin()).
Here Red black tree is once created and then we have a Red black tree of N process So the scheduling
time complexity is O(logn).

Advantages:
There is also one advantage of using RED Black Trees that is if a process is I/O bound then its virtual time
is going to be very less and it appears as the leftmost node in the Red black tree so executed first. So CFS
easily figure out the process which are I/O bound and which are CPU bound and it gives higher priority to
I/O bound process so avoiding the starvation.

1. Brain Fuck Scheduler (BFS):


Unlike the CFS scheduler, BFS is O(n) scheduler which uses doubly linked list which is treated like a queue.
So selection of the new process at time of context switch can go O(n) in the worst case and insertion of
the process is O(1) as the queue is used.
A global run-queue is used so that all the CPU has access to it. During context switch the queue is scanned
until a best process is found with the highest priority and then executed. Priority is decided based on the
virtual deadline formula for every process and executed accordingly.
It is not used in Linux anymore due to Context switch overhead and also O(n) time complexity.

Windows and Unix:


Windows uses a round-robin technique with a multi-level feedback queue for priority scheduling.
Unix also uses multilevel feedback queues.

Characteristics of Multi-Level Feedback Queue:


 In a multilevel queue-scheduling algorithm, processes are permanently assigned to a queue on
entry to the system and processes are not allowed to move between queues.
 As the processes are permanently assigned to the queue, this setup has the advantage of low
scheduling overhead,
 But on the other hand disadvantage of being inflexible.

Advantages:
 It is more flexible.
 It allows different processes to move between different queues.
 It prevents starvation by moving a process that waits too long for the lower priority queue to the
higher priority queue.
Disadvantages:
 For the selection of the best scheduler, it requires some other means to select the values.
 It produces more CPU overheads.
 It is the most complex algorithm.
Multilevel feedback queue scheduling, however, allows a process to move between queues. Multilevel
Feedback Queue Scheduling (MLFQ) keeps analyzing the behavior (time of execution) of processes and
according to which it changes its priority.

Queue 1

Queue 2

Queue 3

Now let us suppose that queues 1 and 2 follow round robin with time quantum 4 and 8 respectively and
queue 3 follow FCFS.
Implementation of MFQS is given below –
 When a process starts executing the operating system can insert it into any of the above three
queues depending upon its priority. For example, if it is some background process, then the
operating system would not like it to be given to higher priority queues such as queue 1 and 2. It
will directly assign it to a lower priority queue i.e. queue 3. Let’s say our current process for
consideration is of significant priority so it will be given queue 1.
 In queue 1 process executes for 4 units and if it completes in these 4 units or it gives CPU for I/O
operation in these 4 units then the priority of this process does not change and if it again comes
in the ready queue then it again starts its execution in Queue 1.
 If a process in queue 1 does not complete in 4 units then its priority gets reduced and it shifted to
queue 2.
 Above points 2 and 3 are also true for queue 2 processes but the time quantum is 8 units. In a
general case if a process does not complete in a time quantum then it is shifted to the lower
priority queue.
 In the last queue, processes are scheduled in an FCFS manner.
 A process in a lower priority queue can only execute only when higher priority queues are empty.
 A process running in the lower priority queue is interrupted by a process arriving in the higher
priority queue.

You might also like