Assignment2 OS (BSEF19M532)
Assignment2 OS (BSEF19M532)
Assignment2 OS (BSEF19M532)
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.
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.
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.
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.