CPU SCHEDULING
CPU SCHEDULING
Brief introduction
the CPU at the same time. When two or more of them are in the ready state, the
The part of the OS that does this is the scheduler and the algorithm it uses is
called the scheduling algorithm. Because the CPU is a scarce resource, a good
satisfaction.
The thing with scheduling however is that there is no one size fits all approach.
scheduling. Personal computers and handheld devices (e.g. mobile phones) may
When to schedule
Processes can be CPU-bound (meaning that they require more CPU time or
have long CPU bursts, and lesser I/O waits) or I/O bound (meaning they have
short CPU bursts and frequent I/O waits). Therefore, the scheduler is saddled
with the responsibility of making scheduling decisions, which could be in any
1. When a process switches from the running state to the waiting state (for
2. When a process switches from the running state to the ready state (for
3. When a process switches from the waiting state to the ready state (for
during clock interrupts because after clock interrupt processing has been
finished, the process that was running before the interrupt is resumed unless
of some fixed time. If the process has not finished executing, it is suspended
and the scheduler picks another available process to run. A clock interrupt
occurs at the end of the time interval to give the control of the CPU back to the
scheduler.
Scheduling criteria
them are desirable in all cases. The choice of a particular algorithm may favor
one class of processes over another. However, scheduling criteria include but
1. CPU utilization: This scheduling criteria measures how busy the CPU is.
completed per unit time. This basically means that the through put measures
maximize throughput.
3. Turnaround time: The interval from the arrival of a process until its
execution and exit is called its turnaround time. We seek to minimize the
4. Waiting time: This is the sum of the periods spent waiting in the ready
queue. The CPU scheduling algorithm should have a goal of minimizing the
6. Fairness: This implies that each process must be given a fair share of the
CPU.
7. Balance: This means that all parts of the system are to be kept busy as the
1. Batch systems: They are mainly used in the business world. They usually do
not have users waiting for a quick response at their terminal. Therefore, non-
process holding onto the CPU indefinitely and preventing others from
accessing it.
3. Real time systems: In real time environments, processes may not run for
long periods of time as they usually do their work and block quickly.
Scheduling Algorithms
the CPU in the order in which they arrive or request it. The processes are
managed with a first in first out queue. As more processes arrive, they are
attached to the end of the queue. Blocked processes that become ready are also
attached to the tail of the queue just like a newly arriving process. When the
CPU is free, it is assigned to the process at the head of the queue and the
Advantages
Disadvantages
The average waiting time may also vary if the processes’ CPU burst
When one big CPU bound process arrives first and gets hold of the CPU,
it hinders other processes from accessing the CPU until it gets off the
CPU, meaning that those processes outside the CPU will have to wait
indefinitely. This is called the convoy effect. This leads to poor CPU
utilization.
Shortest Job First scheduling works on the process with the shortest burst
time or duration first. This is the best approach to minimize waiting time. It is used
in Batch Systems.
It is of two types:
1. Non Pre-emptive
2. Pre-emptive
Consider the below processes available in the ready queue for execution,
as it has the shortest burst time, then P2, followed by P3 and at last P1.
If the arrival time for processes are different, which means all the processes are not
available in the ready queue at time 0, and some jobs arrive after some time, in
such situation, sometimes process with short burst time have to wait for the current
This leads to the problem of Starvation, where a shorter process has to wait for a
long time until the current longer process gets executed. This happens if shorter
jobs keep coming, but this can be solved using the concept of aging.
What is aging?
This a situation in which the priority of a process is increased based on its waiting
time. The longer the process waits, the higher its priority. At a point, the priority
would become high enough for the process to eventually be scheduled to run.
In Preemptive Shortest Job First Scheduling, jobs are put into ready queue as they
arrive, but as a process with short burst time arrives, the existing process is
preempted or removed from execution, and the shorter job is executed first.
processes.
Once a process is executed for the given time period that process is
preempted and another process executes for the given time period.
This algorithm is simple and easy to implement and the most important is
CPU.
It is important to note here that the length of time quantum is generally from
Algorithms.
specific task that needs to be processed. Though it may vary for different
operating systems.
Important terms
1. Completion Time
This mainly indicates the time Difference between completion time and
arrival time. The Formula to calculate the same is: Turn Around Time =
3. Waiting Time(W.T):
It Indicates the time Difference between turnaround time and burst time.
Example:
In the above diagram, arrival time is not mentioned so it is taken as 0 for all
processes.
Note: If arrival time is not given for any problem statement then it is taken as 0 for
Explanation
The value of time quantum in the above example is 5.Let us now calculate the
P1 21 32-0=32 32-21=11
P2 3 8-0=8 8-3=5
P3 6 21-0=21 21-6=15
P4 2 15-0=15 15-2=13
Average waiting time is calculated by adding the waiting time of all processes and
4. Priority Scheduling:
case can be internally or externally set. The process which is most urgent is
processed first, followed by the ones with lesser priority in order. Processes with
The priority of process, when internally defined, can be decided based on memory
requirements, time limits, number of open files, ratio of I/O burst to CPU burst etc.
Whereas, external priorities are set based on criteria outside the operating system,
like the importance of the process, funds paid for the computer resource use,
queue has a higher priority than the currently running process, the CPU is
preempted, which means the processing of the current process is stopped and
the incoming new process with higher priority gets the CPU for its
execution.
scheduling algorithm if a new process arrives with a higher priority than the
current running process, the incoming process is put at the head of the ready
queue, which means after the execution of the current process it will be
processed.
Consider the below table for processes with their respective CPU burst times and
the priorities.
In the Gantt chart, we see that processes are given CPU time just on the basis of
their priorities.
Problem with Priority Scheduling Algorithm
A process is considered blocked when it is ready to run but has to wait for the CPU
the ready queue then the processes waiting in the ready queue with lower priority
may have to wait for long durations before getting the CPU for execution. This is
referred to as starvation.
To prevent starvation of any process, we can use the concept of aging where we
keep on increasing the priority of low-priority process based on its waiting time.
For example, if we decide the aging factor to be 0.5 for each day of waiting, then if
a process with priority 20 comes in the ready queue. After one day of waiting, its
Another class of scheduling algorithms has been created for situations in which
processes and background (or batch) processes. These two types of processes have
processes.
Multi-level queue scheduling algorithm partitions the ready queue into several
separate queues. The processes are permanently assigned to one queue, generally
based on some property of the process, such as memory size, process priority, or
For example, separate queues might be used for foreground and background
queues:
1. System Processes
2. Interactive Processes
4. Batch Processes
5. Student Processes
Each queue has absolute priority over lower-priority queues. No process in the
batch queue, for example, could run unless the queues for system processes,
interactive editing process entered the ready queue while a batch process was
processes on the low priority queues will run. For Example: Once processes on the
system queue, the Interactive queue, and Interactive editing queue become empty,
System Process
The Operating system itself has its own process to run and is termed as
System Process.
Interactive Process
The Interactive Process is a process in which there should be the same kind
collects the programs and data together in the form of the batch before
Student Process
The system process always gets the highest priority while the student
In an operating system, there are many processes, in order to obtain the result we
cannot put all processes in a queue; thus this process is solved by Multilevel queue
scheduling.
With the help of this scheduling we can apply various kind of scheduling for
Starvation:
Due to starvation lower-level processes either never execute or have to wait for a
long amount of time because of lower priority or higher priority process taking a