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

CPU Scheduling

Operating System Concepts


Objectives

 To introduce CPU scheduling, which is the


basis for multiprogrammed operating
systems
 To describe various CPU-scheduling algorithms
 To discuss evaluation criteria for selecting a
CPU- scheduling algorithm for a particular
system.
Basic Concepts
 Maximum CPU utilization obtained with
multiprogramming
 CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and
I/O wait
 CPU burst distribution
Scheduling Types
 There are two basic types of scheduling
 Non-Preemptive
 Process can’t be taken away from the CPU
 E.g. early days of computing i.e. batch processing
systems
 Response time of little concern
 Preemptive
 Process can be taken away from the CPU.
 Scheduler may preempt a process before it blocks or
terminates in order to allocate the CPU to another
process
 E.g. Interactive systems, Multiprogramming system, Time
Sharing Systems
Alternating Sequence of CPU And I/O Bursts
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 non-preemptive.
 All other scheduling is preemptive.
OS scheduling Schedulars

Long term
• Admission or
High level
scheduler

Short term Medium


term

7
Long Term Scheduler
 It decides which jobs or processes are to
be admitted to the ready queue (in the
Main Memory),
 that is, when an attempt is made to execute a
program, its admission to the set of currently
executing processes is either authorized or
delayed by the long-term scheduler.
 The long term queue exists in the Hard Disk or
the "Virtual Memory".
 Long-term scheduling is also important in large-
scale systems such as batch processing systems,
computer clusters, supercomputers etc.

8
Medium term Scheduler
 The medium-term scheduler temporarily
removes processes from main memory and
places them on secondary memory (such
as a disk drive) or vice versa.
 This is commonly referred to as "swapping out"
or "swapping in“, (Swapper)
 The medium-term scheduler may decide to swap
out a process which,
 has not been active for some time, or

 a process which has a low priority, or

 a process which is page faulting frequently, or

 a process which is taking up a large amount of


memory in order to free up main memory for other
processes.
9
Medium term scheduler
 Or swapping the process back in later when more memory
is available, or
 when the process has been unblocked and is no longer
waiting for a resource.

10
Short term Scheduler
 It also known as the CPU scheduler decides,
 which of the ready, in-memory processes are to be
executed (allocated a CPU) next following a clock
interrupt, an I/O interrupt, an operating system call
or another form of signal.
 Thus the short-term scheduler makes scheduling
decisions much more frequently than the long-term
or mid-term schedulers,
 a scheduling decision will at a minimum have to be made
after every time slice, and these are very short.
 This scheduler can be preemptive, implying that it is capable
of forcibly removing processes from a CPU when it decides
to allocate that CPU to another process.

11
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 – # of processes that complete their execution per


time unit

Turnaround time – amount of time to execute a particular


process

Waiting time – amount of time a process has been waiting in


the ready queue

Response time – amount of time it takes from when a request


was submitted until the first response is produced, not output
(for time-sharing environment)
Scheduling Algorithm Optimization
Criteria
Scheduling Algorithms

FCFS or FIFO
• (First Come First Served or First in First out)

SJF (Shortest Job First)

SRT (Shortest Remaining Time)

Priority Scheduling

Round Robbin Scheduling

Highest Response Ration Next (HRRN)

Multilevel Feed back Queues (MFQs) etc

15
FCFS/FIFO

 Jobs are scheduled according to their job arrival


time.
 It is a non preemptive algorithm.
 It tends to favor CPU bound processes.

FIFO

16
First-Come, First-Served (FCFS) Scheduling
Process Burst Time
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

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17
FCFS Scheduling (Cont)
 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

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Much better than previous case
 Convoy effect short process behind long process
Shortest-Job-First (SJF) Scheduling

 Associate with each process the length of its


next CPU burst. Use these lengths to
schedule the process with the shortest time
 SJF is optimal – gives minimum average waiting
time for a given set of processes
 The difficulty is knowing the length of the next
CPU request.
Example of SJF
Process Arrival Time Burst Time
P1 0.0 6
P2 0.0 8
P3 0.0 7
P4 0.0 3
 SJF scheduling Gantt Chart

P4 P1 P3 P2

0 3 9 16 24
 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
SRT (Shortest Remaining Time)
 It is preemptive version of SJF.
 Any time a new process enters the pool of
processes to be scheduled, the scheduler
compares the expected value for its remaining
processing time with that of the process currently
scheduled.
 If the new process’s time is less, the
currently scheduled process is preempted.
 Like SJF, SRT favors short jobs, and long jobs can
be victims of starvation.
 It is effective for those systems that received a
stream of incoming jobs, but it is no longer
useful in most of the today’s operating systems.
21
SRT
 Simply, in SRT a newly arriving process with
a shorter estimated run preempts a running
process with a longer run time to
completion.
Process Arrival time Processing Time
A 0.000 3
B 1.001 6
C 4.001 4
D 6.001 2

22
SRT
 Process A starts executing: it is the only
choice at time 0.
 It remain running when process B arrives because
its remaining time is less.
 At time 3, Process B is the only process in the
queue.
 At time 4.001, process C arrives and starts
running because its remaining time (4) is less
than process B’s remaining time (4.999).
 At time 6.001, process C remains running because
its remaining time (1.999) is less than process D’s
remaining time (2).
 When process C terminates, process D runs b/c
its remaining time is less than that of process B. 23
Shortest Remaining time

 Gantt Chart
Process Arrival time Processing Time
A 0.000 3
B 1.001 6
C 4.001 4
D 6.001 2

A B C D B
0 5 10 15 20

24
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) or
Highest integer  Highest priority, it varies from OS
to OS,
 Preemptive
 Non-preemptive
 SJF is a priority scheduling where priority is the
predicted next CPU burst time
 Problem  Starvation – low priority processes may never
execute
 Solution  Aging – as time progresses increase the priority
of the process
Example
 Draw the Gantt Chart illustrating their executing
using, highest number = highest priority
 Preemptive
 Non preemptive

Process Arrival time Burst Priority


A 0.000 4 3
B 1.001 3 4
C 2.001 3 6
D 3.001 5 5

26
Example Priority Scheduling

 Preemptive

A B C D B A
0 5 10 15 20

 Non preemptive

A C D B
0 5 10 15 20

27
Round Robin (RR)
 Each process gets a small unit of CPU time
(time quantum), usually 10-100 milliseconds.
After this time has elapsed, the process is
preempted and added to the end of the ready
queue.
 It is preemptive counter part of FIFO.
 If there are n processes in the ready queue and the
time quantum is q, then each process gets 1/n of
the CPU time in chunks of at most q time units at
once. No process waits more than (n-1)q time
units.
Round Robbin Scheduling
 Extensively used in time shared systems.
 If time quantum is small then,
 Good Response time but more context switches
 If large then same behavior as in FIFO

29
RR Working

 Interrupts from the interval timer ensures that


processing is suspended and control is
transferred to the scheduling algorithm at the
conclusion of the time quantum.

30
Quantum Size

 In Linux the default quantum size assigned to a


process is 100ms, but can vary from 10 to
200ms, depending on process priority and
behavior.
 In Window XP, this value is 20ms, but can vary
depending on whether the process in running in the
foreground or background of the GUI.

31
Round Robin (RR)

 Performance
 q large  FIFO
 q small  q must be large with respect to context
switch, otherwise overhead is too high.

32
Example RR Scheduling
 Draw the Gantt Chart illustrating their executing
using,
 Round Robbin (quantum = 2)
 Round Robbin (quantum = 1)
Process Arrival time Processing Time
A 0.000 3
B 1.001 6
C 4.001 4
D 6.001 2

A B A B C D B C
0 5 10 15 20

33
Round Robbin (q=1)

A B A B C B C D B C D B C B
0 5 10 15 20

Note: 13 context switches

34
Example of RR with Time Quantum = 4
Process Burst Time
P1 24
P2 3
P3 3
The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1

0 4 7 10 14 18 22 26 30

Typically, higher average turnaround than SJF,


but better response
Time Quantum and Context Switch Time
HRRN
 Highest Response Ratio Next
 Brinch Hanson developed the HRRN
 It correct some of the weaknesses in SJF
 Particularly the excessive bias against longer processes and
the excessive favoritism toward short processes.
 HRRN is a non preemptive scheduling
 In HRRN, process priority is a function not only of its
service time but also of its time spend waiting for service.
 Jobs gain higher priority the longer they wait, which
prevents indefinite postponement (process starvation).

37
HRRN

 HRRN calculates dynamic priorities


accordingg to the following formula,

 Service time appears in denominator, shorter processes


receive preference. However, because the waiting time appear
in numerator, longer processes that have been waiting will also
be given favorable treatment.
 This technique prevents the scheduler from indefinitely
postponing processes.

38
HRRN Example
Processes Service time Waiting time
P1 5sec 20sec
P2 3sec 9sec

 In this case process P1 has a priority 5 and Process


P2 has 4, so the system executes P1 before.

39
Quiz

 With HRRN, are short processes


always scheduled before long
ones???

False: The longer a process waits, the more likely it will be


Scheduled before shorter ones.

40
Multilevel Queue

 In multilevel queue scheduling we assign a


process to a queue and it remains in that
queue until the process is allowed access to
the CPU.
 Consider processes with different CPU burst
characteristics. If a process uses too much of
the CPU it will be moved to a lower priority
queue.
 This will leave I/O bound and (fast)
interactive processes in the higher priority
queue(s).

41
MLFQ

 Assume we have three queues (Q0, Q1 and


Q2). Q0 is the highest priority queue and Q2
is the lowest priority queue.
 The scheduler first executes process in Q0 and
only considers Q1 and Q2 when Q0 is empty.
 Whilst running processes in Q1, if a new
process arrived in Q0, then the currently
running process is preempted so that the Q0
process can be serviced.

42
MLFQ

 Any job arriving is put into Q0.


 When it runs, it does so with a quantum of 8ms
(say). If the process does not complete, it is
preempted and placed at the end of the Q1
queue.
 This queue (Q1) has a time quantum of
16ms associated with it.
 Any processes not finishing in this time are
demoted to Q2, with these processes being
executed on a FCFS basis.

43
MLFQ

 The above description means that any jobs


that require less than 8ms of the CPU are
serviced very quickly.
 Any processes that require between 8ms
and 24ms are also serviced fairly quickly.
 Any jobs that need more than 24ms are
executed with any spare CPU capacity once
Q0 and Q1 processes have been serviced.

44
Multilevel Feedback Queue
 A process can move between the various
queues; aging can be implemented this way
 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
Example of Multilevel Feedback Queue
 Three queues:
 Q0 – RR with time quantum 8 milliseconds
 Q1 – RR time quantum 16 milliseconds
 Q2 – FCFS
Multilevel Feedback Queues
Summary
Scheduling CPU Turnaround Response
Throughput
algorithm Overhead time time
First In First
Low Low High Low
Out
Shortest Job
Medium High Medium Medium
First

Priority based
Medium Low High High
scheduling

Round-robin
High Medium Medium High
scheduling

Multilevel
Queue High High Medium Medium
scheduling

48

You might also like