Cpu Scheduling
Cpu Scheduling
Long term
• Admission or
High level
scheduler
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
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
FCFS or FIFO
• (First Come First Served or First in First out)
Priority Scheduling
15
FCFS/FIFO
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
0 3 6 30
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
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
30
Quantum Size
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
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
37
HRRN
38
HRRN Example
Processes Service time Waiting time
P1 5sec 20sec
P2 3sec 9sec
39
Quiz
40
Multilevel Queue
41
MLFQ
42
MLFQ
43
MLFQ
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