You are on page 1of 8

CPU Scheduling in Operating Systems

Scheduling of processes/work is done to finish the work on time.


Below are different time with respect to a process.

Arrival Time: Time at which the process arrives in the ready queue.


Completion Time: Time at which process completes its execution.
Burst Time: Time required by a process for CPU execution.
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T):  Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time

Why do we need scheduling?


A typical process involves both I/O time and CPU time. In a uni programming system like MS-DOS, time
spent waiting for I/O is wasted and CPU is free during this time. In multi programming systems, one
process can use CPU while another is waiting for I/O. This is possible only with process scheduling.

Operating System - Process Scheduling


Definition
The process scheduling is the activity of the process manager that handles the removal of the
running process from the CPU and the selection of another process on the basis of a particular
strategy.
Process scheduling is an essential part of a Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at a
time and the loaded process shares the CPU using time multiplexing.
Objectives of Process Scheduling Algorithm:

Max CPU utilization [Keep CPU as busy as possible]


Fair allocation of CPU.
Max throughput [Number of processes that complete their execution per time unit]
Min turnaround time [Time taken by a process to finish execution]
Min waiting time [Time a process waits in ready queue]
Min response time [Time when a process produces first response]

Process Scheduling Queues


The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate queue
for each of the process states and PCBs of all processes in the same execution state are placed in
the same queue. When the state of a process is changed, its PCB is unlinked from its current
queue and moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
 Job queue − This queue keeps all the processes in the system.
 Ready queue − This queue keeps a set of all processes residing in main memory, ready
and waiting to execute. A new process is always put in this queue.
 Device queues − The processes which are blocked due to unavailability of an I/O device
constitute this queue.

The OS can use different policies to manage each queue (FIFO, Round Robin, Priority, etc.). The OS
scheduler determines how to move processes between the ready and run queues which can only have one
entry per processor core on the system; in the above diagram, it has been merged with the CPU.

Two-State Process Model


Two-state process model refers to running and non-running states which are described below −

S.N. State & Description

1 Running
When a new process is created, it enters into the system as in the running
state.

2 Not Running
Processes that are not running are kept in queue, waiting for their turn to
execute. Each entry in the queue is a pointer to a particular process.
Queue is implemented by using linked list. Use of dispatcher is as
follows. When a process is interrupted, that process is transferred in the
waiting queue. If the process has completed or aborted, the process is
discarded. In either case, the dispatcher then selects a process from the
queue to execute.
Schedulers
Schedulers are special system software which handle process scheduling in various ways. Their
main task is to select the jobs to be submitted into the system and to decide which process to
run. Schedulers are of three types −

 Long-Term Scheduler
 Short-Term Scheduler
 Medium-Term Scheduler

Long Term Scheduler


It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing. It selects processes from the queue and loads them into
memory for execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. It also controls the degree of multiprogramming. If the degree of
multiprogramming is stable, then the average rate of process creation must be equal to the
average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long term scheduler. When a process changes the state from new to
ready, then there is use of long-term scheduler.

Short Term Scheduler


It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of the
process. CPU scheduler selects a process among the processes that are ready to execute and
allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.

Medium Term Scheduler


Medium-term scheduling is a part of swapping. It removes the processes from the memory. It
reduces the degree of multiprogramming. The medium-term scheduler is in-charge of handling
the swapped out-processes.
A running process may become suspended if it makes an I/O request. A suspended processes
cannot make any progress towards completion. In this condition, to remove the process from
memory and make space for other processes, the suspended process is moved to the secondary
storage. This process is called swapping, and the process is said to be swapped out or rolled
out. Swapping may be necessary to improve the process mix.

Comparison among Scheduler


S.N. Long-Term Scheduler Short-Term Scheduler Medium-Term Scheduler
1 It is a job scheduler It is a CPU scheduler It is a process swapping
scheduler.

2 Speed is lesser than short term Speed is fastest among Speed is in between both short
scheduler other two and long term scheduler.

3 It controls the degree of It provides lesser control It reduces the degree of


multiprogramming over degree of multiprogramming.
multiprogramming

4 It is almost absent or minimal It is also minimal in time It is a part of Time sharing


in time sharing system sharing system systems.

5 It selects processes from pool It selects those processes It can re-introduce the process
and loads them into memory which are ready to execute into memory and execution can
for execution be continued.

Context Switch
A context switch is the mechanism to store and restore the state or context of a CPU in Process
Control block so that a process execution can be resumed from the same point at a later time.
Using this technique, a context switcher enables multiple processes to share a single CPU.
Context switching is an essential part of a multitasking operating system features.
When the scheduler switches the CPU from executing one process to execute another, the state
from the current running process is stored into the process control block. After this, the state for
the process to run next is loaded from its own PCB and used to set the PC, registers, etc. At that
point, the second process can start executing.
Context switches are computationally intensive since register and memory state must be saved
and restored. To avoid the amount of context switching time, some hardware systems employ
two or more sets of processor registers. When the process is switched, the following
information is stored for later use.

 Program Counter
 Scheduling information
 Base and limit register value
 Currently used register
 Changed State
 I/O State information
 Accounting information
Preemptive and Non-Preemptive Scheduling
Preemptive Scheduling is
a CPU scheduling technique that works by dividing time slots of CPU to a
given process. The time slot given might be able to complete the whole process or might not be
able to it. When the burst time of the process is greater than CPU cycle, it is placed back into the
ready queue and will execute in the next chance. This scheduling is used when the process
switch to ready state.
Algorithms that are backed by preemptive Scheduling are round-robin (RR), priority, SRTF
(shortest remaining time first).
 In this type of scheduling, the resources (CPU Cycle) have been allocated to a process for
a limited amount of time.
 A process can be interrupted when it is being executed.
 If a process that has a high priority arrives frequently in the ‘ready’ queue, the low
priority processes may starve.
 This kind of scheduling has overheads since it has to schedule multiple processes.
 It is flexible in nature.
 It is expensive in nature.
 The CPU utilization is high in this type of scheduling.
 Examples of pre-emptive scheduling include: Round Robin scheduling, Shortest
Remaining Time First scheduling.
Non-preemptive Scheduling is a CPU scheduling technique the process takes the resource (CPU
time) and holds it till the process gets terminated or is pushed to the waiting state. No process is
interrupted until it is completed, and after that processor switches to another process.
Algorithms that are based on non-preemptive Scheduling are non-preemptive priority, and
shortest Job first.
 In this type of scheduling, once the resources (CPU Cycle) have been allocated to a
process, the process holds it until it completes its burst time or switches to the ‘wait’
state.
 A process can’t be interrupted until it terminates itself or its time is over.
 If a process that has a long burst time is running the CPU, then the process that has less
CPU burst time would starve.
 It doesn’t have overhead.
 It is not flexible in nature.
 It is not expensive in nature.
 Examples of non-pre-emptive scheduling include: First Come First Serve and Shortest
Job First.

Preemptive Vs Non-Preemptive Scheduling

Preemptive Scheduling Non-Preemptive Scheduling

Resources are allocated according to the Resources are used and then held by the
Preemptive Scheduling Non-Preemptive Scheduling

cycles for a limited time. process until it gets terminated.

The process can be interrupted, even before The process is not interrupted until its life
the completion. cycle is complete.

Starvation may be caused, due to the insertion Starvation can occur when a process with
of priority process in the queue. large burst time occupies the system.

Maintaining queue and remaining time needs No such overheads are required.
storage overhead.

Different Scheduling Algorithms


First Come First Serve (FCFS): Simplest scheduling algorithm that schedules according to
arrival times of processes. First come first serve scheduling algorithm states that the process
that requests the CPU first is allocated the CPU first. It is implemented by using the FIFO
queue. When a process enters the ready queue, its PCB is linked onto the tail of the queue.
When the CPU is free, it is allocated to the process at the head of the queue. The running
process is then removed from the queue. FCFS is a non-preemptive scheduling algorithm.
Note:First come first serve suffers from convoy effect.

Shortest Job First (SJF): Process which have the shortest burst time are scheduled first.If two
processes have the same bust time then FCFS is used to break the tie. It is a non-preemptive
scheduling algorithm.

Longest Job First (LJF): It is similar to SJF scheduling algorithm. But, in this scheduling
algorithm, we give priority to the process having the longest burst time. This is non-preemptive
in nature i.e., when any process starts executing, can’t be interrupted before complete
execution.
Shortest Remaining Time First (SRTF): It is preemptive mode of SJF algorithm in which jobs
are schedule according to shortest remaining time.

Longest Remaining Time First (LRTF): It is preemptive mode of LJF algorithm in which we
give priority to the process having largest burst time remaining.

Round Robin Scheduling : Each process is assigned a fixed time(Time Quantum/Time Slice) in
cyclic way.It is designed especially for the time-sharing system. The ready queue is treated as a
circular queue. The CPU scheduler goes around the ready queue, allocating the CPU to each
process for a time interval of up to 1-time quantum. To implement Round Robin scheduling,
we keep the ready queue as a FIFO queue of processes. New processes are added to the tail of
the ready queue. The CPU scheduler picks the first process from the ready queue, sets a timer
to interrupt after 1-time quantum, and dispatches the process. One of two things will then
happen. The process may have a CPU burst of less than 1-time quantum. In this case, the
process itself will release the CPU voluntarily. The scheduler will then proceed to the next
process in the ready queue. Otherwise, if the CPU burst of the currently running process is
longer than 1-time quantum, the timer will go off and will cause an interrupt to the operating
system. A context switch will be executed, and the process will be put at the tail of the ready
queue. The CPU scheduler will then select the next process in the ready queue.

Priority Based scheduling (Non-Preemptive) : In this scheduling, processes are scheduled
according to their priorities, i.e., highest priority process is scheduled first. If priorities of two
processes match, then schedule according to arrival time. Here starvation of process is
possible.

Highest Response Ratio Next (HRRN): In this scheduling, processes with highest response ratio
is scheduled. This algorithm avoids starvation.
Response Ratio = (Waiting Time + Burst time) / Burst time
Multilevel Queue Scheduling : According to the priority of process, processes are placed in the
different queues. Generally high priority process are placed in the top level queue. Only after
completion of processes from top level queue, lower level queued processes are scheduled. It
can suffer from starvation.

Multi level Feedback Queue Scheduling : It allows the process to move in between queues. The
idea is to separate processes according to the characteristics of their CPU bursts. If a process
uses too much CPU time, it is moved to a lower-priority queue.

Some useful facts about Scheduling Algorithms:


FCFS can cause long waiting times, especially when the first job takes too much CPU time.
1. Both SJF and Shortest Remaining time first algorithms may cause starvation. Consider a
situation when the long process is there in the ready queue and shorter processes keep
coming.
2. If time quantum for Round Robin scheduling is very large, then it behaves same as FCFS
scheduling.
3. SJF is optimal in terms of average waiting time for a given set of processes,i.e., average
waiting time is minimum with this scheduling, but problems are, how to know/predict the
time of next job.
 

You might also like