CPU Schedualing
CPU Schedualing
CPU scheduling is a process which allows one process to use the CPU while the execution of another process is on hold
(in waiting state) due to unavailability of any resource like I/O etc, thereby making full use of CPU. The aim of CPU
scheduling is to make the system efficient, fast and fair.
Whenever the CPU becomes idle, the operating system must select one of the processes in the ready queue to be
executed. The selection process is carried out by the short-term scheduler (or CPU scheduler). The scheduler selects
from among the processes in memory that are ready to execute, and allocates the CPU to one of them.
SCHEDULING ALGORITHMS
To decide which process to execute first and which process to execute last to achieve maximum CPU utilisation,
computer scientists have defined some algorithms, they are:
1. First Come First Serve (FCFS) Scheduling
2. Shortest-Job-First (SJF) Scheduling
3. Priority Scheduling
4. Round Robin (RR) Scheduling
5. Multilevel Queue Scheduling
6. Multilevel Feedback Queue Scheduling
As you can see in the GANTT chart above, the process P4 will be picked up first as it has the shortest burst time,
then P2, followed by P3 and at last P1.
We scheduled the same set of processes using the First come first serve algorithm in the previous tutorial, and
got average waiting time to be 18.75 ms, whereas with SJF, the average waiting time comes out 4.5 ms.
• Problem with Non Pre-emptive SJF
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 process's execution to finish, because in Non Pre-emptive SJF, on arrival
of a process with short duration, the existing job/process's execution is not halted/stopped to execute the
short job first.
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.
2. Pre-emptive Shortest Job First
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.
As you can see in the GANTT chart above, as P1 arrives first, hence it's execution starts immediately, but just
after 1 ms, process P2 arrives with a burst time of 3 ms which is less than the burst time of P1, hence the
process P1(1 ms done, 20 ms left) is preemptied and process P2 is executed.
As P2 is getting executed, after 1 ms, P3 arrives, but it has a burst time greater than that of P2, hence execution
of P2 continues. But after another millisecond, P4 arrives with a burst time of 2 ms, as a result P2(2 ms done, 1
ms left) is preemptied and P4 is executed.
After the completion of P4, process P2 is picked up and finishes, then P2 will get executed and at last P1.
The Pre-emptive SJF is also known as Shortest Remaining Time First, because at any given point of time, the
job with the shortest remaining time is executed first.
3. Priority CPU Scheduling
In this tutorial we will understand the priority scheduling algorithm, how it works and its advantages and disadvantages.
In the Shortest Job First scheduling algorithm, the priority of a process is generally the inverse of the CPU burst time,
i.e. the larger the burst time the lower is the priority of that process.
In case of priority scheduling the priority is not always set as the inverse of the CPU burst time, rather it can be internally
or externally set, but yes the scheduling is done on the basis of priority of the process where the process which is most
urgent is processed first, followed by the ones with lesser priority in order.
Processes with same priority are executed in FCFS manner.
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, makrte factor etc
Types of Priority Scheduling Algorithm
Priority scheduling can be of two types:
1. Preemptive Priority Scheduling: If the new process arrived at the ready queue has a higher priority than the
currently running process, the CPU is preempted, which means the processing of the current process is stoped
and the incoming new process with higher priority gets the CPU for its execution.
2. Non-Preemptive Priority Scheduling: In case of non-preemptive priority 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.
Example of Priority Scheduling Algorithm
Consider the below table fo processes with their respective CPU burst times and the priorities.
As you can see in the GANTT chart that the processes are given CPU time just on the basis of the priorities.
Problem with Priority Scheduling Algorithm
In priority scheduling algorithm, the chances of indefinite blocking or starvation.
A process is considered blocked when it is ready to run but has to wait for the CPU as some other process is running
currently.
But in case of priority scheduling if new higher priority processes keeps coming in 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.
In 1973, when the IBM 7904 machine was shut down at MIT, a low-priority process was found which was submitted in
1967 and had not yet been run.
Using Aging Technique with Priority Scheduling
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 the 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(which is
comparatively low priority) comes in the ready queue. After one day of waiting, its priority is increased to 19.5 and so
on.
Doing so, we can ensure that no process will have to wait for indefinite time for getting CPU time for processing.
4. Round Robin Scheduling
• A fixed time is allotted to each process, called quantum, for execution.
• Once a process is executed for given time period that process is preemptied and other process executes for given
time period.
• Context switching is used to save states of preemptied processes.
• switching is used to save states of preemptied processes.