Unit2 3.cpu Scheduling
Unit2 3.cpu Scheduling
• Shortest job first (SJF) is a scheduling process that selects the waiting
process with the smallest execution time to execute next.
• This scheduling method may or may not be preemptive.
• Significantly reduces the average waiting time for other processes waiting
to be executed.
• The full form of SJF is Shortest Job First.
Characteristics of SJF
• Shortest Job first has the advantage of having a minimum average waiting
time among all operating system scheduling algorithms.
• It is associated with each task as a unit of time to complete.
• It may cause starvation if shorter processes keep coming. This problem can
be solved using the concept of ageing.
Advantages of Shortest Job first
• As SJF reduces the average waiting time thus, it is better than the first
come first serve scheduling algorithm.
• SJF is generally used for long term scheduling
Disadvantages of SJF
• One of the demerit SJF has is starvation.
• Many times it becomes complicated to predict the length of the
upcoming CPU request
Problem
P1 0 ms 5 ms
P2 1 ms 3 ms
P3 2 ms 3 ms
P4 4 ms 1 ms
Round Robin Scheduling
• There is fairness since every process gets an equal share of the CPU.
• The newly created process is added to the end of the ready queue.
• A round-robin scheduler generally employs time-sharing, giving each job a time
slot or quantum.
• While performing a round-robin scheduling, a particular time quantum is allotted
to different jobs.
• Each process get a chance to reschedule after a particular quantum time in this
scheduling.
Disadvantages
• There is Larger waiting time and Response time.
• There is Low throughput.
• There is Context Switches.
• Gantt chart seems to come too big (if quantum time is less for scheduling. For
Example:1 ms for big scheduling.)
• Time consuming scheduling for small quantum.
Examples to show working of Round Robin Scheduling Algorithm
Time Quantum = 5
If a process having high priority If a process with a long burst time is running
Starvation frequently arrives in the ready queue, a CPU, then later coming process with less
low priority process may starve. CPU burst time may starve.
Quantum Time 4
Preemptive Priority CPU Scheduling Algorithm
How does Preemptive Priority CPU Scheduling
Algorithm work?
• Step-1: Select the first process whose arrival time will be 0, we need to select
that process because that process is only executing at time t=0.
• Step-2: Check the priority of the next available process. Here we need to check
for 3 conditions.
• if priority(current_process) > priority(prior_process) :- then execute the current process.
• if priority(current_process) < priority(prior_process) :- then execute the prior process.
• if priority(current_process) = priority(prior_process) :- then execute the process which
arrives first i.e., arrival time should be first.
• Step-3: Repeat Step-2 until it reaches the final process.
• Step-4: When it reaches the final process, choose the process which is having
the highest priority & execute it. Repeat the same step until all processes
complete their execution.
Preemptive Priority CPU Scheduling Algorithm
Turn Around Time (T.A.T) = (Completion Time) – (Arrival Time)
Waiting Time (W.T) = (Turn Around Time) – (Burst Time)
Response Time (R.T) = (First Arrival Time) – (Arrival Time)
Multilevel Queue (MLQ) CPU Scheduling
• It may happen that processes in the ready queue can be divided into different classes
where each class has its own scheduling needs.
• For example, a common division is a foreground (interactive) process and
a background (batch) process. These two classes have different scheduling needs.
For this kind of situation, Multilevel Queue Scheduling is used.
Features of Multilevel Queue (MLQ)
Multiple queues
Multiple queues are maintained for processes with common
characteristics
Priorities assigned
Priorities are assigned to processes based on their type,
characteristics, and importance.
For example, interactive processes like user input/output may
have a higher priority than batch processes like file backups.
Pre-emption
Preemption is allowed in MLQ scheduling, which means a higher
priority process can preempt a lower priority process, and the CPU
is allocated to the higher priority process.
Scheduling algorithm
Different scheduling algorithms can be used for each queue, depending
on the requirements of the processes in that queue.
For example, Round Robin scheduling may be used for interactive
processes, while First Come First Serve scheduling may be used for batch
processes.
Feedback mechanism
A feedback mechanism can be implemented to adjust the priority of a
process based on its behavior over time.
For example, if an interactive process has been waiting in a lower-priority
queue for a long time, its priority may be increased to ensure it is
executed in a timely manner.
Efficient allocation of CPU time
MLQ scheduling ensures that processes with higher priority levels are
executed in a timely manner, while still allowing lower priority processes
to execute when the CPU is idle.
Fairness
MLQ scheduling provides a fair allocation of CPU time to different
types of processes, based on their priority and requirements.
Customizable
MLQ scheduling can be customized to meet the specific
requirements of different types of processes.
• Ready Queue is divided into separate queues for each class of
processes.
• For example, let us take three different types of processes
System processes, Interactive processes, and Batch Processes.
All three processes have their own queue.
Multilevel Feedback Queue Scheduling (MLFQ)
CPU Scheduling
• Multilevel Feedback Queue Scheduling (MLFQ) CPU
Scheduling is like Multilevel Queue(MLQ) Scheduling but in this
process can move between the queues.