OS Ch05

You might also like

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

Operating Systems

Chapter 5: CPU Scheduling


Dr. Ahmed Hagag
Scientific Computing Department,
Faculty of Computers and Artificial Intelligence
Benha University
2019
Chapter 5: CPU Scheduling

• Basic Concepts
• Scheduling Criteria
• Scheduling Algorithms

©Ahmed Hagag Operating Systems 2


Basic Concepts (1/11)

• CPU scheduling is the central in multi-programming


system.
• Maximum CPU utilization obtained with
multiprogramming (prevent CPU from being idle).
• Processes residing in the main memory is selected by the
Scheduler that is:
➢ Concerned with deciding a policy about which process is
to be selected.
➢ Process selection based on a scheduling algorithm.

©Ahmed Hagag Operating Systems 3


Basic Concepts (2/11)

©Ahmed Hagag Operating Systems 4


Basic Concepts (2/11)

©Ahmed Hagag Operating Systems 5


Basic Concepts (3/11)

• Process execution consists of a cycle of


CPU execution and I/O wait.
• Processes alternate between these two
states. Process execution begins with a
CPU burst. That is followed by an I/O
burst, which is followed by another
CPU burst, then another I/O burst, and
so on.
• CPU bursts vary greatly from process to
process and from computer to computer.

©Ahmed Hagag Operating Systems 6


Basic Concepts (4/11)

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).

©Ahmed Hagag Operating Systems 7


Basic Concepts (5/11)

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.

©Ahmed Hagag Operating Systems 8


Basic Concepts (6/11)

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

©Ahmed Hagag Operating Systems 9


Basic Concepts (7/11)

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.

©Ahmed Hagag Operating Systems 10


Basic Concepts (8/11)

Non-preemptive and Preemptive


Which is harder to implement? and why?

©Ahmed Hagag Operating Systems 11


Basic Concepts (9/11)

Non-preemptive and Preemptive


• Preemptive is harder: Need to maintain consistency of
data shared between processes, and more importantly,
kernel data structures (e.g., I/O queues).

©Ahmed Hagag Operating Systems 12


Basic Concepts (10/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.

©Ahmed Hagag Operating Systems 13


Basic Concepts (11/11)

Dispatcher

©Ahmed Hagag Operating Systems 14


Basic Concepts (11/11)

Dispatcher

©Ahmed Hagag Operating Systems 15


Basic Concepts (11/11)

Dispatcher

©Ahmed Hagag Operating Systems 16


Basic Concepts (11/11)

Dispatcher

©Ahmed Hagag Operating Systems 17


Scheduling Criteria (1/2)

• 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. (time from submission to termination)
• 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.

©Ahmed Hagag Operating Systems 18


Scheduling Criteria (2/2)

Scheduling Algorithm Optimization Criteria

• Max CPU utilization.


• Max throughput.
• Min turnaround time.
• Min waiting time.
• Min response time.

©Ahmed Hagag Operating Systems 19


Scheduling Algorithms (1/30)

• There are many different CPU-scheduling algorithms:


1. First Come, First Served (FCFS).
2. Shortest Job First (SJF).
➢ Preemptive SJF.
➢ Non-Preemptive SJF.
3. Priority.
4. Round Robin.
5. Multilevel queues.

©Ahmed Hagag Operating Systems 20


Scheduling Algorithms (2/30)

1. 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:

Note: A process may have many CPU bursts, but in the following
examples we show only one for simplicity.

©Ahmed Hagag Operating Systems 21


Scheduling Algorithms (3/30)

1. 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

©Ahmed Hagag Operating Systems 22


Scheduling Algorithms (3/30)

1. 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

©Ahmed Hagag Operating Systems 23


Scheduling Algorithms (3/30)

1. 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

©Ahmed Hagag Operating Systems 24


Scheduling Algorithms (4/30)

1. 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
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 25


Scheduling Algorithms (4/30)

1. 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
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27

Average waiting time: (0 + 24 + 27)/3 = 17


©Ahmed Hagag Operating Systems 26
Scheduling Algorithms (5/30)

1. 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
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 27


Scheduling Algorithms (5/30)

1. 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
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
24 27 30

Average turnaround time: (24 + 27 + 30)/3 = 27


©Ahmed Hagag Operating Systems 28
Scheduling Algorithms (6/30)

1. 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
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 29


Scheduling Algorithms (6/30)

1. 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
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 24 27

Average response time: (0 + 24 + 27)/3 = 17


©Ahmed Hagag Operating Systems 30
Scheduling Algorithms (7/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
Suppose that the processes arrive in the order: P2 , P3 , P1
The Gantt Chart for the schedule is:

©Ahmed Hagag Operating Systems 31


Scheduling Algorithms (8/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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

©Ahmed Hagag Operating Systems 32


Scheduling Algorithms (8/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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

©Ahmed Hagag Operating Systems 33


Scheduling Algorithms (8/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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

©Ahmed Hagag Operating Systems 34


Scheduling Algorithms (9/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 35


Scheduling Algorithms (9/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3

Average waiting time: (6 + 0 + 3)/3 = 3

©Ahmed Hagag Operating Systems 36


Scheduling Algorithms (10/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 37


Scheduling Algorithms (10/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 3 6

Average turnaround time: (30 + 3 + 6)/3 = 13


©Ahmed Hagag Operating Systems 38
Scheduling Algorithms (11/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 39


Scheduling Algorithms (11/30)

1. First-Come, First-Served (FCFS) Scheduling


Process Burst Time
P1 24
P2 3
P3 3
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

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
6 0 3

Average response time: (6 + 0 + 3)/3 = 3


©Ahmed Hagag Operating Systems 40
Scheduling Algorithms (12/30)

1. First-Come, First-Served (FCFS) Scheduling


• FCFS is fair in the formal sense or human sense of
fairness.
• but it is unfair in the sense that long jobs take priority
over short jobs and unimportant jobs make important
jobs wait.
• One of the major drawbacks of this scheme is that the
waiting time and the average turnaround time is often
quite long.

©Ahmed Hagag Operating Systems 41


Scheduling Algorithms (13/30)

2. 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.
➢ Could ask the user.

©Ahmed Hagag Operating Systems 42


Scheduling Algorithms (14/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

©Ahmed Hagag Operating Systems 43


Scheduling Algorithms (15/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

©Ahmed Hagag Operating Systems 44


Scheduling Algorithms (15/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

©Ahmed Hagag Operating Systems 45


Scheduling Algorithms (15/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

©Ahmed Hagag Operating Systems 46


Scheduling Algorithms (15/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

©Ahmed Hagag Operating Systems 47


Scheduling Algorithms (16/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 48


Scheduling Algorithms (16/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0

Average waiting time: (3 + 16 + 9 + 0)/4 = 7

©Ahmed Hagag Operating Systems 49


Scheduling Algorithms (17/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 50


Scheduling Algorithms (17/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
9 24 16 3

Average Turnaround time: (9 + 24 + 16 + 3)/4 = 13

©Ahmed Hagag Operating Systems 51


Scheduling Algorithms (18/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 52


Scheduling Algorithms (18/30)

2.1 Shortest-Job-First (SJF) Scheduling


ProcessArrival Time Burst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
3 16 9 0

Average Response time: (3 + 16 + 9 + 0)/4 = 7

©Ahmed Hagag Operating Systems 53


Scheduling Algorithms (19/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart

©Ahmed Hagag Operating Systems 54


Scheduling Algorithms (20/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 55


Scheduling Algorithms (20/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 56


Scheduling Algorithms (20/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 57


Scheduling Algorithms (20/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart

©Ahmed Hagag Operating Systems 58


Scheduling Algorithms (21/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 59


Scheduling Algorithms (21/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
(0 − 0) (1 − 1) (10 − 2) (5 − 3)

Average waiting time: (0 + 0 + 8 + 2)/4 = 2.5 msec

©Ahmed Hagag Operating Systems 60


Scheduling Algorithms (22/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 61


Scheduling Algorithms (22/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
(1 − 0) (5 − 1) (17 − 2) (10 − 3)

Average Turnaround time: (1 + 4 + 15 + 7)/4 = 6.75 msec

©Ahmed Hagag Operating Systems 62


Scheduling Algorithms (23/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 63


Scheduling Algorithms (23/30)

2.1 Shortest-Job-First (SJF) (Non-Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 1
P2 1 4
P3 2 7
P4 3 5
Non-Preemptive SJF Gantt Chart
Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
(0 − 0) (1 − 1) (10 − 2) (5 − 3)

Average Response time: (0 + 0 + 8 + 2)/4 = 2.5 msec

©Ahmed Hagag Operating Systems 64


Scheduling Algorithms (24/30)

Determining Length of Next CPU Burst


Can only estimate the length – should be similar to the previous one
Then pick process with shortest predicted next CPU burst

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

©Ahmed Hagag Operating Systems 65


Scheduling Algorithms (25/30)

Prediction of the Length of the Next CPU Burst

©Ahmed Hagag Operating Systems 66


Scheduling Algorithms (26/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

©Ahmed Hagag Operating Systems 67


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5 0 ms
Preemptive SJF Gantt Chart

©Ahmed Hagag Operating Systems 68


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5 0 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 69


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4
P3 2 9
P4 3 5 1 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 70


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3
P3 2 9
P4 3 5 2 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26

©Ahmed Hagag Operating Systems 71


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 3 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26

©Ahmed Hagag Operating Systems 72


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 3 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 2 3 5 10 17 26

©Ahmed Hagag Operating Systems 73


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 5 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 74


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 10 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 75


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 17 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 76


Scheduling Algorithms (27/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8 7
P2 1 4 3 2
P3 2 9
P4 3 5 26 ms
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

©Ahmed Hagag Operating Systems 77


Scheduling Algorithms (28/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 78


Scheduling Algorithms (28/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

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)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 80


Scheduling Algorithms (29/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

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

©Ahmed Hagag Operating Systems 81


Scheduling Algorithms (30/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒

©Ahmed Hagag Operating Systems 82


Scheduling Algorithms (30/30)

2.2 Shortest-remaining-time-first (Preemptive SJF )


ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart

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

©Ahmed Hagag Operating Systems 83


Scheduling Criteria (1/2)

• 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. (time from submission to termination)
• 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.

©Ahmed Hagag Operating Systems 84


Scheduling Criteria (2/2)

Scheduling Algorithm Optimization Criteria

• Max CPU utilization.


• Max throughput.
• Min turnaround time.
• Min waiting time.
• Min response time.

©Ahmed Hagag Operating Systems 85


Scheduling Algorithms (1/25)

• There are many different CPU-scheduling algorithms:


1. First Come, First Served (FCFS).
2. Shortest Job First (SJF).
➢ Preemptive SJF.
➢ Non-Preemptive SJF.
3. Priority.
4. Round Robin.
5. Multilevel queues.

©Ahmed Hagag Operating Systems 86


Scheduling Algorithms (1/25)

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

Problem  Starvation – low priority processes may never execute

Solution  Aging – as time progresses increase the priority of the process

©Ahmed Hagag Operating Systems 87


Scheduling Algorithms (2/25)

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

©Ahmed Hagag Operating Systems 88


Scheduling Algorithms (2/25)

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

©Ahmed Hagag Operating Systems 89


Scheduling Algorithms (3/25)

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

©Ahmed Hagag Operating Systems 90


Scheduling Algorithms (3/25)

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

©Ahmed Hagag Operating Systems 91


Scheduling Algorithms (3/25)

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

©Ahmed Hagag Operating Systems 92


Scheduling Algorithms (3/25)

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

©Ahmed Hagag Operating Systems 93


Scheduling Algorithms (3/25)

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

©Ahmed Hagag Operating Systems 94


Scheduling Algorithms (4/25)

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
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓

©Ahmed Hagag Operating Systems 95


Scheduling Algorithms (4/25)

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

©Ahmed Hagag Operating Systems 96


Scheduling Algorithms (5/25)

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
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓

©Ahmed Hagag Operating Systems 97


Scheduling Algorithms (5/25)

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

©Ahmed Hagag Operating Systems 98


Scheduling Algorithms (6/25)

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
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒 𝑷𝟓

©Ahmed Hagag Operating Systems 99


Scheduling Algorithms (6/25)

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

©Ahmed Hagag Operating Systems 100


Scheduling Algorithms (7/25)

4. Round Robin (RR) Scheduling


Each process gets a small unit of CPU time (time quantum q),
usually 10-100 milliseconds. After this time has elapsed, the
process is preempted and added to the end of the ready queue.
If there are 𝑛 processes in the ready queue and the time quantum
is 𝑞, then each process gets 1/𝑛 of the CPU time in chunks of at
most 𝑞 time units at once.
No process waits more than (𝑛 − 1)𝑞 time units.

©Ahmed Hagag Operating Systems 101


Scheduling Algorithms (7/25)

4. Round Robin (RR) Scheduling

©Ahmed Hagag Operating Systems 102


Scheduling Algorithms (8/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

©Ahmed Hagag Operating Systems 103


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 104


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 105


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 106


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 107


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 108


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 109


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 110


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 111


Scheduling Algorithms (9/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

©Ahmed Hagag Operating Systems 112


Scheduling Algorithms (10/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

# of context switches = ??

©Ahmed Hagag Operating Systems 113


Scheduling Algorithms (10/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

# of context switches = 7

©Ahmed Hagag Operating Systems 114


Scheduling Algorithms (11/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 115


Scheduling Algorithms (11/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Waiting Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 + (10 − 4) 4 7

Average waiting time: (6 + 4 + 7)/3 = 5.667 ms

©Ahmed Hagag Operating Systems 116


Scheduling Algorithms (12/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 117


Scheduling Algorithms (12/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Turnaround Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
30 7 10

Average Turnaround time: (30 + 7 + 10)/3 = 15.667 ms

©Ahmed Hagag Operating Systems 118


Scheduling Algorithms (13/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑

©Ahmed Hagag Operating Systems 119


Scheduling Algorithms (13/25)

4. Round Robin (RR) Scheduling


ProcessA arri Burst TimeT
P1 24
P2 3
P3 3
All the processes arrive at the same time 0.
Round Robin (RR) scheduling of quantum: 4 ms

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

Response Time
𝑷𝟏 𝑷𝟐 𝑷𝟑
0 4 7

Average Response time: (0 + 4 + 7)/3 = 3.667 ms

©Ahmed Hagag Operating Systems 120


Scheduling Algorithms (14/25)

• There are many different CPU-scheduling algorithms:


1. First Come, First Served (FCFS).
2. Shortest Job First (SJF).
➢ Preemptive SJF.
➢ Non-Preemptive SJF.
3. Priority.
4. Round Robin.
5. Multilevel queues.

©Ahmed Hagag Operating Systems 121


Scheduling Algorithms (15/25)

5. Multilevel Queue Scheduling


Ready queue is partitioned into separate queues, eg:
foreground (interactive)
background (batch)
Process permanently in a given queue
Each queue has its own scheduling algorithm:
foreground – RR.
background – FCFS.

©Ahmed Hagag Operating Systems 122


Scheduling Algorithms (16/25)

5. Multilevel Queue Scheduling


Scheduling must be done between the queues:
Fixed priority scheduling; (i.e., serve all from foreground
then from background). Possibility of starvation.
Time slice – each queue gets a certain amount of CPU time
which it can schedule amongst its processes; i.e., 80% to
foreground in RR.
20% to background in FCFS.

©Ahmed Hagag Operating Systems 123


Scheduling Algorithms (17/25)

5. Multilevel Queue Scheduling

©Ahmed Hagag Operating Systems 124


Scheduling Algorithms (18/25)

5. Multilevel Queue Scheduling


Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS

©Ahmed Hagag Operating Systems 125


Scheduling Algorithms (19/32)

5. Multilevel Queue Scheduling


Scheduling
A new job enters queue 𝑄0 which is served FCFS
 When it gains CPU, job receives 8 milliseconds.
 If it does not finish in 8 milliseconds, job is moved to queue 𝑄1 .

At 𝑄1 job is again served FCFS and receives 16 additional milliseconds


 If it still does not complete, it is preempted and moved to queue 𝑄2 .

©Ahmed Hagag Operating Systems 126


Scheduling Algorithms (20/32)

5. Multilevel Queue Scheduling


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 Using multi-processors
P3 2 20 or multi-core processor
P4 3 40

Multilevel Queue Fixed priority


non-preemptive

©Ahmed Hagag Operating Systems 127


Scheduling Algorithms (21/32)

𝑄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

©Ahmed Hagag Operating Systems 128


Scheduling Algorithms (21/32)

𝑄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

©Ahmed Hagag Operating Systems 129


Scheduling Algorithms (21/32)

𝑄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

©Ahmed Hagag Operating Systems 130


Scheduling Algorithms (21/32)

𝑄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

©Ahmed Hagag Operating Systems 131


Scheduling Algorithms (21/32)

𝑄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

©Ahmed Hagag Operating Systems 132


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44
P3 2 20 12
𝑄2 P4 3 40

23ms
7ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3

𝑄1 𝑃2

𝑄2

©Ahmed Hagag Operating Systems 133


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44
P3 2 20 12
𝑄2 P4 3 40

23ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4

𝑄1 𝑃2

𝑄2

©Ahmed Hagag Operating Systems 134


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36
P3 2 20 12
𝑄2 P4 3 40 32

31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms
𝑄1 𝑃2

𝑄2

©Ahmed Hagag Operating Systems 135


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36
P3 2 20 12
𝑄2 P4 3 40 32

31ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 136


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24
P3 2 20 12
𝑄2 P4 3 40 32

43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 137


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24
P3 2 20 12
𝑄2 P4 3 40 32

43ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 138


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24 8
P3 2 20 12
𝑄2 P4 3 40 32 16

59ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 139


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24 8
P3 2 20 12
𝑄2 P4 3 40 32 16

67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 140


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24 8
P3 2 20 12
𝑄2 P4 3 40 32 16

67ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 141


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24 8
P3 2 20 12
𝑄2 P4 3 40 32 16

83ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 142


Scheduling Algorithms (21/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT Burst Time
P1 0 7
𝑄1 P2 1 60 52 44 36 24 8
P3 2 20 12
𝑄2 P4 3 40 32 16

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 143


Scheduling Algorithms (22/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Waiting Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
P3 2
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 144


Scheduling Algorithms (22/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Waiting Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0
P3 2
=𝟎
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 145


Scheduling Algorithms (22/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Waiting Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0 7−1
P3 2
=𝟎 =𝟔
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 146


Scheduling Algorithms (22/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Waiting Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0 7−1 15 + 8 − 2
P3 2
=𝟎 =𝟔 = 𝟐𝟏
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 147


Scheduling Algorithms (22/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Waiting Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0 7−1 15 + 8 − 2 23 + 12 + 8 − 3
P3 2
=𝟎 =𝟔 = 𝟐𝟏 = 𝟒𝟎
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 148


Scheduling Algorithms (23/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Turnaround Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
P3 2
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 149


Scheduling Algorithms (23/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Turnaround Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
7−0
P3 2
=𝟕
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 150


Scheduling Algorithms (23/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Turnaround Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
7−0 67 − 1
P3 2
=𝟕 = 𝟔𝟔
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 151


Scheduling Algorithms (23/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Turnaround Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
7−0 67 − 1 43 − 2
P3 2
=𝟕 = 𝟔𝟔 = 𝟒𝟏
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 152


Scheduling Algorithms (23/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Turnaround Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
7−0 67 − 1 43 − 2 83 − 3
P3 2
=𝟕 = 𝟔𝟔 = 𝟒𝟏 = 𝟖𝟎
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 153


Scheduling Algorithms (24/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Response Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
P3 2
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 154


Scheduling Algorithms (24/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Response Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0−0
P3 2
=𝟎
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 155


Scheduling Algorithms (24/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Response Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0−0 7−1
P3 2
=𝟎 =𝟔
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 156


Scheduling Algorithms (24/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Response Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0−0 7−1 15 − 2
P3 2
=𝟎 =𝟔 = 𝟏𝟑
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 157


Scheduling Algorithms (24/32)

5. Multilevel Queue Scheduling


𝑄0
ProcessAarri Arrival TimeT
Response Time
P1 0
𝑄1 𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P2 1
0−0 7−1 15 − 2 23 − 3
P3 2
=𝟎 =𝟔 = 𝟏𝟑 = 𝟐𝟎
𝑄2 P4 3

7ms 15ms 23ms 31ms 43ms 59ms 67ms 83ms


7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 158


Scheduling Algorithms (25/32)

5. Multilevel Queue Scheduling


Now we add the concepts of varying arrival times and preemption to the
analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 Using single-core
P3 2 20 processor
P4 3 40

Multilevel Queue Fixed priority


non-preemptive

©Ahmed Hagag Operating Systems 159


Scheduling Algorithms (26/32)

𝑄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

©Ahmed Hagag Operating Systems 160


Scheduling Algorithms (27/32)

𝑄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

©Ahmed Hagag Operating Systems 161


Scheduling Algorithms (27/32)

𝑄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

©Ahmed Hagag Operating Systems 162


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 44 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms
7ms
7ms 8ms
𝑄0 𝑃1 𝑃2

𝑄1

𝑄2

©Ahmed Hagag Operating Systems 163


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 44 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms
7ms 23ms
7ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3

𝑄1

𝑄2

©Ahmed Hagag Operating Systems 164


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 44 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms
7ms 23ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4

𝑄1

𝑄2

©Ahmed Hagag Operating Systems 165


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms
7ms 23ms 47ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms
𝑄1 𝑃2

𝑄2

©Ahmed Hagag Operating Systems 166


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3

𝑄2

©Ahmed Hagag Operating Systems 167


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms
𝑄1 𝑃2 𝑃3

𝑄2

©Ahmed Hagag Operating Systems 168


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4

𝑄2

©Ahmed Hagag Operating Systems 169


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 170


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms
𝑄2 𝑃2

©Ahmed Hagag Operating Systems 171


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 172


Scheduling Algorithms (27/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16
15ms 31ms 59ms
7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms
𝑄0 𝑃1 𝑃2 𝑃3 𝑃4
16ms 12ms 16ms
𝑄1 𝑃2 𝑃3 𝑃4
36ms 16ms
𝑄2 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 173


Scheduling Algorithms (28/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT Burst Time
P1 0 7
P2 1 60 52 36 36 24 8
P3 2 20 12
P4 3 40 32 16

15ms 31ms 59ms


7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms 16ms 12ms 16ms 36ms 16ms
𝑃1 𝑃2 𝑃3 𝑃4 𝑃2 𝑃3 𝑃4 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 174


Scheduling Algorithms (29/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT
P1 0
Waiting Time
P2 1 36 24 8
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P3 2 0 6 + 16 13 + 24 20 + 28
P4 3 + 28 + 36
=𝟎 = 𝟓𝟎 = 𝟑𝟕 = 𝟖𝟒

15ms 31ms 59ms


7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms 16ms 12ms 16ms 36ms 16ms
𝑃1 𝑃2 𝑃3 𝑃4 𝑃2 𝑃3 𝑃4 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 175


Scheduling Algorithms (30/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT
P1 0
Turnaround Time
P2 1 36 24 8
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P3 2 7−0 111 − 1 59 − 2 127 − 3
P4 3 =𝟕 = 𝟏𝟏𝟎 = 𝟓𝟕 = 𝟏𝟐𝟒

15ms 31ms 59ms


7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms 16ms 12ms 16ms 36ms 16ms
𝑃1 𝑃2 𝑃3 𝑃4 𝑃2 𝑃3 𝑃4 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 176


Scheduling Algorithms (31/32)

5. Multilevel Queue Scheduling


ProcessAarri Arrival TimeT
P1 0
Response Time
P2 1 36 24 8
𝑷𝟏 𝑷𝟐 𝑷𝟑 𝑷𝟒
P3 2 0−0 7−1 15 − 2 23 − 3
P4 3 =𝟎 =𝟔 = 𝟏𝟑 = 𝟐𝟎

15ms 31ms 59ms


7ms 23ms 47ms 75ms 111ms 127ms
7ms 8ms 8ms 8ms 16ms 12ms 16ms 36ms 16ms
𝑃1 𝑃2 𝑃3 𝑃4 𝑃2 𝑃3 𝑃4 𝑃2 𝑃4

©Ahmed Hagag Operating Systems 177


Scheduling Algorithms (32/32)

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

Multilevel Queue Fixed priority


non-preemptive
Fig.1

©Ahmed Hagag Operating Systems 178


Dr. Ahmed Hagag
ahagag@fci.bu.edu.eg
https://www.youtube.com/channel/UCzYgAyyZTLfnLFjQexOKxbQ

https://www.facebook.com/ahmed.hagag.71/

You might also like