Professional Documents
Culture Documents
OS Ch05
OS Ch05
OS Ch05
• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms
Schedulers
• Long-term scheduler chooses some of them to go to
memory (ready queue).
• Then, short-term scheduler (or CPU scheduler) chooses
from ready queue a job to run on CPU.
• Medium-term scheduler may move (swap) some
partially-executed jobs from memory to disk (to
enhance performance).
CPU Scheduler
• Whenever the CPU becomes idle, the operating system
must select one of the processes in the ready queue to be
executed. The selection process is carried out by the
short-term scheduler, or CPU scheduler.
Scheduling can be
• Non-preemptive
➢ Once a process is allocated the CPU, it does not
leave until terminate.
• Preemptive
➢ OS can force (preempt) a process from CPU at
anytime.
✓ Say, to allocate CPU to another higher-priority process.
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.
Dispatcher
Dispatcher
Dispatcher
Dispatcher
Note: A process may have many CPU bursts, but in the following
examples we show only one for simplicity.
P1 P2 P3
0 24 27 30
P1 P2 P3
0 24 27 30
P1 P2 P3
0 24 27 30
P1 P2 P3
0 24 27 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3
0 24 27 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27
P1 P2 P3
0 24 27 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3
0 24 27 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
24 27 30
P1 P2 P3
0 24 27 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3
0 24 27 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27
P2 P3 P1
0 3 6 30
P2 P3 P1
0 3 6 30
P2 P3 P1
0 3 6 30
P2 P3 P1
0 3 6 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P1
0 3 6 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3
P2 P3 P1
0 3 6 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P1
0 3 6 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 3 6
P2 P3 P1
0 3 6 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P2 P3 P1
0 3 6 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
P4 P1 P3 P2
0 3 9 16 24
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0
P4 P1 P3 P2
0 3 9 16 24
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
9 24 16 3
P4 P1 P3 P2
0 3 9 16 24
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P4 P1 P3 P2
0 3 9 16 24
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n = actual length of n th CPU burst
2. n +1 = predicted value for the next CPU burst
3. , 0 1
4. Define : n=1 = t n + (1 − ) n .
Commonly, set to ½
Preemptive version called shortest-remaining-time-first
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26
P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26
P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 P2 P4 P1 P3
0 1 5 10 17 26
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
10 − 1 1−1 17 − 2 5−3
=𝟗 =𝟎 = 𝟏𝟓 =𝟐 Average waiting time = [9+0+15+2]/4 = 26/4 = 6.5 msec
©Ahmed Hagag Operating Systems 79
Scheduling Algorithms (29/30)
P1 P2 P4 P1 P3
0 1 5 10 17 26
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
17 − 0 5−1 26 − 2 10 − 3
= 𝟏𝟕 =𝟒 = 𝟐𝟒 =𝟕 Average turnaround time = [17+4+24+7]/4 = 52/4 = 13 msec
P1 P2 P4 P1 P3
0 1 5 10 17 26
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P1 P2 P4 P1 P3
0 1 5 10 17 26
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
0−0 1−1 17 − 2 5−3
=𝟎 =𝟎 = 𝟏𝟓 =𝟐 Average response time = [0+0+15+2]/4 = 17/4 = 4.25 msec
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)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU
burst time
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1 Highest priority
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
6 0 16 18 1 Average Waiting time = [6+0+16+18+1]/5 = 8.2 msec
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
16 1 18 19 6 Average Turnaround time = [16+1+18+19+6]/5 = 12 msec
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
3. Priority Scheduling
ProcessA arri Burst TimeT Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Priority scheduling Gantt Chart
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓
6 0 16 18 1 Average Response time = [6+0+16+18+1]/5 = 8.2 msec
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
# of context switches = ??
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
# of context switches = 7
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 + (10 − 4) 4 7
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 7 10
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 4 7
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
𝑄2
P3 2 20
P4 3 40
𝑄0
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
𝑄2
P3 2 20
P4 3 40
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
𝑄2
P3 2 20
P4 3 40
7ms
7ms 8ms
𝑄0 𝑃1 𝑃2
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60 52
𝑄2
P3 2 20
P4 3 40
15ms
7ms 8ms
𝑄0 𝑃1 𝑃2
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60 52
𝑄2
P3 2 20
P4 3 40
15ms
7ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3
𝑄1 𝑃2
𝑄2
23ms
7ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3
𝑄1 𝑃2
𝑄2
23ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
𝑄1 𝑃2
𝑄2
31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms
𝑄1 𝑃2
𝑄2
31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2
43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2
43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
59ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2
67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4
83ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
P3 2 20 𝑄2
P4 3 40
𝑄0
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
P3 2 20 𝑄2
P4 3 40
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
𝑄0
5. Multilevel Queue Scheduling
ProcessAarri Arrival TimeT Burst Time
𝑄1
P1 0 7
P2 1 60
P3 2 20 𝑄2
P4 3 40
7ms
7ms
𝑄0 𝑃1
𝑄1
𝑄2
𝑄1
𝑄2
𝑄1
𝑄2
𝑄1
𝑄2
𝑄2
𝑄2
𝑄2
𝑄2
Student Example:
Using a Multilevel Queue Scheduling algorithm in Fig.1. Consider the
following processes with the relative CPU bursts.
ProcessAarri Arrival TimeT Burst Time
P1 0 20
P2 1 60
P3 2 5
P4 2 40
https://www.facebook.com/ahmed.hagag.71/