Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

CPU Scheduling

Presented
by
Soma Hazra
Outline…
Basic Concepts
Scheduling Criteria
Scheduling Algorithms
First-Come-First-Serve (FCFS) Scheduling
Shortest Job First (SJF) Scheduling
Priority Scheduling
Round-Robin (RR) Scheduling
Multilevel Queue Scheduling
Multilevel Feedback Queue Scheduling
Basic Concepts
Scheduling is a fundamental operating-system
function.
Almost all computer resources are scheduled before
use.
CPU scheduling is the basis of multi-programmed
operating system.
The objective of multiprogramming is to Maximize
the CPU utilization.
As CPU is one of the primary resources, its scheduling
is central to the operating-system design.
The success of CPU scheduling depends on the
process execution cycle.
The execution cycles of process are called as CPU
burst and I/O burst cycle.
CPU - I/O Bursts Cycle
Processes alternate between
these two states.
Process execution begins with
a CPU burst.
That is followed by an I/O
burst then another CPU burst,
then another I/O burst, and so
on.
The last CPU burst will end up
with a system request to
terminate execution, rather
than with another I/O burst.
CPU Scheduler
Selects from among the processes in memory that are
ready to execute, and allocates the CPU to one of them.
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state.
2. Switches from running to ready state.
3. Switches from waiting to ready.
4. Terminates.
Scheduling under 1 and 4 is nonpreemptive.
All other scheduling is preemptive.
Under nonpreemptive scheduling, once the CPU has been
allocated to a process, the process keeps the CPU until it
releases the CPU either by terminating or by switching to
the waiting state.
Dispatcher
 Dispatcher module gives control of the CPU to the
process selected by the short-term scheduler. This
involves:
switching context
switching to user mode
jumping to the proper location in the user program to
restart that program
Dispatch latency – time it takes for the dispatcher to
stop one process and start another running.
Scheduling Criteria
CPU utilization – keep the CPU as busy as possible.
Throughput – the number of processes that complete their
execution per time unit.
Turnaround time – the interval from the time of submission
to the time of completion of a process. [OR]
 Turnaround time (TAT) = Finishing time – Arrival time
Waiting time – the sum of the time periods spends waiting
in the ready queue. [OR]
 Waiting time (WT) = Starting time – Arrival time
Response time – amount of time it takes from when a
request was submitted until the first response is produced.
[OR]
 Response time (RT) = First response – Arrival time
Optimization Criteria
Maximum CPU utilization
Maximum throughput
Minimum turnaround time
Minimum waiting time
Minimum response time

Note: The algorithm which gives the minimum


waiting time, turn around time and response
is the best one.
First-Come, First-Served (FCFS)
Scheduling
FCFS is the simplest CPU scheduling algorithm.
The process that requests the CPU first is allocated the
CPU first.
The implementation of the FCFS policy is easily managed
with a FIFO queue.
When the process enters the ready queue, its PCB is linked
on to the tail of the FIFO 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.
The average waiting time under FCFS policy is quite long.
FCFS scheduling algorithm is nonpreemtive.
FCFS Scheduling example
Example : Process Burst Time (in mili second)
P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3

0 24 27 30
Turnaround time for P1 = 24; P2 = 27; P3 = 30
Average TAT time : (24 + 27 + 30 )/3 = 27 ms
Waiting time for P1 = 0; P2 = 24; P3 = 27
Average WT: (0 + 24 + 27)/3 = 17 ms
Response time for P1 = 0; P2 = 24; P3 = 27 (Same as WT in Case Non-PS)
FCFS Scheduling contd…
Suppose that the processes arrive in the order P2 , P3 , P1 .
 The Gantt chart for the schedule is:
P2 P3 P1

0 3 6 30

 Turnaround time for P1 = 30; P2 = 3; P3 = 6


 Average TAT: (3 + 6 + 30)/3 = 13 ms
 Waiting time for P1 = 6; P2 = 0; P3 = 3
 Average WT: (6 + 0 + 3)/3 = 3 ms
 Much better than previous case.
 Convoy effect - short process behind long process.
 This effect results in lower CPU and device utilization.
Shortest-Job-First (SJF) Scheduling
 Associate with each process, there is a length of its next CPU
burst. Use these lengths to schedule the process with the
shortest time.
 Two schemes:
 nonpreemptive – once CPU given to the process it cannot
be preempted until completes its CPU burst.
 preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is known as the
Shortest-Remaining-Time-First (SRTF).
 SJF is optimal – gives minimum average waiting time for a
given set of processes.
Example of Non-Preemptive SJF
Example: Process Arrival Time Burst Time (in ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (non-preemptive)

P1 P3 P2 P4

0 3 7 8 12 16

Waiting time for P1= 0; P2 = 8-2 = 6; P3 = 7-4 = 3; P4 = 12-5 = 7


Average waiting time = (0 + 6 + 3 + 7)/4 = 4 ms
Example of Preemptive SJF
Example: Process Arrival Time Burst Time (ms)
P1 0 7
P2 2 4
P3 4 1
P4 5 4
 SJF (preemptive)

P1 P2 P3 P2 P4 P1

0 2 4 5 7 11 16

 Waiting Time for: P1 = [{0+(11-2)}-0] = 9; P2 = [{2+(5-4)}-2] = 1;


P3 = (4-4) = 0; P4 = (7-5) = 2 ;
Average waiting time = (9 + 1 + 0 +2)/4 = 3
Priority Scheduling
A priority number (integer) is associated with each process.
The CPU is allocated to the process with the highest priority
(smallest integer  highest priority).
SJF is a special case of priority scheduling algorithm where
priority (p) is the predicted next CPU burst time.
Equal priority processes are scheduled in FCFS order.
Priority can be defined either internally or externally.
 Internal priorities are set by taking some measurable quantities
like, time limits, memory requirements, the number of open files,
and the ratio of avg. I/O burst to avg. CPU burst time.
 External priorities are set by taking the external factors (to O.S)
such as the importance of the process, the type and the amount
of funds being paid for computer use, even the political factors.
Priority Scheduling contd…
Priority scheduling is either preemptive or nonpreemptive.
 A preemptive priority-scheduling algorithm will preempt the CPU
if the priority of the newly arrived process is higher than the
priority of the current running process.
 A nonpreemptive priority-scheduling algorithm will simply put the
new process at the head of the ready queue.
Problem  Starvation – low priority processes may never
execute.
 Also called as indefinite blocking.
Solution  Aging – as time progresses increase the priority
of the process.
Round Robin Scheduling (RR)
It is designed especially for the time-sharing systems.
It is similar to FCFS scheduling but preemption is added to
switch between processes.
Each process gets a small unit of CPU time (time slice or time
quantum), usually 10-100 milliseconds. After this time has
elapsed, the process is preempted and added to the end/tail
of the ready queue.
The ready queue is treated as a circular queue.
Performance
 If q is very large  similar to FCFS policy.
 If q is very small  many switching will occur and it may cause
overhead to the system.
Example of RR with Time Quantum =
5 ms
Process Burst Time
P1 30
P2 6
P3 8
The Gantt chart is:

P1 P2 P3 P1 P2 P3 P1 P1 P1 P1

0 5 10 15 20 21 24 29 34 39 44

Waiting time for P1 = {0+(15-5)+(24-20)} = 14;


P2 = {5+(20-10)} = 15; P3 = {10+(21-15)} = 16
Average WT: (14 + 15 + 16)/3 = 15 ms
Average TAT = 29.66 ms and Average RT = 5 ms.
Multilevel Queue Scheduling

Ready queue is partitioned into separate queues:


 foreground (interactive) processes
 background (batch) processes
The processes are permanently assigned to one queue, generally
based on some properties of process (memory size, process
priority, or process type.
Each queue has its own scheduling algorithm.
 foreground – Round-Robin
 background – FCFS
In addition, there must be scheduling among the queues, which is
commonly implemented as fixed-priority preempted scheduling.
Multilevel Queue Scheduling contd…
For example, the foreground queue may have absolute priority over the
background queue that means each queue has absolute priority over
lower-priority queues.
So, no process in the batch queue, for example, could run unless the
queues for system processes, interactive processes were all empty.
If an interactive editing process entered the ready queue while a batch
process was running, the batch process would be preempted. (e.g. Solaris
2)
Another possibility is to time slice between queues.
Each queue gets a certain portion of CPU time, which it can then
schedule among the various processes in its queue.
 Foreground queue ~ 80% of CPU time for RR scheduling
 Background queue ~ 20% of CPU time for FCFS scheduling
Multilevel Queue Scheduling contd…

Fig: Example of a multilevel queue scheduling


Multilevel Feedback Queue
In multilevel queue-scheduling processes doesn’t move from one
queue to another as processes are permanently assigned to a
queue on entry to the system.
However, multilevel feedback queue scheduling allows a process to
move between queues.
The idea is to separate processes with different CPU-burst
characteristics.
If a process uses too much of CPU time, it will be moved to a
lower-priority queue.
Similarly, a process that waits too long in a lower-priority queue
may be moved to a higher-priority queue.
This form of aging prevents starvation.
Example of Multilevel Feedback
Queue
Consider a multilevel feedback queue scheduler with three
queues:
 Q0 – time quantum 8 milliseconds
 Q1 – time quantum 16 milliseconds
 Q2 – FCFS
Scheduling:
 The scheduler first executes all processes in Q0. Only when Q0 will
empty, it will executes the processes in Q1.
 Similarly, processes in Q2 will be executed only if Q0 and Q1 are empty.
 A process that arrives for Q1 will preempt a process in Q2.
 A process that arrives for Q0 will preempt a process in Q1.
Multilevel Feedback Queues

A process entering the ready


queue is put in Q0.A process in Q0
is given a time quantum of 8 ms.
If it doesn’t finish with this time,
it is moved to the tail of Q1.
If Q0 is empty, the process at the
head of Q1 is given a quantum 16
ms.
 If it does not complete, it is
preempted and is put into Q2.
Multilevel Feedback Queue contd…
Multilevel-feedback-queue scheduler defined by the following
parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter
when that process needs service.
From the properties and definition of a multilevel feedback
queue scheduler makes it the most general CPU-scheduling
algorithm, but it also the most complex.
It can be configured to match a specific system under design.

You might also like