L 7 Scheduling
L 7 Scheduling
Outline
Scheduling Objectives
Levels of Scheduling
Scheduling Criteria
Scheduling Algorithms
FCFS, Shortest Job First, Priority, Round Robin,
Multilevel
Multiple Processor Scheduling
Real-time Scheduling
Algorithm Evaluation
Introduction to Scheduling
Multiprogramming Environment, Many
processes competing for the CPU at the same
time.
Two or more processes are in ready state and
one CPU then choice is to be made which
process to run next.
The part of the OS that makes the choice is
called the scheduler and algorithm it uses is
called the scheduling algorithms.
Process Scheduling and Thread Scheduling
many things are same.
Introduction to Scheduling
Batch Systems
I/P in the form of card s or a magnetic tape
Scheduling was very simple, Just run the next job on
the tape
Time Sharing Systems
Scheduling algorithms became more complex
because multiple users waiting for service.
Because CPU time is a scarce resource, a good
scheduler can make a big difference in perceived
performance and satisfaction
Great work was done into devising cleaver &
efficient scheduling algorithms
Introduction to Scheduling
Advent of PCs situation changed in two ways
First, most of the time there is only one process
Second, computers became so faster that the CPU is
rarely a scarce resource any more.
Ex. Word or Excel running at a time
It hardly matters which goes first since the user
is waiting for both of them to finish
Scheduling does not matter much on simple PCs
Introduction to Scheduling
When we turn to high-end networked
workstations and servers the situation changes
Multiple processes often compete for the
CPU so scheduling matters
EX CPU has to choose between running a
process that updates the screen after the user
has closed the window
Running a process that sends out queued email
It makes huge difference in the perceived
response
Introduction to Scheduling
Closing Window must not take time
because user can perceive that and
If email is delayed by 2 seconds its okay,
even may not be noticed also.
In this case scheduling matters very much
In addition to pick a right process, scheduler
also has to worry about making efficient use
of the CPU because process switching is also
expensive.
Process Behavior
9/3/2024 10
When to Schedule
CPU scheduling decisions may take place
under following circumstances, When a
process
3. Switches from waiting to ready state
For example, at the completion of I/O
This is preemptive
4. Terminates
– For example, the process (running program) is done
– This is non-preemptive
9/3/2024 11
Process State
A process changes state as it executes.
https://youtu.be/Y3mQYaQsrvg
new admitted
exit terminated
5 Scheduler picks
another process 6
2
running
ready
3
Scheduler picks
I/O or this Process
event I/O or
completion event wait
4 1
blocked
CPU Scheduler
Selects from among the processes in memory that are ready to
execute, and allocates the CPU to one of them.
Non-preemptive Scheduling
Preemptive Scheduling
Process can be interrupted and must release the CPU.
Need to coordinate access to shared data
Can take the CPU back due to
Time slice
Higher priority process arrived
Interrupts
9/3/2024 13
Categories Of Scheduling Algorithms
Scheduling algorithms differs for different environment
and different systems
Batch
No users waiting for a quick response
Non-preemptive or preemptive algorithm with long time periods
for each process are often acceptable
This approach reduces process switches and thus improves
performance
Interactive
Preemption is essential to keep one process hogging the CPU
and denying service to the others
Real Time
With real time constraints
9/3/2024 14
Scheduling Algorithm Goals/ Scheduling
Criteria/ Objectives of Scheduling
What a good algorithm should do?
CPU Utilization
Keep the CPU and other resources as busy as possible
Throughput
# of processes that complete their execution per time
unit.
Turnaround time
amount of time to execute a particular process from its
entry time. (Time of Completion of Job – Time of
Submission of Job)
9/3/2024 15
Scheduling Algorithm Goals/ Scheduling
Criteria/ Objectives of Scheduling
Waiting time
amount of time a process has been waiting in the ready
queue.
Response Time (in a time-sharing
environment)
amount of time it takes from when a request was
submitted until the first response is produced, called
response time
The amount of time it takes to start responding, but not
the time it takes to output that response.
Scheduling Algorithm Goals/ Scheduling
Criteria/ Objectives of Scheduling
Priority
Give preferential treatment to processes with higher
priority
Balanced Utilization
Utilization of memory, I/O devices & other system
resources are also considered not only CPU utilization
Fairness
Can be reflected by treating all the processes same and
no process should suffer indefinite postponement
Scheduling
P1 P2 P3 P4
0 3 9 13 15
First-Come, First-Served(FCFS)
Scheduling order of the processes
0 3 9 13 15
Average waiting time
(0+3+9+13)/4 = 6.25ms
First-Come, First-Served(FCFS)
Scheduling Turn around Time:
0 3 9 13 15
Example
Example
Process Arrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Gantt Chart for Schedule
P1 P3 P2 P4
0 7 8 12 16
Average waiting time =
(0+6+3+7)/4 = 4
Shortest-Job-First(SJF) Scheduling
Advantages:
SJF is provably optimal, in that it gives the minimum
average waiting time for a given set of processes.
By moving short processes before long process
waiting time for short can be decreased by increasing
waiting time of the long process.
The average waiting time decreases.
Difficulty
With SJF is knowing the length of the next CPU
request
Can be work out by approximating the next CPU
Burst.
Example order of the processes
Non-Preemptive SJF P1, P2, P3,P4
P4 = (12-3)=9;
P1 P2 P4 P3
Average waiting time
0 P2 P3 P4 8 12 17 26
(0+7+15+9)/4 = 7.75ms
Example
Non-Preemptive SJF
Example Arrival Time for all
Process Burst Time processes is 0 ms
P1 3
P2 6 order of the processes
P3 4
P1, P2, P3,P4
P4 2
Example
Non-Preemptive SJF
Example Arrival Time for all processes is
0 ms
Process Burst Time
order of the processes
P1 3
P1, P2, P3,P4
P2 6
P3 4
Waiting time
P4 2
P1 = 2;
P2 = 9;
P3 = 5;
P4 P1 P3 P2
P4 = 0;
0 2 5 9 6
Average waiting time
(2+9+5+0)/4 = 4 ms
Example
Non-Preemptive SJF
Example Turn around Time
Process Burst Time P1 = 2+3 = 5
P1 3 P2 = 9+6 = 15
P2 6 P3 = 5+4 = 9
P3 4
P4 = 0+2 =2
P4 2
Avg. Turnaround Time =
(5+15+9+2)/4 = 7.75 ms
P4 P1 P3 P2
0 2 5 9 6
SRTF (Shortest Remaining Time First)
SJF is preemptive or non-preemptive
The choice arises when a new process arrives at
the ready queue while a previous process is
executing.
The new process may have shorter next CPU burst
than what is left of the currently executing process.
The preemptive SJF algorithm will preempt the
currently executing process, whereas a non-
preemptive SJF algorithm will allow the currently
running process to finish the CPU burst.
Preemptive SJF is sometimes called Shortest-
Remaining-Time-First
Preemptive SJF Scheduling(SRTF)
Example
Process Arrival TimeBurst Time
P1 0 7
P2 2 4
P3 4 1
P4 5 4
Gantt Chart for Schedule
P1 P2 P3 P2 P4 P1
0 2 4 5 7 11 16
Average waiting time =
(9+1+0+2)/4 = 3
Example SRTF
Waiting Time for
Process Arrival Time Burst Time P1 = 0 + (10-1) = 9 ms
P1 0 8
P2 = 1-1 = 0 ms
P2 1 4
P3 2 9
P3 = 17-2 = 15 ms
P4 3 5 P4 = 5-3 = 2 ms
Avg. Waiting Time =
(9+0+15+2) / 4 = 6.5 ms
Example
Process Arrival Time Processing Time
A 0 3
B 1 6
C 4 4
D 6 2
A 0 3
B 1 6
C 4 4
D 6 2
Disadvantages:
SRTF has higher overload than SJF.
Example
A 0 3
B 1 6
C 4 4
D 6 2
Calculate the Turnaround Time and Waiting Time for all processes applying
A. First Come First Serve algorithm (FCFS)
B. Shortest Job First algorithm (SJF)
C. Shortest Remaining Time First (SRTF)
D. Round Robin algorithm (with Time Quanta=3, Quanta=6, Quanta=10) (RR)
FCFS
Non-Preemptive SJF
Preemptive SJF
RR (time quanta = 3)
RR (time quanta = 6)
RR (time quanta = 10)
Comparison FCFS & RR
SR. FCFS RR
No.
1 FCFS is non-preemptive RR is preemptive
P3 = (2-2) ms = 0 ms